Design Patterns in Java Io

Source: Internet
Author: User

Introduction:

The effect of learning the design mode and Java class library is not very good. The relationship between the design pattern and the Java class library is like the relationship between the 9 Yin Zhenjing and the Beidou method. One is the working principle, and the other is the tactics implemented by the principle, guo Jing forgot about the danger on xuanyuan's platform and focused on thinking is a wonderful combination of the two. Such a design pattern (increasing development time and cost, improving scalability and maintainability) can only reflect its value in the basic class library. The design pattern is used to lay the foundation, in reality, if a project uses a design model, it will inevitably be laborious and thankless. The key to learning the class library is to master the design model she uses, which is very solid and hard to forget.

Java Io class diagram

Image from: Click to open the link

The following article describes the design patterns used in I/O. The article is as follows:

Author: finnan <Li.zhongnan@hotmail.com>

Design Patterns are both theoretical and empirical. It is not a good way to simply follow the document to understand the design pattern. When experience increases, let's look back at the design pattern, which is more mysterious. It is more beneficial and useful.

In fact, many design modes are used in JDK to provide strong support for reusability, scalability and flexibility of class libraries.

Jdk I/O packages mainly use two design modes: adatper mode and decorator mode.

1. Adatper Mode

The adatper mode is also called the wrapper mode. The wrapper pattern can better understand its meaning. For example, the specific implementation of JNI technology is written in C language, but the Java program is provided to the user at last, that is, C code is packaged/wrapped/wrapper in Java. This is the wrapper mode, that is, the adapter mode.

By using the adatper mode, classes that normally cannot work together due to interface incompatibility can work together. By wrapping their own interfaces in an existing class.

Inputstreamreader is a bridge between byte flow and char stream in Java Io. It reads byte information from byte stream and decodes it into char stream, providing the method defined by the reader interface.

From the code, the inputstreamreader class defines a streamdecoder class variable SD:

Private Final streamdecoder SD;

When you create an inputstreamreader instance through the constructor inputstreamreader (inputstream in), you can use the input parameter inputstream to create a streamdecoder instance SD:

Public inputstreamreader (inputstream in ){
Super (in );
Try {
SD = streamdecoder. forinputstreamreader (in, this, (string) null); // # Check Lock Object
} Catch (unsupportedencodingexception e ){
// The default encoding shoshould always be available
Throw new error (E );
}
}

The code for implementing the read () method of the reader interface is:

Public int read () throws ioexception {
Return SD. Read ();
}

We can see that it is actually implemented by calling the streamdecoder read () method.

The image is:

You and I get together, and don't come, and I get together with others, you let me work, I let others work.

In addition, some classes in the Java Io package use this mode, such:

Outputstreamwriter: converts a char stream to a byte stream. That is, the method in writer is used, but the result writes characters to the byte stream.

Stringreader/stringwriter: converts Reader/writer to string.

Fileinputstream/fileoutputstream: connects file and inputstream/outputstream to read and write files.

Finally, let's take a look at the graphic description of the adatper mode:


2. Decorator Mode

The decorator mode, as its name implies, is the meaning of modification, decoration, and icing on the cake. If a class already has some functions, you can use the decorator mode if you want to add other functions.

By using the decorator mode, you can expand the functions of a class at runtime. The principle is: Add a decoration class to wrap the original class. The packaging method is generally to use the original object as the parameter of the modifier class constructor. The decoration class implements new functions. However, it can directly call the methods in the original class without using new functions. The decoration class must have the same interface as the decorated class.

Let's look at the linenumberreader class in Java Io, which inherits from bufferedreader. You can regard it as a class that implements the reader interface and adds functions such as Readline (), getlinenumber (), and setlinenumber.

This class provides two constructors:

Linenumberreader (Reader in)

Linenumberreader (Reader in, int sz)

The original and decorated reader is used as the constructor parameter. In addition to the functions required by the reader interface, new functions are added.

In applications, if we need to read characters from a file in one row and one line, we can use the linenumberreader class to complete such a function:

Linenumberreader reader = new linenumberreader (New filereader ("file_name "));

Here, Reader is a public interface, filereader is a decorated class, And linenumberreader is a decorative class. Linenumberreader and filereader both implement the reader interface. filereader serves as linenumberreader to construct a linenumberreader instance.

For the decorator mode, it can be like this:

We all work for the boss. We work together to do the same thing. We use me as the master and focus on your contact with the boss. I 've done almost the same thing. You can help me check and beautify it, finally, report to your boss.

The decorator mode can be used for illustration:


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.