Design Patterns in Java IO-adorners and Adapter modes

Source: Internet
Author: User

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

Adorner mode: the ability to dynamically extend an object without having to change the original class file and use inheritance

Adapter mode: Transforms the interface of a class into another interface that the customer wants

Directly on the Code analysis:

 PackageCom.think.cla;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader; Public classEcho { Public Static  voidMain (String [] args)throwsioexception{String Line; InputStreamReader input=NewInputStreamReader (system.in); System.out.println ("Enter data and push enter:"); BufferedReader Reader=NewBufferedReader (input); Line=Reader.readline (); System.out.println ("Data entered:" +Line ); } }

The Java I/O library has two symmetries, namely:

(1) Input-output symmetry: such as InputStream and OutputStream each occupy the input and output of the byte stream at the root of the two parallel hierarchical structure, while reader and writer occupy the root of the two parallel hierarchical structures of the input and output of the char stream respectively.

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

As you can see, this class accepts a System.in object of type InputStream, fits it into a reader type, and then uses the
The BufferedReader class "decorates" it, adding the buffering function to it. In this way, you can use the Readerline () of the BufferedReader object
Method reads the input data for the entire row, and the data type is string.

Design Patterns in Java IO-adorners and Adapter modes

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.