Design principles for I/O libraries in Java

Source: Internet
Author: User

The interview encountered this kind of problem, again make a record, in case just!

1. There are two design constructs in Java I/O: Decoration mode and Adapter mode

Symmetry in the 2.Java library:

(1) output-input symmetry: InputStream and OutputStream handling byte streams; reader and writer handling char streams.

(2) Byte-char symmetry: InputStream and reader subclasses are responsible for the input of byte and char respectively, and OutputStream and writer's subclasses are responsible for the output of byte and char streams respectively, which form parallel hierarchies.

3. Mode application:

Decoration Mode:

Decorative mode of InputStream

The various roles of the decorating mode:

(1) abstract component (Component) role: Played by InputStream. This is an abstract class that provides a unified interface for each seed-type stream processor.

(2) Specific component (concretecomponent) role: by Bytearrayinputstream, FileInputStream, PipedInputStream as well as stringbufferinputstream such as raw stream processor play. They implement an interface defined by the abstract component role, which can be decorated by a linked stream processor.

(3) abstract decoration (Decorator) role: Played by FilterInputStream. It implements the interface specified by the InputStream.

(4) Specific decoration (concretedecorator) role: Played by several classes, namely DataInputStream, Bufferinputstream and two infrequently used classes Linenumberinputstream and Pushbackinputstream

Note: StringBufferInputStream and Linenumberinputstream are obsolete and are no longer recommended for use.

OutputStream's Decorative pattern:

Individual roles in the adornment mode:

(1) abstract component (Component) role: Played by OutputStream. This is an abstract class that provides a unified interface for a variety of sub-type stream processors.

(2) Specific component (Concretecomponent) role: Played by Bytearrayoutputstream, FileOutputStream, and PipedOutputStream, They all implement the interfaces declared by the OutputStream.

(3) abstract decoration (Decorator) role: Played by Filteroutputstream. It has the same interface as the OutputStream, which is the key to decorating the class.

(4) Specific decoration (concretedecorator) role: Played by several classes, namely Bufferedoutputstream, DataOutputStream, and PrintStream.

Decorating mode in Reader:

Individual roles in the adornment mode:

(1) abstract component (Component) role: Played by reader. This is an abstract class that provides a unified interface for a variety of sub-type stream processors.

(2) Specific component (concretecomponent) role: CharArrayReader, InputStreamReader, Pipedreader and StringReader play, They all implement the interface that reader declares

(3) abstract decoration (Decorator) role: Played by BufferedReader and FilterReader. The two have the same interface as the Readeer, which is the key to the decoration class.

(4) The specific decoration (concreted) role: LineNumberReader as the specific decorative role of BufferedReader, pushbackreader as the specific decoration role of filterreader.

The decorating mode in writer:

Individual roles in the adornment mode:

(1) abstract component (Component) role: played by writer. This is an abstract class that provides a unified interface for a variety of sub-type stream processors.

(2) Specific component (Concretecomponent) role: Played by Chararraywriter, OutputStreamWriter, PipedWriter and StringWriter, They all implement the interface that reader declares.

(3) abstract decoration (Decorator) role: Played by BufferedWriter, Filterwriter, and PrintWriter, they have the same interface as writer.

(4) Specific decoration (concretedecorator) Role: It is merged with the abstract decorative character. Because of the merging of the abstract decorative characters with the specific decorative characters, the adornment mode is simplified here.

-----------------------------------------------------Gorgeous split-line-------------------------------------------------------------- --

Application of Adapter mode:

Bytearrayinputstream is an adapter class:

FileOutputStream is an adapter class:

StringBufferInputStream is an adapter class:

Bytearrayoutputstream is an adapter class:

PipedOutputStream is an adapter class:

CharArrayReader is an adapter class:

StringReader is an adapter class:

Others, about Inputstreamreader,pipedreader, etc. are also adapter classes.

Chararraywriter is an adapter class:

PipedWriter is an adapter class:

StringWriter is an adapter class:

Reference:

Http://www.2cto.com/kf/201404/291510.html

http://my.oschina.net/gao0516/blog/136103

Design principles for I/O libraries in Java

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.