Java Io and decorator Mode

Source: Internet
Author: User

The IO package in Java applies to the decorator mode. The following is the class structure of IO in Java:

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.

 

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.

Application of Three decoration Modes

After learning the decoration mode, you will find that the most famous application in Java is the Library Design of Java I/O standards. This section takes processing byte streams as an example to see how the decoration mode is applied.

Why not use the decoration mode without inheritance?

We know that the Java I/O Library requires a lot of combinations of performance. If these combinations are implemented through inheritance, each combination requires a class, in this way, a large number of repetitive problems will occur, so that the number of classes will "explode ". If the decoration mode is adopted, not only the number of classes is greatly reduced, but also the repetition of performance can be minimized. Therefore, the decoration mode is the basic mode of the Java I/O library. Here I would like to use an example in

In this example, beverage is an abstract class inherited by all drinks sold in a coffee shop. Beverage has an abstract Method Cost. All subclasses must implement this abstract method and calculate their prices. There are now four basic coffee: houseblend, darkroast, decaf, and espresso, all of which are inherited from beverage. The demand is that in the four most basic coffee, you can add condiments as needed, such as steamed milk, soy, mocha, and whipped milk. If we implement the combination of these condiments and the original coffee by inheritance, we will naturally design the following class diagram:

 

 

After seeing the class diagram above, we can't help but say that this is "class explosion ". We can draw the following design class diagram based on the design concept of the decoration mode:

 

Decorator pattern class diagram:

 

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.