C#设计模式--装饰器模式

0.C#设计模式-简单工厂模式

1.C#设计模式--工厂方法模式

2.C#设计模式--抽象工厂模式

3.C#设计模式--单例模式

4.C#设计模式--建造者模式

5.C#设计模式--原型模式

6.C#设计模式--设配器模式

设计模式:

装饰器模式(Decorator Pattern)

简单介绍:

装饰器模式(Decorator Pattern):

假设有一部手机你购买来以后只有手机,然后你需要再给其添加一些装饰,比如保护壳,钢化膜或者手机贴画等,这个就是装饰者模式的思想

装饰器模式主要组成部分:

Component:定义一个对象接口,可以给这些对象动态地添加职责

ConcreteComponent:定义一个对象,可以给这个对象添加一些职责

Decorator:维持一个指向Component的指针,并定义一个与Component接口一致的接口

ConcreteDecorator:负责向ConcreteComponent添加功能

在装饰模式中,Decorator定义了一个装饰接口类。因为Decorator与ConcreteComponent继承同一个接口,所以继承Decorator的类ConcreteDecorator可以使用ConcreteComponent的方法,再在ConcreteDecorator里面加入一些新的方法,也就是装饰,就成为了一个包装好的装饰类。

装饰器模式类图:

装饰者模式C#代码举例:

Component 对象类

1     /// <summary>
2     /// 对象类
3     /// </summary>
4     public abstract class Component
5     {
6         public abstract void Print();
7     }

ConcreteComponenet 类 对象的修饰实现类

复制代码
 1     public class ConcreteComponent:Component
 2     {
 3         /// <summary>
 4         /// 对象修饰类
 5         /// </summary>
 6 
 7         public override void Print()
 8         {
 9             Console.WriteLine("我是ConcreteComponent!");
10         }
11     }
复制代码

Decorator类 装饰器类

复制代码
 1     /// <summary>
 2     /// 装饰器类
 3     /// </summary>
 4     public class Decorator:Component
 5     {
 6         protected Component _component;
 7 
 8         public void SetComponent(Component component)
 9         {
10             this._component = component;
11         }
12         public override void Print()
13         {
14             if (_component != null)
15             {
16                 _component.Print();
17             }
18         }
19     }
复制代码

ConcreteDecoratorA类 装饰子类A

复制代码
 1     /// <summary>
 2     /// 装饰子类A
 3     /// </summary>
 4     public class ConcreteDecoratorA: Decorator
 5     {
 6         public override void Print()
 7         {
 8             base.Print();
 9             Console.WriteLine("我是ConcreteDecoratorA!");
10         }
11     }
复制代码

ConcreteDecoratorB 装饰子类B

复制代码
 1     /// <summary>
 2     /// 装饰子类B
 3     /// </summary>
 4     public class ConcreteDecoratorB:Decorator
 5     {
 6         public override void Print()
 7         {
 8             base.Print();
 9             Console.WriteLine("我是ConcreteDecoratorB!");
10         }
11     }
复制代码

用户测试类:

复制代码
 1     class Client
 2     {
 3         static void Main(string[] args)
 4         {
 5             ConcreteComponent concreteComponent = new ConcreteComponent();
 6             ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA();
 7             ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB();
 8 
 9             concreteDecoratorA.SetComponent(concreteComponent);
10             concreteDecoratorA.Print();
11             Console.WriteLine("--------------------------------");
12             concreteDecoratorB.SetComponent(concreteDecoratorA);
13 
14             concreteDecoratorB.Print();
15             Console.Read();
16 
17         }
18     }
复制代码

运行结果:

源代码工程文件下载

装饰者模式实际生活举例

举例说明

假设有一部手机你购买来以后只有手机,然后你需要再给其添加一些装饰,比如保护壳,钢化膜或者手机贴画等

mobilephone 原手机接口

1     /// <summary>
2     /// 手机接口
3     /// </summary>
4     public abstract class MobilePhone
5     {
6         public abstract void Print();
7     }

AppleMobilePhone类 手机的修饰类

复制代码
 1     /// <summary>
 2     /// 手机的修饰
 3     /// </summary>
 4     public class AppleMobilePhone :MobilePhone
 5     {
 6         public override void Print()
 7         {
 8             Console.WriteLine("我是一部苹果手机!");
 9         }
10     }
复制代码

Decorator类 装饰器类

复制代码
 1     /// <summary>
 2     /// 装饰器
 3     /// </summary>
 4     public class Decorator:MobilePhone
 5     {
 6         protected MobilePhone mobliePhone;
 7         public void SetMobilePhone(MobilePhone phone)
 8         {
 9             this.mobliePhone = phone;
10         }
11 
12         public override void Print()
13         {
14             if (mobliePhone != null)
15             {
16                 mobliePhone.Print();
17             }
18         }
19     }
复制代码

MobilePhoneShell类 手机壳修饰类

复制代码
 1     /// <summary>
 2     /// 手机壳修饰类
 3     /// </summary>
 4     public class MobilePhoneShell:Decorator
 5     {
 6         public override void Print()
 7         {
 8             base.Print();
 9             Console.WriteLine("我是一个手机保护壳!");
10         }
11     }
复制代码

MobilePhoneStickers类 手机贴画修饰类

复制代码
 1     /// <summary>
 2     /// 手机贴画修饰类
 3     /// </summary>
 4     public class MobilePhoneStickers:Decorator
 5     {
 6         public override void Print()
 7         {
 8             base.Print();
 9             Console.WriteLine("我是一个手机贴画!");
10         }
11     }
复制代码

用户测试类

复制代码
 1     class Client
 2     {
 3         static void Main(string[] args)
 4         {
 5             Console.WriteLine("----------------手机-----------------");
 6             AppleMobilePhone appleMobilePhone = new AppleMobilePhone();
 7             appleMobilePhone.Print();
 8 
 9             Console.WriteLine("---------加了手机壳的手机----------------");
10             MobilePhoneShell mobilePhoneShell = new MobilePhoneShell();
11             mobilePhoneShell.SetMobilePhone(appleMobilePhone);
12             mobilePhoneShell.Print();
13 
14             Console.WriteLine("----------加了手机贴画的手机---------------");
15             MobilePhoneStickers mobilePhoneStickers = new MobilePhoneStickers();
16             mobilePhoneStickers.SetMobilePhone(appleMobilePhone);
17             mobilePhoneStickers.Print();
18             Console.WriteLine("----------既加了手机壳又加了手机贴画的手机--------");
19 
20             mobilePhoneStickers.SetMobilePhone(mobilePhoneShell);
21             mobilePhoneStickers.Print();
22 
23             Console.Read();
24 
25         }
26     }
复制代码

运行结果

源代码工程文件下载地址

posted @   JiYF  阅读(4361)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示