Design principle of Decorator mode

Source: Internet
Author: User

What is adorner mode? Dynamically adding some additional job responsibilities to an object, the adorner pattern is more flexible than the inherited subclass for added functionality.

From what we can see: The core class is the decorator class; it plays a key role in the middle. On the basis of inheriting the wrapper class, it also has a private object which needs to decorate the class, why do we need to inherit the class that need to decorate? We can see that at the time of the call, our method can implement the same kind of invocation as the method I was decorated with. We call the method of the decorated class in the operation () method in the Decorator class. Our specific Concretedecoratora and Concretedecoratorb are specific decorations, and we can specialize some operations in the operation () method of the class (note: This place is in the eye, You need to call the parent class's method: base. Operation ()).

We post the code first, and then do the analysis:

Component Abstract class Component{public abstract operation ();} Concretecomponent class concretecomponent:component{public override void operation () {Console.WriteLine ("Operation of specific objects" );}} Decorator classes abstract class decorator:component{protected Component component;//set specific virtual wrapper class object public void SetComponent ( Component Component) {this.component=component;} public override void operation () {if (component!=null) {component. Operation ();}}}    The Concretedecoratora class concretedecoratora:decorator{private string addedstate;//has a setcomponent method in the process of inheritance; public override void operation () {base. Operation ();//execute this adorner special directive addedstate= "XXXXXXX";}} The Concretedecoratorb class concretedecoratorb:decorator{//has the SetComponent method in the process of inheritance; public override void Operation ( {Addedbehavior ();//executes a special instruction base. Operation ();} public void Addedbehavior () {//Writing special code}}//client class {public static void main (string[] args) {concretecomponent c=new Co Ncretecomponent (); Concretedecoratora d1=new Concretedecoratora (); COncretedecoratorb d2=new Concretedecoratorb ();d 1.setComponent (c);d 2.setComponent (D1);d 2. Operation ();}}
From the Client Code section, we can see that the adornment mode is actually wrapping the object with the SetComponent method, which realizes the adornment object and how to use the adornment object to separate, each adornment object only needs to care about own function to be OK;

Design principle of 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.