Decorator mode of design pattern (ii)

Source: Internet
Author: User

Decorator mode in practice is the Java input and output stream, in the class is very large, we are learning, we see so many classes are not sure, in fact, because used the decorator mode, a large number of classes are decorators.

For example, the FileInputStream class, FileInputStream used to read text files, is the decorator, and its decorators have bufferedinputstream and Linenumberinputstream, because with the two decorators, When reading a file, you can use FileInputStream to buffer input and count the number of rows.

So we can write a class that inherits from FilterInputStream and rewrite two read methods to customize our decorator class.

For example, I wrote a Uppercaseinputstream class that inherits from FilterInputStream, rewriting two read methods, one for bytes, one for byte arrays, and the lowercase letters in the method into uppercase.

Import*;p ublic class   Uppercaseinputstream extends Filterinputstream{public uppercaseinputstream ( InputStream in) {super (in);} public int read () throws ioexception//override method {int () for Byte, Return (C==-1?c:character.touppercase ((char) (c))) ;} public int read (byte[] b,int offset,int length) throws ioexception//Overwrite method {int (b,offset,length) for byte array ; for (int i=offset;i<offset+result;i++) {b[i]= (byte) character.touppercase ((char) b[i]);} return result;}}

Test class

Import*;p ublic class  Testuppercaseinputstream {public static void main (string[] args) throws exception{ InputStream in;in=new FileInputStream ("f:\\my\\java\\ design mode \ \ Decoration Mode \\src\\ decorative mode application \\test.txt");// The FileInputStream is equivalent to the decorator In=new Bufferedinputstream (in);//Decorate In=new uppercaseinputstream (in) with a buffered stream,//decorate int c;//with a stream of your own writing Read stream while (( ()) >=0) {System.out.print ((char) c+ "");} In.close ();}}

The content of the Test.txt is (write the lyrics with a word ...). )

The output is

And I found a little rule, that is, generally, if the constructor contains a reference to an input stream parameter, it is usually a decorator class, and if not, it is usually the decorator class.

Like what:

FilterInputStream only one construction method public FilterInputStream (InputStream in)
Which means it's a decorator.

FileInputStream does not have a similar construction method, indicating that it is a decorator.

It can be seen that the decorator design pattern has a drawback is that it is difficult to understand how it is designed, the learner is easy to see confused, but this does not affect its use.


After learning these design patterns, it is useful to learn the design patterns, before learning the decorator design pattern, I do not know the original class inside the structure in the end is how, why sometimes need to specify the reference in the construction method, but sometimes do not need, after learning to understand, so that learning design patterns for understanding Java basic knowledge is very helpful. Also, after learning the design pattern, the concept of object-oriented is more understood.

Learning Design patterns, I think to write code and drawing class diagram combination, otherwise it is difficult to understand how this design pattern is used, and must use the example of life to understand, to apply, this is my experience of these days learning.

Decorator mode of design pattern (ii)

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: 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.