C # Decorator Pattern)

Source: Internet
Author: User

1. Decorator Pattern. The decoration mode dynamically extends the functions of an object without changing the original class file and using inheritance. It creates a packaging object, that is, decoration, to package a real object. 2. features (1) Decoration objects and real objects have the same interface. 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. 3. Application Scope 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. 4. Advantage 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. (This article is more reflected) 5. Disadvantages 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 actual projects. 6. design principles. 1. Multi-Purpose Combination and less inheritance. The behavior of inheriting child classes is determined statically at compilation, and all child classes will inherit the same behavior. However, if you can use a combination to expand the behavior of an object, you can dynamically scale it at runtime. 2. classes should be designed to be open to extensions and closed to modifications. 8. Simplified mode (two methods will be provided for reference at the end) 1. If there is only one Concrete Component class and there is no abstract Component Interface, Decorator can inherit the Concrete Component. 2. If there is only one Concrete Decorator class, you can merge Decorator and Concrete Decorator. 9. sample Code (1) abstract interface copy code // <summary> // defines the Component object interface // </summary> public abstract class Component {public abstract void Operation (); // an abstract responsibility} // <summary> // specific object // </summary> class ConcreteComponent: Component {public override void Operation () {Console. writeLine ("operations on specific objects") ;}/// modifier abstract class Decorator: Component {protected Component component; public void SetComponent (Comp Onent component) {this. component = component;} public override void Operation () {if (component! = Null) {component. operation () ;}} class ConcreteDecoratorA: Decorator {public override void Operation () {base. operation (); // first run the Operation () of the original Compnent, and then execute the functions of this class, such as AddedBehavior, which is equivalent to decorating the Console of the original Component. writeLine ("Operation of decoration object A") ;}} class ConcreteDecoratorB: Decorator {public override void Operation () {base. operation (); // first run the Operation () of the original Compnent, and then execute the functions of this class, such as AddedBehavior, which is equivalent to decorating the Console of the original Component. writeLine ("actions on decoration object B") ;}} copy code (2) No abstract interface copy code public class Car {public virtual void Description () {Console. write ("basic") ;}} public class ESPDecorator: Car {Car car; public ESPDecorator (Car car) {this. car = car;} public override void Description () {car. description (); Console. writeLine ("with ESP function") ;}} public class OtherDecorator: Car {Car car; public OtherDecorator (Car car) {this. car = car;} public override void Description () {car. description (); Console. writeLine ("with other functions") ;}// the first ConcreteComponent c = new ConcreteComponent (); ConcreteDecoratorA d1 = new ConcreteDecoratorA (); d1.SetComponent (c ); concreteDecoratorB d2 = new ConcreteDecoratorB (); d2.SetComponent (c); d2.Operation (); // type 2 Car car = new ESPDecorator (new OtherDecorator (new Car (); car. description ();

Related Article

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.