Design pattern of Zen design pattern-Decorator mode

Source: Internet
Author: User

One: the definition of decorative pattern
---> Dynamically add some additional responsibilities to an object. For added functionality, the adornment mode is more flexible than generating subclasses.
---> If you remember the proxy mode, it is easy to understand this kind of diagram, the role of decoration class is a special proxy class.
---> In decoration mode, there must be one of the most basic, core, primitive interfaces or abstract classes to act as component abstract artifacts.


II: The role of decorative mode
Component Abstract Artifacts
Component is an interface or abstract class that defines our core object, the most primitive object
Concretecomponent Concrete Components
Concretecomponent is the core, the most primitive, the most basic interface or abstract class implementation, you want to decorate is it.
Decorator Decorative Characters
is generally an abstract class, what do you do with it? To implement an interface or abstract method, it is not necessarily an abstract method, in its attributes there must be a private variable pointing to the component abstract component.
Specific decorative characters
Concretedecoratora and Concretedecoratorb are two specific decorations, you want to decorate your core, the most primitive, the most basic things into other things



Three: The advantages and disadvantages of decorative mode

Advantages of Decorative Mode
Decorative and decorative classes can be developed independently, without coupling each other. In other words, the component class does not need to know the decorator class, and the decorator class extends the functionality of the component class from the outside, and decorator does not need to know the concrete artifacts.
Adornment mode is an alternative to inheritance relationships. We look at the decoration class decorator, regardless of how many layers are decorated, the returned object or component, the realization or is-a relationship.
Decoration mode can dynamically extend the functionality of an implementation class, which does not need to be said, the definition of decorative mode is the case.

Disadvantages of Decoration mode
It is enough to remember one thing about a decorative pattern: multi-layered decorations are more complex. Why is it so complicated? You think about it, like peeling onions, you peel it to the end and you find out that the innermost decoration is a problem, imagine the workload, so minimize the number of decorative classes to reduce the complexity of the system.

Four: The decorative mode of the application scenario
You need to extend the functionality of a class or add additional functionality to a class.
You need to dynamically add functionality to an object that can be revoked dynamically.
You need to retrofit or retrofit a group of siblings, which is of course the preferred decoration mode.




Five: Best Practices for decorating patterns
Decorative patterns are a powerful complement to inheritance. You have to know that inheritance is not omnipotent, inheritance can solve the actual problem, but in the project you have to consider such as easy to maintain, easy to expand, easy to reuse, and in some cases if you use inheritance will add many subclasses, and flexibility is very poor, it is of course maintenance is not easy, that is, decoration mode can replace inheritance, Solve the problem of our class expansion. At the same time, you have to know that inheritance is statically adding functionality to the class, while the adornment mode is dynamically adding functionality,
The decorating mode has a very good advantage: extensibility is very good. In a project, you will have a very large number of factors to consider, especially business changes, from time out to a demand, in particular, to make a large number of delays in the project needs, that mood is quite uncomfortable! Decoration mode can give us a good help to re-encapsulate a class through decoration mode, rather than through inheritance to complete

Six: examples of decorative patterns

"1" Abstract role

1  Packagecom.yeepay.sxf.template12;2 /**3 * Abstract character (decorative mode is essential)4  * @authorSXF5  *6  */7  Public Abstract classComponent {8     //an abstract approach9      Public Abstract voidoperate ();Ten}
View Code

"2" decorated real characters

1  Packagecom.yeepay.sxf.template12;2 /**3 * The real character being decorated4  * @authorSXF5  *6  */7  Public classConcretecomponentextendsComponent {8 9 @OverrideTen      Public voidoperate () { OneSystem.out.println ("Concretecomponent.operate (the core thing that really does")); A     } -  -      the}
View Code

Abstract role of the "3" decorator

1  Packagecom.yeepay.sxf.template12;2 /**3 * Abstract Decorator4  * @authorSXF5  *6  */7  Public Abstract classDecoratorextendscomponent{8     //decorated by9     PrivateComponent Component =NULL;Ten      One     //passing the decorator through a constructor function A      PublicDecorator (Component Component) { -          This. component=component; -     } the  -     //called by the decorator's real purpose - @Override -      Public voidoperate () { +          This. Component.operate (); -     } +      A}
View Code

The role of "4" Decorator 1

1  Packagecom.yeepay.sxf.template12;2 /**3 * Decorator 14  * @authorSXF5  *6  */7  Public classConcreteDecorator1extendsdecorator{8 9     PublicConcreteDecorator1 (Component Component) {Ten         Super(component); One     } A     - @Override -      Public voidoperate () { theSystem.out.println ("Concretedecorator1.operate () Decorator 1 did something"); -         Super. Operate (); -System.out.println ("Concretedecorator1.operate () Decorator 1 did something"); -     } +  -      +}
View Code

The role of "5" Decorator 2

1  Packagecom.yeepay.sxf.template12;2 /**3 * Decorator 24  * @authorSXF5  *6  */7  Public classConcreteDecorator2extendsDecorator {8     9      PublicConcreteDecorator2 (Component Component) {Ten         Super(component); One     } A  - @Override -      Public voidoperate () { theSystem.out.println ("Concretedecorator2.operate (decorator 2 did some things)"); -         Super. Operate (); -System.out.println ("Concretedecorator2.operate (decorator 2 did some things)"); -     } +  -  +  A}
View Code

Role of the "6" Client test class

1  Packagecom.yeepay.sxf.template12;2 /**3 * Test Class4  * @authorSXF5  *6  */7  Public classClienttest {8 9      Public Static voidMain (string[] args) {Ten         //the real decorator. OneComponent component=Newconcretecomponent (); A         //Decorator 1 Decoration genuine decorator -Component component2=NewConcreteDecorator1 (component); -         //Decorator 2 Decorative decorators 1 theComponent component3=NewConcreteDecorator2 (component2); -         //Performer Decorator 2 - component3.operate (); -         /** + * Execution Results - * Concretedecorator2.operate (decorator 2 did some things) + * Concretedecorator1.operate () Decorator 1 did some things A * Concretecomponent.operate (really the core thing to do at * Concretedecorator1.operate () Decorator 1 did some things - * Concretedecorator2.operate (decorator 2 did some things) -          */ -     } -}
View Code

Design pattern of Zen design pattern-Decorator 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.