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).在“装饰模式”中很好的提现了开放 ...
随机推荐
- Hibernate 问题,在执行Query session.createQuery(hql) 报错误
在配置文件中加入 <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classi ...
- 跨域请求之JSONP 一
跨域请求之JSONP 一 跨域请求的方式有很多种, iframe document.domain window.name script XDomainRequest (IE8+) XMLHTTPReq ...
- JS常用的设计模式(9)——策略模式
策略模式的意义是定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.一个小例子就能让我们一目了然. 回忆下jquery里的animate方法. $( div ).animate( {&quo ...
- linux 下安装 搭建 svn服务器
1.下载svn http://subversion.apache.org/download 下载完成后解压,执行 ./configure --prefix=/usr/svn 提示 configure: ...
- MITM to crack Https connections
Everybody knows that https is http over SSL, and https is a secure way for protecting confidential d ...
- WinForm中使用AnyCAD三维控件 の 初始化
在WinForm中可以方便的集成AnyCAD.Net三维控件,只需要以下几部即可完成. 一.添加DLL程序集 AnyCAD.Foundation.Net.dll AnyCAD.Presentation ...
- 设计模式-原型模式(Prototype)
场景分析: 前面我们提到,交易对象Trade,还有继承他的债券交易BondTrade.期货交易FutureTrade. 现在有一个需求,需要提供方法将交易拆分成多笔小交易. 代码如下(如果没有clon ...
- 必须会的SQL语句(二) 创建表、修改表结构、删除表
1.创建数据库表 --使用哪个数据库,如果不写这一句是默认的数据库,也可以用鼠标选当前数据库 use testDB --创建表 Create Table tablename ( ...
- 《cocos2d-x游戏开发》—— lua学习总结(一)数组的使用
在lua中,数组是用table来实现的. 1.数组的定义: self.itemArrays = {}; --作为数组来使用的表itemArrays 2. 数组插入一条数据: local showIte ...
- ASP.NET常用代码汇总
1. 打开新的窗口并传送参数: 传送参数:response.write("<script>window.open('*.aspx?id="+this.DropDownL ...