C#设计模式之装饰者模式(Decorator Pattern)
1.概述
装饰者模式,英文名叫做Decorator Pattern。装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。
2.特点

/// <summary>
/// 定义Component对象接口
/// </summary>
public abstract class Component
{
public abstract void Operation();//一个抽象的职责
}
/// <summary>
/// 具体对象
/// </summary>
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();
}
}
} class ConcreteDecoratorA : Decorator
{ public override void Operation()
{ base.Operation(); //首先运行原Compnent的Operation(),再执行本类的功能,如AddedBehavior,相当于对原Component进行了装饰 Console.WriteLine("具体装饰对象A的操作");
}
}
class ConcreteDecoratorB : Decorator
{ public override void Operation()
{ base.Operation(); //首先运行原Compnent的Operation(),再执行本类的功能,如AddedBehavior,相当于对原Component进行了装饰 Console.WriteLine("具体装饰对象B的操作");
}
}
(2)无抽象接口
public class Car
{
public virtual void Description()
{
Console.Write("基本");
}
} public class ESPDecorator : Car
{
Car car;
public ESPDecorator(Car car)
{
this.car = car;
}
public override void Description()
{
car.Description();
Console.WriteLine("带有ESP功能");
}
}
public class OtherDecorator : Car
{
Car car;
public OtherDecorator(Car car)
{
this.car = car;
}
public override void Description()
{
car.Description();
Console.WriteLine("带有其它功能");
}
}
代码调用
//第一种
ConcreteComponent c = new ConcreteComponent();
ConcreteDecoratorA d1 = new ConcreteDecoratorA();
d1.SetComponent(c);
ConcreteDecoratorB d2 = new ConcreteDecoratorB();
d2.SetComponent(c);
d2.Operation();
//第二种
Car car = new ESPDecorator(new OtherDecorator(new Car()));
car.Description();
Console.Read();
C#设计模式之装饰者模式(Decorator Pattern)的更多相关文章
- 设计模式学习--装饰者模式(Decorator Pattern)
概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...
- python 设计模式之装饰器模式 Decorator Pattern
#写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...
- 23种设计模式之装饰器模式(Decorator Pattern)
装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...
- c#设计模式之装饰器模式(Decorator Pattern)
引子 在面向对象语言中,我们常常会听到这样一句话:组合优于继承.那么该如何去理解这句话呢? 下面我将以游戏装备为模型用简单的代码去展示它 先创建一个装备的抽象类,然后创建刀枪2个具体的业务子类 pub ...
- 【UE4 设计模式】装饰器模式 Decorator Pattern
概述 描述 动态地给一个对象增加一些额外的职责(Responsibility),就增加对象功能来说,装饰模式比生成子类实现更为灵活.是一种对象结构型模式. 套路 抽象构件(Component) 具体构 ...
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法
装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...
- 设计模式 - 装饰者模式(Decorator Pattern) 具体解释
装饰者模式(Decorator Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033 装饰者 ...
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...
随机推荐
- KVM: 安装Windows virtio半虚拟化驱动
Install KVM Windows virtio para-virtualized dirver If you can't read Chinese, there's an English ver ...
- Objective-C的基础语法总结
1.NSLog(@“hello world!”);//打印语句的函数,需要打印的字符串放在@之后. NSLog(@“are %d and %d different?%@”,4,4,@”YES”); 2 ...
- vim的.vimrc文件设置
set nocompatibleset autowriteset autoreadset nobackupset noswapfile " --- syntax and indent --- ...
- MITM to crack Https connections
Everybody knows that https is http over SSL, and https is a secure way for protecting confidential d ...
- CSS 居中效果完整指南
本文翻译自:<Centering in CSS: A Complete Guide> 使用 CSS 实现效果困难吗?显然不是.实际上有许多方法可以实现居中效果,但在具体情况中,我们往往无法 ...
- 一款安卓ShowcaseView视图源码效果
该源码是从源码天堂那边转载过来的,大家可以看看一下吧啊,一款安卓ShowcaseView视图源码效果,非常不错的,特别是在做引导时使用. 源码下载地址:http://code.662p.com/vie ...
- css3内处理
1.插入文字:content属性: p:after{content:"内容"} p:before{content:"内容"} ...
- ie使用firebug
在网页插入以下代码即可. <script type="text/javascript" src="http://getfirebug.com/releases/li ...
- POJ C程序设计进阶 编程题#3:寻找山顶
编程题#3:寻找山顶 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 在一个 ...
- VS2008调试快捷键
F5: 启动调试 Ctrl+F5: 开始执行(不调试) F10: 逐过程(不进入函数单步) F11: 逐语句(进入函数单步) Shift+F11跳出(实用) Ctrl+F10: 运行到光标处 F6: ...