Java design mode "Decorator pattern" decoration mode

Source: Internet
Author: User

I. Overview
Dynamically adding some additional responsibilities to an object, the adornment pattern is more flexible than the subclass implementation in terms of adding object functionality. Decorative mode is an object-structured pattern. Adornment mode is a technique used in place of inheritance, which uses an association relationship between objects to replace the inheritance relationship between classes. Decoration class is introduced in the decoration mode, which can call the method of the original class to be decorated and add new method to extend the function of the original class.

Second, the application scenario
Decorate the original object and expand the new feature/feature without changing the original object. Decorative mode can be used when the system cannot be extended in an inherited way or inherited is not conducive to system expansion and maintenance.

Third, UML class diagram

Iv. participants
①component (abstract artifacts): it is a common parent of concrete artifacts and abstract decorative classes, declaring the business methods implemented in concrete artifacts.
②concretecomponent (concrete component): It is a subclass of an abstract component class that defines a concrete artifact object (the decorator), implements the method declared in the abstract artifact, and the adorner can add additional responsibilities (methods) to it.
③decorator (Abstract decoration Class): It is also a subclass of abstract component classes, used to add responsibilities to specific components, but the specific responsibilities are implemented in their subclasses. It maintains a reference to an abstract artifact object that can invoke the method of decorating the previous artifact object and extend the method through its subclasses to achieve the purpose of decorating.
④concretedecorator (Specific decoration Class): It is a subclass of an abstract adornment class that is responsible for adding new responsibilities to the component. Each specific adornment class defines some new behaviors that can invoke methods defined in the abstract adornment class, and can add new methods to augment the behavior of the object.

V. Use case Learning
1. Role: Abstract Component Component.java

/** * 设计模式之 装饰模式<br/> * 角色:抽象构件<br/> * 被装饰的对象抽象类<br/> * @author [email protected] * */publicabstractclass Component {    publicabstractvoidoperation();}

2. Role: Concrete Component Class Concretecomponent.java

/** * 设计模式之 装饰模式<br/> * 角色:具体构件类<br/> * 抽象构件类的子类,定义具体的构件对象,也就是具体的被装饰对象<br/> * @author  [email protected] * */publicclass ConcreteComponent extends Component {    @Override    publicvoidoperation() {        System.out.println("我是具体的构件类(被装饰对象),这是我的原有方法");    }}

3, Role: Abstract Decorative class Decorator.java

/** * Design pattern decoration mode <br/> * Role: Abstract decoration class <br/> * is also a subclass of abstract component classes, which is designed to be able to perform multiple decorations <br/> * functions: Introduce abstract component classes, add responsibilities to specific component classes, But specific responsibilities are implemented in their subclasses <br/> * @author [email protected] * */ Public  class Decorator extends Component {    //Maintain a reference to an abstract artifact object    PrivateComponent Component;//Inject an object of an abstract artifact type     Public Decorator(Component Component) { This. Component = component; }@Override     Public void Operation() {//Call Legacy business methodsComponent.operation (); }}

4, Role: Specific decorative category Concretedecoratora.java

/** * Design pattern decoration Mode <br/> * Character: Specific decoration class <br/> * Add new specific responsibilities to the widget, expand the behavior of the original object * @author [email protected] * */
      Public  class concretedecoratora extends Decorator {     Public Concretedecoratora(Component Component) {Super(component); }@Override     Public void Operation() {Super. Operation ();//Call the enhanced business method to decorate, extend, and enhance the original objectAddedbehavior (); }Private void Addedbehavior() {System.out.println ("I am a concrete decorator Class A, I can enhance the original object method"); }}

5, Role: Specific decorative category Concretedecoratorb.java

/** * Design pattern decoration Mode <br/> * Character: Specific decoration class <br/> * Add new specific responsibilities to the widget, expand the behavior of the original object * @author [email protected] * */< /c0> Public  class concretedecoratorb extends Decorator {     Public Concretedecoratorb(Component Component) {Super(component); }@Override     Public void Operation() {Super. Operation ();//Call the enhanced business method to decorate, extend, and enhance the original objectAddedotherbehavior (); }Private void Addedotherbehavior() {System.out.println ("I am a concrete decoration Class B, I can also enhance the original object method"); }}

6, the Client test class Client.java

 Public classClient { Public Static void Main(string[] args)        {Component Component,decoratora; Component =NewConcretecomponent (); Decoratora =NewConcretedecoratora (component);//concretecomponent enhancement behavior of the original concrete component classDecoratora.operation (); System. out. println ("---------------------------\ n"); System. out. println ("---decoratora and decoratorb two times for the original component objects---");//If you need to re-decorate the Decoratora object after the original artifact object is decoratedComponent Decoratorb; Decoratorb =NewConcretedecoratorb (Decoratora);    Decoratorb.operation (); }}

7. Operation Result:

我是具体的构件类(被装饰对象),这是我的原有方法我是具体的装饰类A,我可以增强原有对象方法------------------------------对原有构件对象进行 decoratorA、decoratorB 二次装饰---我是具体的构件类(被装饰对象),这是我的原有方法我是具体的装饰类A,我可以增强原有对象方法我是具体的装饰类B,我也可以增强原有对象方法

Vi. Other

Java design mode "Decorator pattern" decoration 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.