Decorator pattern definition: Instead of adding or modifying class attribute actions through derived classes, this effect is achieved dynamically through pattern design. It is more convenient and flexible than inheritance and reduces the complexity of the program.
Example
Wang Feng builds a championship team.
First of all, the team category was empty. After Wang Feng’s continuous efforts, he won students for the team and created a suitable platform for team members to perform.
The team continues to become stronger and more complete because the decorators add and modify the base class according to different needs, and finally win your approval and meet your needs.
Class diagram to implement the assembler pattern:
Team formation code
//汪峰战队 abstract class WangFengTeam { //执行策划命令 abstract public void Acrion(); } //学员 class Student : WangFengTeam { // Methods override public void Acrion() { Console.WriteLine("汪峰团队学员情况:"); } } // 战队总策划 abstract class Direct : WangFengTeam { //汪峰战队 protected WangFengTeam team; // 策划活动 public void SetComponent(WangFengTeam team) { this.team = team; } //执行策划 override public void Acrion() { if (team != null) { team.Acrion(); } } } //男孩唱狂放型的,构建一个组合。 class BoyTeam : Direct { // 组合名称 public string teamName= "锋利的Jquery"; //具体策划 override public void Acrion() { base.Acrion(); Console.WriteLine("我是汪峰团队,狂放型的。"); } } //女孩唱婉约型的,来个模特表演 class GrilTeam : Direct { //具体策划 override public void Acrion() { base.Acrion(); Console.WriteLine("我是汪峰团队,婉约型的。"); //模特表演 show(); } public void show() { Console.WriteLine("婉约型,走秀"); } }
Client code:
public static void Main() { Student team = new Student(); BoyTeam boy = new BoyTeam(); GrilTeam girl = new GrilTeam(); //团队男孩装饰 boy.SetComponent(team); //团队女孩装饰 girl.SetComponent(boy); girl.Acrion(); Console.Read(); }
Advantages and disadvantages of the decorator pattern
After reading the detailed introduction of the decorator pattern, let's continue to analyze its advantages and disadvantages.
Advantages:
The purpose of the decoration pattern and inheritance is to extend the functionality of the object, but the decorator pattern is more flexible than inheritance
By using different concrete Decoration classes and the permutations and combinations of these classes, designers can create many combinations of different behaviors
The decorator pattern has good scalability
Disadvantages:
Decoration The operator pattern will lead to many small objects in the design, and if overused, the program will become more complex. And more objects make it harder to make mistakes, especially if they all look alike.
Usage Scenarios
Let’s take a look at the specific situations in which the decorator pattern is used. The decorator pattern should be used in the following situations:
Need to extend the functionality of a class or add to a class Additional Responsibilities.
Need to dynamically add functions to an object, and these functions can be dynamically revoked.
Need to add a very large number of functions generated by the permutation and combination of some basic functions
Implementation of the decorator pattern in .NET
There is also a decorator pattern in the .NET class library Implementation, this class is System.IO.Stream. Let’s take a look at the Stream class structure:
BufferedStream, CryptoStream and GZipStream are actually two specific decoration classes. The decorator here The pattern omits the abstract decorator role (Decorator). The following demonstrates how the client can dynamically add functions to MemoryStream.
MemoryStream memoryStream = new MemoryStream(new byte[] {95,96,97,98,99}); // 扩展缓冲的功能 BufferedStream buffStream = new BufferedStream(memoryStream); // 添加加密的功能 CryptoStream cryptoStream = new CryptoStream(memoryStream,new AesManaged().CreateEncryptor(),CryptoStreamMode.Write); // 添加压缩功能 GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true);
Summary
This is the end of the introduction to the decorator pattern. The decorator pattern uses object combination rather than inheritance to dynamically expand object functions at runtime. ability, and can expand multiple functions as needed, avoiding the "poor flexibility" and "multi-subclass derivation problems" caused by using inheritance alone. At the same time, it is well in line with the object-oriented design principles of "preferring object composition over inheritance" and the "open-closed" principle.
For more articles related to analyzing the decorator pattern in C# design pattern programming, please pay attention to the PHP Chinese website!