Beauty of design patterns: Decorator)

Source: Internet
Author: User

Index alias intent structure applicability disadvantage effect-related mode implementation method (1): the interface of the Decorator object must be consistent with the interface of the Component it is decorated. Implementation Method (2): omitting the abstract Decorator class. The alias Wrapper intends to dynamically add some additional responsibilities to an object. The Decorator mode is more flexible than the subclass generation function. Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending funible ality. Structure participant Component defines an object interface to dynamically add responsibilities to these objects. ConcreteComponent defines an object and can add some responsibilities to this object. The Decorator maintains a pointer to the Component object and defines an interface consistent with the Component interface. ConcreteDecorator adds responsibilities to components. APPLICABILITY The Decorator mode can be used in the following scenarios: Add responsibilities to a single object dynamically and transparently without affecting other objects. Handle revoking responsibilities. When the subclass generation method cannot be used for expansion. Disadvantage: Decorator is a transparent packaging, which is somewhat different from Component. Using the Decorator mode for system design often produces many similar small objects. As a result, it is difficult to learn the system and to troubleshoot problems. The effect is more flexible than static inheritance. Avoid having too many features in the class at the level of the hierarchy. The related mode Decorator mode is different from the Adapter mode, because the Decorator only changes the object's responsibilities without changing its interfaces, and the Adapter will give the object a new interface. Decorator can be regarded as a degraded Composite with only one component. However, Decorator only adds additional responsibilities to an object. It does not aim at object aggregation. Using a Decorator can change the appearance of an object, while the Strategy mode allows you to change the kernel of an object. This is two ways to change objects. When the Component class is huge, the price for using the Decorator mode is too high, and the Strategy Mode is relatively better. Implementation Method (1): the interface of the Decorator object must be consistent with the interface of the Component it decorated. All ConcreteDecorator classes must have a common parent class. The Decorator mode is used only to change components from the outside, so components do not have to have any knowledge about the decoration, that is, the decoration is transparent to the components. Copy code 1 namespace DecoratorPattern. implementation1 2 {3 public abstract class Component 4 {5 public abstract void Operation (); 6} 7 8 public class ConcreteComponent: Component 9 {10 public override void Operation () 11 {12 // do something13} 14} 15 16 public abstract class Decorator: Component17 {18 private Component _ component; 19 20 public Decorator (Component component) 21 {22 _ component = compo Nent; 23} 24 25 public override void Operation () 26 {27 _ component. operation (); 28} 29} 30 31 public class ConcreteDecorator: Decorator32 {33 public ConcreteDecorator (Component component) 34: base (component) 35 {36} 37 38 public override void Operation () 39 {40 base. operation (); 41 AddedBehavior (); 42} 43 44 private void AddedBehavior () 45 {46 // do some other things47} 48} 49 50 public class Client51 {5 2 public void TestCase1 () 53 {54 Component component1 = new ConcreteComponent (); 55 Component component2 = new ConcreteDecorator (component1); 56 57 component2.Operation (); 58} 59} 60} copy the code implementation method (2): omitting the abstract Decorator class. When you only need to add a role, there is no need to define the abstract Decorator class. In this case, the role of the Decorator to forward requests to the Component can be combined into the ConcreteDecorator. Copy code 1 namespace DecoratorPattern. implementation2 2 {3 public abstract class Component 4 {5 public abstract void Operation (); 6} 7 8 public class ConcreteComponent: Component 9 {10 public override void Operation () 11 {12 // do something13} 14} 15 16 public class ConcreteDecorator: Component17 {18 private Component _ component; 19 20 public ConcreteDecorator (Component component) 21 {22 _ component = component; 23} 24 25 public override void Operation () 26 {27 _ component. operation (); 28 AddedBehavior (); 29} 30 31 private void AddedBehavior () 32 {33 // do some other things34} 35} 36 37 public class Client38 {39 public void TestCase1 () 40 {41 Component component1 = new ConcreteComponent (); 42 Component component2 = new ConcreteDecorator (component1); 43 44 component2.Operation (); 45} 46} 47}

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.