The decorator pattern for Java design patterns

Source: Internet
Author: User

Why the decorator pattern appears

To enhance the functionality of a class, you can create a new class to inherit from this class, which solves the problem, but if you add more functionality, the hierarchy of inheritance becomes deeper, causing the problem of inheritance redundancy

Decorator mode can enhance the functionality of a class without inheriting classes, by using an association relationship between objects instead of inheriting relationships between classes

Of course, you can also use proxy mode to enhance the functionality of the class

Roles that appear in decorator mode

1. Abstract class for decorators and decorators

2. The decorator (inheriting from the decorator and the decorator's abstract class)

3, the abstract of the adorner

4, the specific implementation of the adorner

below toLock the door and install the cat's eyeExample to introduce the decorator pattern class structure diagram in detail.

Code

Component

// abstract class of adorners and decorators  Public Abstract class Component {    // Common abstract method, that is, the method to be decorated public    abstract  void  Show ();

Door

// an abstract class of decorators and decorators who inherit from the decorator  Public class extends Component {    /**     * Specific implementation of decoration method * /     @Override     publicvoid  Show () {        System.out.println ("Everyone good I am the door");}    }

Componentdecorator

//Abstract of adorners Public Abstract classComponentdecoratorextendsComponent {//By The decorator's reference, can be manipulated by the decorator    PrivateComponent Component; //Decorator's constructor, used to initialize the adorner object     Publiccomponentdecorator (Component Component) { This. Component =component; }    /*** Rewrite the method to be decorated*/@Override Public voidShow () {component.show (); //Call the decorator's method of decoration    }}

Lockdecorator

//the concrete implementation of the adorner Public classLockdecoratorextendsComponentdecorator {/*** constructor *@paramComponent Decorator Object*/     Publiclockdecorator (Component Component) {Super(component); }    /*** Rewrite the method to be decorated*/@Override Public voidShow () { This. Setlock ();//Call decoration Method        Super. Show ();//call the parent class to decorate the method, that is, the decorator's method of decoration    }    /*** The implementation of decoration method*/     Public voidSetlock () {System.out.println ("Lock a component"); }}

Eyedecorator

//the concrete implementation of the adorner Public classEyedecoratorextendsComponentdecorator {/*** constructor *@paramComponent Decorator Object*/     Publiceyedecorator (Component Component) {Super(component); }    /*** Rewrite the method to be decorated*/@Override Public voidShow () { This. Seteye ();//Call decoration Method        Super. Show ();//call the parent class to decorate the method, that is, the decorator's method of decoration    }    /*** The implementation of decoration method*/     Public voidSeteye () {System.out.println ("Cat's Eye" for components); }}

Customer (Test Class)

 Public classCustomer { Public Static voidMain (string[] args) {Component door, decorator1, Decorator2; Door=NewDoor (); Decorator1=NewLockdecorator (door);//Decorative Door Objectsdecorator1.show (); System.out.println ("------------------I'm a serious dividing line------------------"); Decorator2=NewEyedecorator (Decorator1);//Decorative Decorator1 Objectsdecorator2.show (); }}
Run results
Lock the components I 'm the door------------------I'm a serious dividing line------------------for components add a cat's eye to the component lock everyone, I'm the door.
Use occasions

1. When you need to add a new feature to an existing object, you can consider decorator mode or proxy mode

2, the function of an object is often changed or often need to dynamically add functionality


JDK InputStream is the classic decorator model, interested in small partners can study the source

The decorator pattern for Java 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.