View Java/IO library from decorator and adapter mode (1)

Source: Internet
Author: User

I think any book about the mode can't help but mention its practical application when talking about the decorator mode-applications in the Java/IO library, <Java and mode> This book is no exception, but it is a bit different. This book has a special topic during introduction. It looks at the Java/IO library in two modes, after completing this topic, I personally feel that I have a new understanding of the Java/IO library, while also deepening the understanding of the decorator mode and adapter mode, I would like to share with you the great achievements I have made. At the same time, I would like to explain that most of the following texts and images come from <Java and mode>.

I. Introduction (General Introduction to Java I/O)

Regardless of the programming language, input and output are an important part. Java is no exception, and Java greatly expands the input/output functions and application scope. It uses the stream mechanism to implement input/output. The so-called stream is the orderly arrangement of data, and the stream can be from a source (called streams or source of stream, to a destination (called a streaming or sink of stream. A program reads data from the input stream and writes data to the output stream.

For example, a program can use the fileinputstream class to read data from a disk file, as shown in:

 

A stream processor like fileinputstream is called a stream processor. Like a Stream pipeline, it sucks some type of data from a queue and outputs some type of data. The above is called a Stream pipeline diagram.

Similarly, you can use the fileoutputstream class to write data to a disk file, as shown in:

  

In practice, this mechanism is not very useful, and the program needs to write very structured information. Therefore, the byte data is actually numerical values, text, source code, and so on. The Java I/O Library provides a chaining mechanism that connects a stream processor to the beginning and end of another stream processor and uses the output of one of them as the input, form a Stream pipeline link.

For example, the datainputstream stream processor can use the output of the fileinputstream Stream object as the input, and convert the byte type data to the original Java type and string type data. As shown in:

Similarly, writing data of the byte type to a file is not a simple process. The data that a program needs to write to a file is often structured, while the byte type is the original type. Therefore, it must be converted during writing. The dataoutputstream stream processor receives the raw data type and the string data type, while the output data of this stream processor is of the byte type. That is to say, dataoutputstream can convert the source data to byte data and then input it.

In this way, you can link dataoutputstream to fileoutputstream, so that the program can write source data of the original data type and string type into the dual pipeline of this link, to write structured data to a disk file, as shown in:

 

This is the major role of the link.

The stream processing process by the stream processor must all have streams. If the streams processed by the stream class are classified, they can be basically divided into two categories:

The first array, String, file, etc. This is called the original struct.

Second, a stream of the same type is used as a link stream class, called a link pipeline.

Design Principles of Java I/O Libraries

The Java I/O Library abstracts a variety of common tokens, streams, and processing processes. The client's Java program does not need to know the final scheme, whether the streaming is a file on the disk or an array, or whether the data is buffered, and whether the data can be read by row number or other processing details.

As mentioned in the book, people who first met the Java/IO library are all confused by the complexity of the library, and those who are familiar with the library, however, it is often difficult to argue whether the library is designed properly. The author of the book puts forward his own opinions. To understand the huge and complex library of Java I/O, the key is to master two symmetry and two design pattern modes.

Java I/O libraries have two symmetry:

1. Input-Output symmetry. For example, inputstream and outputstream occupy the root of two parallel hierarchical structures of the input and output of the byte stream. Reader and writer occupy the root of the two parallel hierarchical structures of the input and output of the char stream.

2 byte-Char symmetry. inputstream and reader sub-classes are responsible for the input of byte and char streams respectively. outputstream and writer sub-classes are responsible for the output of byte and char streams respectively, they form a parallel hierarchical structure.

Two design modes of the Java I/O Library:

The overall design of the Java I/O Library conforms to the decorator and adapter modes. As mentioned above, the class for processing a stream in this database is called a stream class. The fileinputstream, fileoutputstream, datainputstream, and dataoutputstream mentioned in the Introduction are examples of stream processors.

1. modifier mode: in the hierarchical structure represented by inputstream, outputstream, reader, and writer, some stream processors can play a decorative role on other stream processors to form a new one, stream processor with improved features. The modifier mode is the overall design mode of the Java I/O library. This principle complies with the modifier mode, as shown in:

2. Adapter mode: in the hierarchical structure represented by inputstream, outputstream, reader, and writer, some stream processors adapt to other types of processors. This is the application of the adapter mode, as shown in.

  

The adapter mode is applied to the original stream processor design and forms the starting point of all stream processors in the I/O library.

This evening, we will first go here. Tomorrow, we will take a closer look at how the two design patterns are applied in the I/O library.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.