Design Mode-decoration Mode

Source: Internet
Author: User

Decoration mode: The decoration mode dynamically extends the functions of an object without changing the original class file and using inheritance.

(1) Characteristics of the decoration mode:

(1) The decoration object has the same interface as the real object. In this way, the client object can interact with the decoration object in the same way as the real object. (2) A decoration object contains a reference of a real object (3) the decoration object accepts all requests from the client. It forwards these requests to real objects. (4) Decoration objects can add some additional functions before or after forwarding these requests. In this way, you can add additional functions externally without modifying the structure of the given object during running. In the object-oriented design, the function of a given class is extended by inheritance. (2) usage of the decoration mode: 1. You need to extend the functions of a class or add additional responsibilities to a class. 2. You need to dynamically add functions to an object. These functions can be dynamically revoked. 3. A large number of functions need to be added by the arrangement and combination of some basic functions to make the inheritance relationship unrealistic. 4. When the subclass generation method cannot be used for expansion. One case is that there may be a large number of independent extensions. To support each combination, a large number of subclasses will be generated, resulting in explosive growth of the number of subclasses. Another scenario is that the class definition is hidden, or the class definition cannot be used to generate a subclass. (3) Advantages of the decoration mode: 1. The purpose of the decorator mode and the inheritance relationship is to expand the object functions, but the decorator can provide more flexibility than inheritance. 2. By using different specific decorative classes and the arrangement and combination of these decorative classes, designers can create a combination of many different behaviors. (4) Disadvantages of the decoration model: 1. This kind of feature is more flexible than inheritance, and it also means more complexity. 2. The decoration mode will lead to many small classes in the design. Excessive use will complicate the program. 3. The decoration mode is designed for the component type. However, if you want to program specific components, you should rethink your application architecture and whether the decorator is appropriate. Of course, you can also change the Component Interface, add new public behavior, and implement the "Translucent" modifier mode. Make the best choice in the actual project. (5) differences between the decoration mode and the adapter mode: 1. About new responsibilities: the adapter can also add new responsibilities when switching, but the main purpose is not here. The decorator mode adds new responsibilities to the decorator. 2. Original interface: the adapter mode uses a new interface to call the original interface. The original interface is invisible or unavailable to the new system. The original interface is used in the decoration mode, and the system also uses the original interface for the decoration objects. (The modifier mode for adding new interfaces can be considered as its variant-"Translucent" modifier) 3. about the object of the package: the adapter knows the details of the adapter (that is, the class or the interface ). The decorator only knows what its interface is. The specific type (whether it is a base class or another derived class) is only known during running. (6) The roles in the decoration mode include:
(1) Abstract component (Component) Role: provides an abstract interface to standardize the objects to be prepared to receive additional responsibilities.
(2) concrete component role: defines a class that will receive additional responsibilities.
(3) decorator: holds an instance of a component object and implements an interface consistent with the abstract Component Interface.
(4) The concrete decorator role is responsible for adding additional responsibilities to the component object. (7) Example
 1 using System; 2  3 namespace 装饰模式 4 { 5     abstract class Component 6     { 7         public abstract void Operation();  8     } 9     class ConcreteComponent:Component10     {11         public override void Operation()12         {13             Console.WriteLine("需要扩展功能的对象的原有操作。");14         }15     }16     abstract class Decorator:Component17     {18         public Component component;19         public void SetComponent(Component component)20         {21             this.component = component;22         }23         public override void Operation()24         {25             if (component != null)26             {27                 component.Operation();28             }29         }30     }31     class ConcreteDecoratorA : Decorator32     {33         public override void Operation()34         {35             base.Operation();36             Console.WriteLine("功能A...");37         }38     }39     class ConcreteDecoratorB : Decorator40     {41         public override void Operation()42         {43             base.Operation();44             Console.WriteLine("功能B...");45         }46     }47     class Program48     {49         static void Main(string[] args)50         {51             ConcreteComponent c = new ConcreteComponent();52             ConcreteDecoratorA ca = new ConcreteDecoratorA();53             ConcreteDecoratorB cb = new ConcreteDecoratorB();54             //装饰过程55             ca.SetComponent(c);56             cb.SetComponent(ca);57 58             cb.Operation();59 60             Console.ReadKey();61         }62     }63 }

 

Design Mode-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.