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 }