Decorator design mode of Java. Io

Source: Internet
Author: User

When talking about the decorator mode, we often mention the java. IO Design Mode.

As a flexible and scalable class library, JDK uses a large number of design patterns. The research on Patterns in JDK not only increases the understanding of patterns, it also helps you to better understand the structure and composition of the class library.

In the java. Io package, the decorator mode is used for outputstream, inputstream, reader, and writer.

Decorator mode introduction:

The decorator mode is also called the wrapper mode. It is used to dynamically add some additional responsibilities to an object.

The following is the decorator mode of UML.

Component: base class of the decorated object

Defines the interface/abstract class of an object, which can dynamically add responsibilities to these objects.

Concretecomponent: The specific object to be decorated

Define a specific object, and decorete can add additional responsibilities to it

Decorator: the abstract class of the decorator.

Maintain a reference pointing to the component instance and define the same interface as the component.

Concretedecorator: decorator

A specific decoration object adds specific responsibilities to the specific decoration objects held internally

Decorator mode in outputstream in Java. Io package:

In the java. Io package, outputstream is a decorator mode design. The outputstream abstract class is used as the decorator, and bufferedoutputstream is concretedecorator.

Their relationships are illustrated in the UML diagram:

Outputstream serves as component and decorator, and filteroutputstream and bufferedoutputstream serves as concretedecorator. Bufferedoutputstream also provides specific decoration for filteroutputstream, such as the flush () and write (INT) methods.

Outputstream is an abstract class that is the common parent class of all output streams. Its source code is as follows:

Package java. IO;

Public abstract class outputstream implements closeable, flushable {

Public abstract void write (int B) throws ioexception;

Public void write (byte B []) throws ioexception {

Write (B, 0, B. Length );

}

Public void write (byte B [], int off, int Len) throws ioexception {

If (B = NULL ){

Throw new nullpointerexception ();

} Else if (Off <0) | (Off> B. Length) | (LEN <0) |

(Off + Len)> B. Length) | (off + Len) <0 )){

Throw new indexoutofboundsexception ();

} Else if (LEN = 0 ){

Return;

}

For (INT I = 0; I <Len; I ++ ){

Write (B [off + I]);

}

}

Public void flush () throws ioexception {

}

Public void close () throws ioexception {

}

}

It defines the abstract method of write (int B. This is equivalent to the component class in decorator mode.
Filteroutputstream is inherited from outputstream.

Package java. IO;

Public class filteroutputstream extends outputstream {

Protected outputstream out;

Public filteroutputstream (outputstream out ){

This. Out = out;

}

Public void write (int B) throws ioexception {

Out. Write (B );

}

Public void write (byte B []) throws ioexception {

Write (B, 0, B. Length );

}

Public void write (byte B [], int off, int Len) throws ioexception {

If (Off | Len | (B. Length-(LEN + off) | (off + Len) <0)

Throw new indexoutofboundsexception ();

For (INT I = 0; I <Len; I ++ ){

Write (B [off + I]);

}

}

Public void flush () throws ioexception {

Out. Flush ();

}

Public void close () throws ioexception {

Try {

Flush ();

} Catch (ioexception ignored ){

}

Out. Close ();

}

}

Protected outputstream in filteroutputstream
The out attribute is equivalent to the component of the decorator. Filteroutputstream does not really provide the write (INT) implementation, but calls the write method of out. The flush () method is the same. The specific write (INT) and flush () methods are implemented in bufferedoutputstream.

The Code Implementation of bufferedoutputstream is as follows:

Package java. IO;

Public

Class bufferedoutputstream extends filteroutputstream {

Protected byte Buf [];

Protected int count;

Public bufferedoutputstream (outputstream out ){

This (Out, 8192 );

}

Public bufferedoutputstream (outputstream out, int size ){

Super (out );

If (size <= 0 ){

Throw new illegalargumentexception ("buffer size <= 0 ");

}

Buf = new byte [size];

}

/** Flush the internal buffer */

Private void flushbuffer () throws ioexception {

If (count> 0 ){

Out. Write (BUF, 0, count );

Count = 0;

}

}

Public synchronized void write (int B) throws ioexception {

If (count> = Buf. Length ){

Flushbuffer ();

}

Buf [count ++] = (byte) B;

}

Public synchronized void write (byte B [], int off, int Len) throws
Ioexception {

If (LEN> = Buf. Length ){

Flushbuffer ();

Out. Write (B, off, Len );

Return;

}

If (LEN> Buf. Length-count ){

Flushbuffer ();

}

System. arraycopy (B, off, Buf, Count, Len );

Count + = Len;

}

Public synchronized void flush () throws ioexception {

Flushbuffer ();

Out. Flush ();

}

}

The bufferedoutputstream class provides the specific implementation of flush () and write (INT), which is the specific decoration of decorator.

References:

Software Package
Layered Structure of Java. Io

Original
Gof patterns

Coffee cup-decorator)

Thorough analysis of Java IO Design Patterns

 

Related Article

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.