Software architecture Experiment 1
1. Under Dos command, use dir command and dir|more command respectively to experience the pipeline filtering model
- dir displays subdirectories and files in the current working directory
- more displays one output screen at a time
- dir | more displays subdirectories and files under the current working directory by displaying one output screen at a time
The | here is the pipeline, which takes the output of the first command as the input and processes it with the second command.
Why use pipes
- Pipeline is one of the ways of communication between processes. Every command is a program
- The and sending of commands are compact and simple
- Using pipes to connect multiple commands in series can sometimes complete complex tasks that are inconvenient or impossible to execute alone
- Standard errors output from the pipeline are mixed together
filter
Several commands are combined through pipe symbols to form a pipe. In general, commands used in this way are called filters. The filter takes the input, modifies its contents in some way, and then outputs it.
- dir command
C:\Users\Liuhd>dir Driver C The volume in is Windows-SSD The serial number of the volume is 4009-8282 C:\Users\Liuhd Directory of 2021/10/10 13:44 <DIR> . 2021/08/1 six 13:37 <DIR> .. 2021/10/06 22:31 <DIR> .android 2021/09/25 11:32 340 .bash_history 2021/04/12 20:51 <DIR> .cache 2021/09/24 17:03 <DIR> .config 2021/04/12 20:51 <DIR> .eclipse 2021/10/01 12:08 16 .emulator_console_auth_token 2021/09/24 17:37 81 .gitconfig 2021/10/01 11:40 <DIR> .gradle 2021/09/24 17:03 <DIR> .halo 2021/09/29 16:10 <DIR> .jdks 2021/04/06 11:08 1,907 .labelmerc 2021/09/29 16:02 <DIR> .m2 2021/04/06 11:08 <DIR> .matplotlib 2021/10/10 13:44 <DIR> .mume 2021/04/06 11:07 12 .python_history 2021/03/29 20:59 <DIR> .ssh 2021/09/27 20:49 3,362 .viminfo 2021/03/23 20:31 <DIR> .vscode 2021/03/23 16:46 <DIR> 3D Objects 2021/04/06 11:02 <DIR> anaconda3 2021/10/01 11:24 <DIR> AndroidProject 2021/09/02 10:35 <DIR> CLionProjects 2021/08/16 17:40 <DIR> Contacts 2021/09/03 22:43 300 d4ac4633ebd6440fa397b84f1bc94a3c.7z 2021/10/11 12:22 <DIR> Desktop 2021/10/06 16:51 <DIR> Documents 2021/10/10 13:36 <DIR> Downloads 2021/04/12 20:51 <DIR> eclipse-workspace 2021/08/16 17:40 <DIR> Favorites 2021/10/06 17:05 <DIR> IdeaProjects 2021/08/29 20:19 0 import_sys.SQL 2021/09/03 22:09 66 inittk.ini 2021/09/03 22:09 41 inst.ini 2021/08/16 17:40 <DIR> Links 2021/08/16 17:40 <DIR> Music 2021/09/03 22:09 <DIR> Nox_share 2021/09/03 22:09 45 nuuid.ini 2021/10/11 14:01 <DIR> OneDrive 2021/08/29 18:09 <DIR> Oracle 2021/09/05 19:41 <DIR> Pictures 2021/04/26 13:24 <DIR> PotPlayer 2021/08/16 17:40 <DIR> Saved Games 2021/08/16 17:40 <DIR> Searches 2021/03/23 17:06 <DIR> UIDowner 2021/09/03 22:09 53 useruid.ini 2021/08/16 17:40 <DIR> Videos 2021/09/03 22:08 <DIR> vmlogs 12 Files 6,223 byte 37 Directories 119,086,456,832 Available bytes
- dir|more command
C:\Users\Liuhd Directory of 2021/10/10 13:44 <DIR> . 2021/08/16 13:37 <DIR> .. 2021/10/06 22:31 <DIR> .android 2021/09/25 11:32 340 .bash_history 2021/04/12 20:51 <DIR> .cache 2021/09/24 17:03 <DIR> .config 2021/04/12 20:51 <DIR> .eclipse 2021/10/01 12:08 16 .emulator_console_auth_token 2021/09/24 17:37 81 .gitconfig 2021/10/01 11:40 <DIR> .gradle 2021/09/24 17:03 <DIR> .halo 2021/09/29 16:10 <DIR> .jdks 2021/04/06 11:08 1,907 .labelmerc 2021/09/29 16:02 <DIR> .m2 2021/04/06 11:08 <DIR> .matplotlib 2021/10/10 13:44 <DIR> .mume 2021/04/06 11:07 12 .python_history 2021/03/29 20:59 <DIR> .ssh 2021/09/27 20:49 3,362 .viminfo 2021/03/23 20:31 <DIR> .vscode 2021/03/23 16:46 <DIR> 3D Objects 2021/04/06 11:02 <DIR> anaconda3 2021/10/01 11:24 <DIR> AndroidProject 2021/09/02 10:35 <DIR> CLionProjects 2021/08/16 17:40 <DIR> Contacts 2021/09/03 22:43 300 d4ac4633ebd6440fa397b84f1bc94a3c.7z 2021/10/11 12:22 <DIR> Desktop 2021/10/06 16:51 <DIR> Documents 2021/10/10 13:36 <DIR> Downloads 2021/04/12 20:51 <DIR> eclipse-workspace 2021/08/16 17:40 <DIR> Favorites 2021/10/06 17:05 <DIR> IdeaProjects 2021/08/29 20:19 0 import_sys.SQL 2021/09/03 22:09 66 inittk.ini 2021/09/03 22:09 41 inst.ini 2021/08/16 17:40 <DIR> Links 2021/08/16 17:40 <DIR> Music 2021/09/03 22:09 <DIR> Nox_share 2021/09/03 22:09 45 nuuid.ini 2021/10/11 14:01 <DIR> OneDrive 2021/08/29 18:09 <DIR> Oracle 2021/09/05 19:41 <DIR> Pictures 2021/04/26 13:24 <DIR> PotPlayer -- More --
2. Use the pipeline flow class of Java to realize the pipeline filtering architecture, complete the client to send a text, the receiving end to receive, and use threads to realize this function.
The pipeline input and output of java are actually implemented using a circular buffer array. The default size of this array is 1024 bytes. The input stream PipedInputStream reads data from the circular buffer array, and the output stream PipedOutputStream writes data to the circular buffer array. When the buffer array is full, the thread of the output stream PipedOutputStream will be blocked; When the buffer array is empty for the first time, the thread of the input stream PipedInputStream will block. However, when it is actually used, it will be found that it is not so easy to use.
Pipedinputstream. Connect (pipedinputstream) or pipedinputstream. Connect (pipedinputstream) has the same effect
Only one of them can be selected, and two connect ions cannot be called at the same time
Using pipedinputstream and PipedOutputStream in a thread will cause deadlock. The second attempt to write data through the pipeline output stream is blocked, and the data cannot be read from the pipeline input stream. Interested students can click these two classes to view the source code of read() and write(), both of which are synchronous methods.
So I designed a sender class Sender and a receiver class Receiver as both ends of the pipeline to send data in the main thread and receive data in the other receiving thread
In this way, threads can be avoided, and special attention should be paid to the synchronization relationship between sending and receiving
public class PipedDemo{ public static void main(String[] args) { Receiver receiver = new Receiver(); Sender sender = new Sender(receiver); sender.send(); new Thread(receiver).start(); } } class Sender{ private PipedOutputStream pipedOutputStream = new PipedOutputStream(); public Sender(Receiver receiver) { try { pipedOutputStream.connect(receiver.getPipedInputStream()); } catch (IOException e) { e.printStackTrace(); } } public void send() { try { String str = "Hello! I'm from the pipeline!"; pipedOutputStream.write(str.getBytes()); } catch (IOException e) { e.printStackTrace(); } } } class Receiver implements Runnable{ private PipedInputStream pipedInputStream = new PipedInputStream(); public PipedInputStream getPipedInputStream() { return pipedInputStream; } @Override public void run() { int n = 0; byte[] bytes = new byte[1024]; try { int l = pipedInputStream.read(bytes); System.out.println(new String(bytes,0,l)); } catch (IOException e) { e.printStackTrace(); } } }
(3) There is a graphic class in two-dimensional space. Based on this graphic class, some graphics are defined: rectangle, square, circle and ellipse. Use the idea of object-oriented programming, and calculate the area of each drawing.
Don't talk much. Look at the picture
- Calculable.java
public interface Calculable { double CalculateArea(); }
- Shape.java
public abstract class Shape implements Calculable{ protected double area; // the measure of area }
- Rectangle.java
public class Rectangle extends Shape{ private int length; private int width; @Override public double CalculateArea() { area = length * width; return area; } }
- Square.java
public class Square extends Shape{ private int side; @Override public double CalculateArea() { area = side * side; return side; } }
- Circular.java
public class Circular extends Shape{ private int radius; @Override public double CalculateArea() { area = Math.PI * radius * radius; return area; } }
- Ellipse.java
public class Ellipse extends Shape{ private int majorAxis; //the major axis private int minorAxis; //Minor axis @Override public double CalculateArea() { area = Math.PI * majorAxis * minorAxis; return area; } }