. NET Decorating mode explained

Source: Internet
Author: User
Definition of decoration Mode:

Decorating mode is the ability to dynamically extend an object without having to change the original class file and use inheritance. It is by creating a wrapper object, that is, decorating to wrap the real object.

Decorator Mode structure diagram:

Decorator Mode role:

(1) abstract component (Component) role: An abstract interface is given to standardize the objects that are ready to receive additional responsibilities.
(2) Specific component (concrete Component) Role: Defines a class that will receive additional responsibilities.
(3) decoration (Decorator) Role: Holds an instance of a component (Component) object and implements an interface that is consistent with the abstract component interface.
(4) Specific decoration (concrete Decorator) role: Responsible for adding additional responsibilities to the component objects.

Realize:

Citing life examples, we bought a new phone, slowly add new decorations to the phone, such as foil, add pendants .....

1. First abstract the phone class as an abstract component in the decorator pattern:

<summary>///Mobile abstract class, abstract component class in abstract mode///</summary>public abstract class phone{//  <summary>  ///Printing method  ///</summary> public abstract void print ();}

2. If we now buy an Apple phone, now inherit from the abstract class, implementing the specific component class:

<summary>///Apple phone, which is decorated with specific component classes in the mode///</summary>public class applephone:phone{//  <summary>  /////Override the base class method///  </summary> public  override void Print ()  {    Console.WriteLine ("I have an apple phone");  }

3. Now I want to add a new decoration to this phone, we first abstract out the decoration class:

<summary>///decoration abstract class, let adornment completely replace abstract component, so must inherit phone///</summary>public abstract class decorator:phone{  Private Phone p;  The decoration object is decorated to the phone component entity object public     Decorator (phone p)  {    this.p = p;  }   public override void Print ()  {    if (THIS.P! = null)    {      p.print ()    ;  }}}

4. Specific decorative objects, inheriting decorative abstract class: Here specific foil decoration, and pendant decoration:

<summary>///foil, concrete decorator///</summary>public class sticker:decorator{public  Sticker (Phone p): Base (P {} public   override void Print ()  {    base. Print ();    Add Behavior    addsticker ();  }   <summary>  ////new behavior method///</summary> public void Addsticker ()  {    Console.WriteLine ("Now the iphone has a foil");}  }
<summary>///phone pendant, that is, the specific decorator///</summary>public class accessories:decorator{public  accessories ( Phone p): Base (p) {} public   override void Print ()  {    base. Print ();     Add a new behavior    addaccessories ();  }   <summary>  ////new behavior method///</summary> public void addaccessories ()  {    Console.WriteLine ("Now the iphone has a pretty pendant");}  }

5. Call:

<summary>///design mode-Decorator mode/  //</summary>class program{static void Main (string[] args)  {    Phone ap = new Applephone (); Newly bought an apple phone    Decorator aps = new Sticker (AP);//Preparing the Foil module    APS. Print ();     Decorator APA = new Accessories (AP); A few days later added the pendant component    APA. Print ();     Sticker s = new Sticker (AP);    Prepare the foil pack    Accessories A = new accessories (s);//Prepare the pendant    a.print ();  }}

(1) abstract component (Component) role: An abstract interface is given to standardize the objects that are ready to receive additional responsibilities. > here is the phone interface
(2) Specific component (concrete Component) Role: Defines a class that will receive additional responsibilities. > here refers to the Applephone
(3) decoration (Decorator) Role: Holds an instance of a component (Component) object and implements an interface that is consistent with the abstract component interface. > refers to the decorator
(4) Specific decoration (concrete Decorator) role: Responsible for adding additional responsibilities to the component objects. > refers to Accessories and sticker

Advantages:

1. The purpose of the decorator pattern and the inheritance relationship is to extend the functionality of the object, but decorator can provide more flexibility than inheritance.
2. Designers can create a combination of many different behaviors by using different decorative classes and arranging combinations of these decorations.

Disadvantages:

1. This is more flexible than inheritance, but it also means more complexity.
2. Decorating mode causes many small classes to appear in the design, which can complicate the program if overused.
3. The adornment mode is programmed for the abstract component (Component) type. However, if you are programming for a specific component, you should rethink your application architecture and whether the decorator is appropriate. Of course, you can change the component interface, add new public behavior, and realize the "translucent" decorator pattern. Make the best choice in the actual project.

The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.

  • 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.