C#设计模式之装饰模式(Decorator Pattern)
来源:互联网 发布:手机用数据会出400bad 编辑:程序博客网 时间:2024/05/01 23:59
装饰模式解释:
装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
特点:
1. 装饰对象和真实对象有相同的接口。这样客户端对象就能以和真实对象相同的方式和装饰对象交互。
2. 装饰对象包含一个真实对象的引用(reference)
3. 装饰对象接受所有来自客户端的请求。它把这些请求转发给真实的对象。
4. 装饰对象可以在转发这些请求以前或以后增加一些附加功能。这样就确保了在运行时,不用修改给定对象的结构就可以在外部增加附加的功能。在面向对象的设计中,通常是通过继承来实现对给定类的功能扩展。
使用时机:
1. 需要扩展一个类的功能,或给一个类添加附加职责。
2. 需要动态的给一个对象添加功能,这些功能可以再动态的撤销。
3. 需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变的不现实。
4. 当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。
在装饰模式中的各个角色有:
1. 抽象构件(Component)角色:给出一个抽象接口,以规范准备接收附加责任的对象。
2. 具体构件(Concrete Component)角色:定义一个将要接收附加责任的类。
3. 装饰(Decorator)角色:持有一个构件(Component)对象的实例,并实现一个与抽象构件接口一致的接口。
4. 具体装饰(Concrete Decorator)角色:负责给构件对象添加上附加的责任。
UML图:
代码实现:
Component抽象类:
abstract class Component{ public abstract void Operation();}
class ConcreteComponent : Component{ public override void Operation() { Console.WriteLine("具体对象的操作"); }}
abstract class Decorator : Component{ protected Component component; public void SetComponent(Component component) { this.component = component; } public override void Operation() { if (component != null) { component.Operation(); //实际执行的是原Component的Operation(). } }}
class ConcreteDecoratorA : Decorator{ private string addedState; //<span style="font-family: Arial, Helvetica, sans-serif;">本类才有的属性</span> public override void Operation() { base.Operation(); //<span style="font-family: Arial, Helvetica, sans-serif;">先执行原Component的Operation(),再执行本类的功能.如 addedState,相当于对原类进行了装饰</span> addedState = "New State"; Console.WriteLine("具体装饰对象A的操作"); }}class ConcreteDecoratorB : Decorator{ public override void Operation() { base.Operation(); //先执行原Component的Operation(),再执行本类的功能.如 AddedBehavior(),相当于对原类进行了装饰 AddedBehavior(); //<span style="font-family: Arial, Helvetica, sans-serif;">本类才有的方法</span> Console.WriteLine("具体装饰对象B的操作"); } private void AddedBehavior() {<span style="white-space:pre"></span>Console.WriteLine("新增加的行为"); }}
class Program{ static void Main(string[] args) { ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); //装饰的方法是:先用ConcreteComponent实例化对象c, //然后用ConcreteComponentA的对象d1来包装c, //然后用ConcreteComponentB的对象d2来包装d1, //最后执行d2的Operation(). d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); Console.Read(); }}
1. 如果只有一个Concrete Component类而没有抽象的Component接口时,可以让Decorator继承Concrete Component。
2. 如果只有一个Concrete Decorator类时,可以将Decorator和Concrete Decorator合并。
- C#设计模式之装饰者模式(Decorator Pattern)
- C#设计模式之装饰模式(Decorator Pattern)
- 设计模式之装饰模式(Decorator Pattern)
- 设计模式之装饰模式--- Pattern Decorator
- 设计模式系列之四:装饰者模式(Decorator Pattern)
- 设计模式笔记之装饰者模式(Decorator Pattern)
- 设计模式之装饰者模式Decorator Pattern
- 设计模式(二)之装饰模式(Decorator Pattern)
- JAVA设计模式之 装饰模式【Decorator Pattern】
- 设计模式(结构型)之装饰者模式(Decorator Pattern)
- 设计模式总结之Decorator Pattern(装饰者模式)
- Java设计模式之装饰者模式(Decorator pattern)
- Android设计模式之装饰者模式(Decorator Pattern)
- 设计模式[6] Decorator Pattern 装饰模式
- 设计模式-装饰者模式(Decorator Pattern)
- 设计模式-装饰模式(Decorator Pattern)
- 设计模式心得:装饰模式 (decorator pattern)
- 设计模式-装饰模式(Decorator Pattern)
- Android 粒子效果
- ThreadPoolExecutor的原理及使用方法
- spoj 375 Query on a tree(树链剖分,边,线段树)
- Esper 视图 Views窗口语法
- 【HDU】5789 Permutation【dp】
- C#设计模式之装饰模式(Decorator Pattern)
- 【Python错误】windows下使用pip/easy_install提示Fatal error in launcher: Unable to create process using...
- 顺序表应用7:最大子段和之分治递归法
- Java中常用的线程池介绍
- Split()---"." "|" "*" "\" "]"
- ggplot的使用
- Java NIO的简单介绍
- 【HDU】5786 Interval【分类讨论+容斥】
- 文章标题