Decoration Mode C #

Source: Internet
Author: User

Decoration Mode C #
1. Overview
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) 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.
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. Advantages
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 practical)
5. Disadvantages
1. This 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
6. Design Principles
1. Multi-Purpose Combination, 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.

Instance:

// Modifier mode abstract class public abstract class BaseDecorator {protected ReturnInfo returnInfo = new ReturnInfo () {Code = 1, IsSuccess = false}; public BaseDecorator decorator = null; // In the decorator mode, the last decorator object must be input. If not, null public BaseDecorator (BaseDecorator _ decorator) {decorator = _ decorator ;} // execution method public abstract ReturnInfo Run (string orderId, string supplierId, string supOrderId); // The returned result protected ReturnInfo GetResult () {returnInfo. code = 0; returnInfo. isSuccess = true; returnInfo. message = "operation successful"; return returnInfo ;}}
Public class DecoratorA: BaseDecorator {public DecoratorA (BaseDecorator decorator): base (decorator) {} public override Model. returnInfo Run (string orderId, string supplierId, string supOrderId) {// method of execution // you need to execute if (decorator! = Null) {return decorator. Run (orderId, supplierId, supOrderId) ;}else {return GetResult ();}}}
Public class DecoratorB: BaseDecorator {public DecoratorB (BaseDecorator decorator): base (decorator) {} public override Model. returnInfo Run (string orderId, string supplierId, string supOrderId) {// method of execution // you need to execute if (decorator! = Null) {return decorator. Run (orderId, supplierId, supOrderId) ;}else {return GetResult ();}}}
Public class Test {BaseDecorator decorator = new DecoratorA (null); decorator = new DecoratorB (decorator); // The program is the return decorator executed from the bottom up. run (orderId, supplierId, supOrderId );}







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.