View Java IO from design mode

Source: Internet
Author: User

One, decorator mode

There are two ways to extend the functionality of a class:

(1) class inheritance (high coupling, which produces more subclasses, causing the explosion of the class)(2) object combination is decorative mode (decoupling, does not create more subclasses) dynamically add functionality to the object) so the class should be open to the extension and closed for modification.  Decorator Design Pattern considerations:(1) The decorator and the decorator must have the same super-type. (2) The decorator can be packaged by the decorator, can also be packaged decorator. Often use multilayer packaging to achieve the goal.

(3) The adorner combines the object of the decorator (the key feature of the decoration Class). Allows us to dynamically extend behavior through nested decorations.

Four concepts:

(1) abstract component Roles (Component):
It is an interface that encapsulates the functionality (methods) that will be added.
It is equivalent to the InputStream in IO. (or OutputStream)

(2) specific component Roles (concretecomponent):
It is a class that implements the Component interface, so an implementation of the functionality to be added is encapsulated in the class.

(3)   decorative character (  Decorator) :  It is a class that also implements the  Component  interface and must also hold a reference to an interface  Component  object.

b:  overriding method (that is, added functionality) requires call  Component  The method for the object.

(4) the specific decorative role (subclass of the Decorator class, can have one, can also have multiple):
These classes inherit the class Decorator, overriding the method of the parent class (the function to be added), and its own construction method.
A: The construction method uses super.
B: The first step: This method of the super parent class.
Step Two: Add your own features (some sentences).

Decorator: Java IO Framework and Collection framework

(i) Java IO framework J DK5.0 more than 60 IO stream classes comprise four families: InputStream, OutputStream, Reader, Writer.

Inputstream/outputstream is an abstract class that operates on a sequence of bytes.

Reader/writer is an abstract class that operates based on Unicode code units.

The InputStream has a read-in function that is an abstract adorner.

FileInputStream specific adorner with read-in file function

FilterInputStream has the abstract meaning of adorners

Bufferedinputstream Decorator with specific function (buffering function)

• inputstream: Byte sequence input class originator----------------------Component Public abstract class InputStream implements Closeable• The most basic way to read bytes is to extend the subclass. Public abstract int Read () throws IOException;•• FileInputStream: reads the byte stream class in the file Inherits InputStream------------concretecomponent public class FileInputStream extends InputStream• Constructor functionpublic FileInputStream (String name) throws FileNotFoundException• Local methods, specific read-in methods that interact with the lower operating systemPublic native int read () throws IOException; (based on the underlying implementation of the local method, Java calls a non-Java program implementation)

FilterInputStream: Filter Flow class, from the adorner function, for the input assembly of various functions------------Decoratorpublic class FilterInputStream extends InputStream• Used to record the InputStream object that is being decorated, which requires new features to be assembledprotected volatile inputstream in;• Construct adorner in to set the InputStream object to be wrappedprotected FilterInputStream (InputStream in) {this.in = in;//set to be wrapped InputStream object}• Read in bytespublic int read () throws IOException {return In.read ();}•bufferedinputstream: The input stream has buffering function, it is an adorner that can assemble the buffering function, inherit FilterInputStream------------------Concretedecorator public class Bufferedinputstream extends FilterInputStream//Constructor in is the inputstream of the assembly buffering function

Public Bufferedinputstream (InputStream in) {This (in, defaultbuffersize);

Public ReadLine () {}

View Java IO from design mode

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.