Design mode-Adorner mode (Decrator model)

Source: Internet
Author: User

Wen/Vincentzh

Original link: http://www.cnblogs.com/vincentzh/p/6057666.html

Catalogue
    1. 1. Overview
    2. 2. Purpose
    3. 3. Structure composition
    4. 4. Realize
    5. 5. Summary
1. Overview

Adorner mode dynamically expands the functionality of an object without altering the original class file and using inheritance. Usually add functionality to an object, either directly in the class to modify, or through subclass inheritance to extend the function of the object, or use the decorative mode of the corresponding functions of any combination, so that the expansion of the function is flexible and dynamic. Decorating mode is the function of wrapping objects by creating a wrapper object, which is the adornment to wrap the real object and extend the wrapper object to the real object.

2. Purpose

The purpose of the adorner is: through any combination of decorative objects, the real object of the functional packaging, to achieve the expansion of the object function of the dynamic, simple, reusability, flexibility.

3. Structure composition

The decorative pattern mainly involves four characters: abstract theme character, specific theme character , abstract decorative character , specific decorative role

  Abstract Theme Roles : Abstract theme roles provide the functions (basic functional methods) that both specific roles and abstract decorative roles implement together, and the specific topic roles and abstract decorative roles must implement the method of abstract theme role definition;

  specific topic role: The specific theme role is the real object class in the decoration mode, the method of implementing abstract theme Role (basic function method), adding additional functions to the role through the specific decorative role wrapper;

  Abstract Decorative Role : The parent class of all adorners implements the function of the abstract theme character (mainly for the reuse of the adorner function, that is, the decorator a can decorate another specific adorner B, because the adorner class is also an abstract theme role) and holds an abstract theme role object , the object is actually the object being decorated. If you do not inherit a component interface class, you can only add a single function to a component, that is, the adorner object cannot decorate other adorner objects.

  Specific Decorative role : The specific adorner class, to achieve specific features to be added to the decorated object. Used to decorate a specific component object or another specific adorner object.

4. Realize

Abstract theme Roles

1  Package Com.cnblogs.vincentzh.decoratormodel; 2 // Abstract Theme Roles 3 // define basic function methods only 4  Public Interface Component 5 {6      Public void function (); 7 }

Specific theme Roles

1  PackageCom.cnblogs.vincentzh.decoratormodel;2 //Specific Theme Roles3  Public classConcretecomponentImplementsComponent4 {5 @Override6      Public voidfunction ()7     {8 basefunction ();9     }Ten      One      Public voidbasefunction () A     { -System.out.println ("Basic Features"); -     } the}

Abstract decorative Characters

1  PackageCom.cnblogs.vincentzh.decoratormodel;2 //Abstract Decorative characters3  Public classDecoratorImplementsComponent4 {5     PrivateComponent Component; // roles with abstract themes  6     7      PublicDecorator (Component Component)8     {9          This. Component =component;Ten     } One      A @Override -      Public voidfunction () -     { the component.function (); -     } -}

Two specific decorative characters

1  PackageCom.cnblogs.vincentzh.decoratormodel;2 //specific decorative role a3  Public classConcretedecoratoraextendsDecorator4 {5      PublicConcretedecoratora (Component Component)6     {7         Super(component);8     }9     Ten @Override One      Public voidfunction () A     { -         Super. function (); - Functiona (); the     } -      -      Public voidFunctiona () -     { +SYSTEM.OUT.PRINTLN ("Additional function a"); -     } +}

 PackageCom.cnblogs.vincentzh.decoratormodel;//Specific decorative role B Public classConcretedecoratorbextendsdecorator{ PublicConcretedecoratorb (Component Component) {Super(component); } @Override Public voidfunction () {Super. function ();    Functionb (); }         Public voidfunctionb () {System.out.println ("Additional function B"); }}

Client

1  PackageCom.cnblogs.vincentzh.decoratormodel;2 //Client Test class3  Public classClient4 {5      Public Static voidMain (string[] args)6     {7Component Component =Newconcretecomponent ();8Component Decoratora =NewConcretedecoratora (component);9Component Decoratorb =NewConcretedecoratorb (Decoratora);Ten          One decoratorb.function (); A         //when calling Decoratorb's function method, the function method of the parent class Decorator is executed first, and -         //Decorator holds Component object references (all decorated and decorated classes are Component), -         //calls the function method that passes in the Component Reference object, where Decoratorb executes first the         //Decoratora method, and Decoratora executes the component function method first, passing -         //A reference to the adorner or the real object will go first to the method under the Reference object, which is what the adorner can -         //the reason for the random combination.  -     } +}

Execution results

5. Summary

The I/O flow in Java is the use of decorative mode I/o streams can be divided into node stream and filter stream . ,

 node Flow : Is the basic class that deals directly with input/output devices or file systems, such as: FileInputStream, FileOutputStream, etc.;

 Filter Flow : Filter filter, that is, the existing input/output stream to the corresponding additional processing, such as buffer read/write (Bufferedinputstream, bufferedoutputstream), etc.;

InputStream and OutputStream are the parent of all input/output streams, which is the so-called abstract theme character (Component) in the adornment mode, and the class that belongs to the node stream is naturally the specific theme character (Concretecomponent). They are responsible for interacting with hardware or file systems to form the most basic read-write operation flow; FilterInputStream and Filterroutputstream are the parent classes of all filter streams, and they inherit from InputStream and OutputStream, The corresponding is the abstract decorative character (Decorator); The specific filter flow is the specific decorative role (concretedecorator), which is responsible for the basic functions of the node stream packaging add function, such as: bufferinputstream add buffer read function.

In the Hadoop programming model, the two basic classes of MapReduce for HDFS file data reads Fsdatainputstream and Fsdataoutputstream are also inherited from Java FilterInputStream and Fil Teroutputstream, in this way, the principle of read/write for HDFS file is not different from the principle of general file read/write, but the addition of the Distributed file read function based on the original file read/write.

Design mode-Adorner mode (Decrator model)

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.