Analysis of decorator patterns in C # design pattern programming

Source: Internet
Author: User
Decorator mode definition: Instead of adding class attribute actions through derived classes, this effect is achieved dynamically through pattern design, and is more convenient and flexible than inheritance to reduce the complexity of the program.

Example

Wang Feng to build the champions team.

First, the team class is empty, after Wang Feng continuous efforts, for the team to win the students, but also for the team members to create a suitable platform for its play.

The team constantly become strong, become complete, is by the decorator, according to different needs, to the base class to increase the change, unanimously won your approval, to meet your needs.

To implement the class diagram for the assembler pattern:

Team Build Code

//Wang Feng Squad abstract class Wangfengteam {//Execute planning Command abstract public void Acrion ();}   Learner class Student:wangfengteam {//Methods override public void Acrion () {Console.WriteLine ("Wang Feng Team trainee Status:");    }}//Team always plan abstract class Direct:wangfengteam {//Wang Feng Team protected Wangfengteam team;   Planning activity public void SetComponent (Wangfengteam team) {this.team = team; }//execution plan override public void Acrion () {if (team! = null) {team.     Acrion (); }}}//The boy sings the wild type, building a combination.    Class Boyteam:direct {//combination name public string teamname= "sharp jquery"; Specific plan override public void Acrion () {base.     Acrion (); Console.WriteLine ("I am the Wang Feng team, the wild type.")   "); }}//Girls sing graceful, come to a model show class Grilteam:direct {//specifically plan override public void Acrion () {base.            Acrion (); Console.WriteLine ("I am a Wang Feng team, graceful type.")      ");   Model show Show ();   } public void Show () {Console.WriteLine ("Graceful type, catwalk"); } }

Client code:

public static void Main ()   {     Student team = new Student ();     Boyteam boy = new Boyteam ();     Grilteam girl = new Grilteam ();      Team Boys decorate Boy     . SetComponent (team);     Team Girl decorating     girl. SetComponent (boy);     Girl. Acrion ();     Console.read ();   }

The pros and cons of decorator mode
After reading the details of decorator mode, we continue to analyze its pros and cons.

Advantages:

Decoration This mode and inheritance are intended to extend the functionality of objects, but decorator mode is more flexible than inheritance

By using different decorative classes and permutations of these classes, designers can create combinations of many different behaviors.

Decorator mode is well scalable

Disadvantages:

Decorator mode causes many small objects to appear in the design, and if overused, it makes the program more complex. And more objects are going to make mistakes that are difficult, especially when these objects look alike.

Usage Scenarios
Let's look at what the decorator pattern is used in, and the decorator mode should be used in the following cases:
You need to extend the functionality of a class or add additional responsibilities to a class.
You need to dynamically add functionality to an object that can be revoked dynamically.
Need to increase the amount of functionality that arises from the combination of some basic functions


. The implementation of decorator pattern in net
There is also an implementation of the decorator pattern in the. NET class Library, which is System.IO.Stream, which looks at the Stream class structure:

BufferedStream, CryptoStream and GZipStream are actually two concrete decorations, where the decorator pattern omits the abstract decorative character (Decorator). The following shows how the client dynamically adds functionality to the MemoryStream dynamically.

MemoryStream MemoryStream = new MemoryStream (new byte[] {95,96,97,98,99});      Extended buffering function      BufferedStream buffstream = new BufferedStream (MemoryStream);      Added cryptographic features      CryptoStream cryptostream = new CryptoStream (memorystream,new aesmanaged (). CreateEncryptor (), cryptostreammode.write);      Add compression function      gzipstream gzipstream = new GZipStream (MemoryStream, compressionmode.compress, true);

Summarize
In this case, the decorator mode is finished, and the decorator pattern implements the ability to dynamically expand the object's functionality when it is run again, using object combinations rather than inheritance, and can extend multiple functions as needed to avoid the "poor flexibility" and "multi-subclass derivation problems" that are inherited by using inheritance alone. At the same time, it conforms well to the principle of object-oriented design, which is "preferential use of object combination rather than inheritance" and "open-closed".

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.