重看Decorator Pattern,联想到Delegate传递及Flags Enum--欢迎拍砖!
话说装饰模式(Decorator)的动机是“动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。[GOF 《设计模式》]”。再次学到该模式,有感,联想到Delegate和Flags Enum。Delegate也可实现在已有功能上动态添加新功能,有点”装饰“的意图,Flags Enum可以进行组合使用。如果对装饰模式不熟悉,请移步大神博文http://terrylee.cnblogs.com/archive/2006/03/01/340592.html。本文描述该模式的相关思考,不正之处,请大神指点拍砖!谢谢
该模式的UML图:
,Decorator抽象,既继承Component又组合引用Component,为什么需要这样呢?从该模式的具体代码看到,装饰之上可以继续装饰,故需要引用一个装饰,为什么有需要继承呢,感觉装饰需要一个抽象吧。
现在来假设一个场景,有一个现有的对象,如下:
public class Component
{
public void Operation(string msg)
{
Console.WriteLine("Compent.Operation。hi,:"+msg);
}
}
该操作不是可override的,那怎么使用装饰模式来扩展新功能呢?这里给出装饰模式的“变形代码”
public class ComponentWrapper
{
public virtual void Operation(string msg)
{
Component m_Component = new Component();
m_Component.Operation(msg);
}
} public class ComponentWrapper1 : ComponentWrapper
{
private ComponentWrapper m_decorator;
public ComponentWrapper1(ComponentWrapper decorator)
{
m_decorator = decorator;
}
public override void Operation(string msg)
{
m_decorator.Operation(msg);
Add(msg);
}
public static void Add(string msg)
{
Console.WriteLine("ComponentWrapper1 added");
}
} public class ComponentWrapper2 : ComponentWrapper
{
private ComponentWrapper m_decorator;
public ComponentWrapper2(ComponentWrapper decorator)
{
m_decorator = decorator;
}
public override void Operation(string msg)
{
m_decorator.Operation(msg);
Add(msg);
}
public static void Add(string msg)
{
Console.WriteLine("ComponentWrapper2 added");
}
}
//调用代码
ComponentWrapper d = new ComponentWrapper();//未装饰
ComponentWrapper d1 = new ComponentWrapper1(d);//装饰上功能1
ComponentWrapper d2 = new ComponentWrapper2(d1);//装饰上功能2 d2.Operation("stevey");
上面代码中,ComponentWrapper 作为装饰的基类,对已有功能进行包装,在装饰1中继承装饰基类又包含一个装饰。可能说包装感觉更形象一点,一层一层的包装,或者说人穿的衣服也是一层一层的,哈哈。可以看到,一个装饰对象就作为一个”功能集“整体,实际上是一个引用装饰链,调用依次传递到顶端。由此联想到,功能也可以追加,功能也可以做成像链式依次执传递。遵循上面场景的方法契约,于是就凭着感觉写出如下code:
public static class ComponentExtension
{
/// <summary>
/// 在已有操作之后加上新操作
/// </summary>
/// <param name="action">原操作</param>
/// <param name="otherAction">新操作</param>
/// <returns></returns>
public static Action<string> After(this Action<string> action,Action<string> otherAction)
{
return (msg) => {
action(msg);
otherAction(msg);
};
} public static Action<string> Before(this Action<string> action, Action<string> otherAction)
{
return (msg) =>
{
otherAction(msg);
action(msg);
};
}
}
调用代码:
//使用delegate装饰新功能
Action<string> action = new Component().Operation;
Action<string> wrapper1 = action.After(ComponentWrapper1.Add);//装饰上功能1
Action<string> wrapper2 = wrapper1.After((msg) =>//装饰上功能2
{
Console.WriteLine("wrapper2 added");
});
wrapper2("hello");
Console.WriteLine("*****************************");
//
Action<string> wrapper3 = wrapper1.After(wrapper2);
wrapper3("(原功能+功能1)+{(原功能+功能1)+装饰上功能2}");//在已有的装饰整体上加上另一个装饰
上面的代码可以链式,其他不解释了,代码是最直接的意思表达,在一个功能上继续包装一个功能,得到的就是一个装饰,可以作为整体,继续装饰。。。貌似比模式轻量级点不。
标记枚举,也有点”装饰“的味道,MSDN上的代码:
[Flags]
enum Days2
{
None = 0x0,
Sunday = 0x1,
Monday = 0x2,
Tuesday = 0x4,
Wednesday = 0x8,
Thursday = 0x10,
Friday = 0x20,
Saturday = 0x40
}
//Flags Enum
// Initialize with two flags using bitwise OR.
var meetingDays = Days2.Tuesday | Days2.Thursday; // Set an additional flag using bitwise OR.
meetingDays = meetingDays | Days2.Friday; Console.WriteLine("Meeting days are {0}", meetingDays);
// Output: Meeting days are Tuesday, Thursday, Friday // Remove a flag using bitwise XOR.
meetingDays = meetingDays ^ Days2.Tuesday;
Console.WriteLine("Meeting days are {0}", meetingDays);
// Output: Meeting days are Thursday, Friday
就写到这里吧,感觉有点语无伦次,不在状态,大家就将就看吧。算是一点装饰模式的读后感,欢迎大家讨论,不正之处,还请指出,谢谢!
重看Decorator Pattern,联想到Delegate传递及Flags Enum--欢迎拍砖!的更多相关文章
- 第 13 章 装饰模式【Decorator Pattern】
以下内容出自:<<24种设计模式介绍与6大设计原则>> Ladies and gentlemen,May I get your attention,Please?,Now I’ ...
- C#设计模式之八装饰模式(Decorator Pattern)【结构型】
一.引言 今天我们要讲[结构型]设计模式的第三个模式,该模式是[装饰模式],英文名称:Decorator Pattern.我第一次看到这个名称想到的是另外一个词语“装修”,我就说说我对“装修”的理解吧 ...
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- .NET设计模式(10):装饰模式(Decorator Pattern)
.NET设计模式(10):装饰模式(Decorator Pattern) 装饰模式(Decorator Pattern) --.NET设计模式系列之十 年月..在....对于..由于使用装饰模 ...
- 设计模式系列之装饰模式(Decorator Pattern)
装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装.这种模式创建了一个装饰类,用来包装原 ...
- NET设计模式 第二部分 结构性模式(9):装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) ——.NET设计模式系列之十 Terrylee,2006年3月 概述 在软件系统中,有时候我们会使用继承来扩展对象的功能,但是由于继承为类型引入的静态特 ...
- 设计模式学习--装饰者模式(Decorator Pattern)
概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...
- 设计模式系列之装饰模式(Decorator Pattern)——扩展系统功能
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
随机推荐
- Linux 套接字编程中的 5 个隐患
http://www.ibm.com/developerworks/cn/linux/l-sockpit/ 在 4.2 BSD UNIX® 操作系统中首次引入,Sockets API 现在是任何操作系 ...
- 172. Factorial Trailing Zeroes
题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- 【Web】CDN加速效果浅析
1. 什么是CDN? CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的CACHE(缓存)层,将网站的内容发布到最接近用 ...
- fedora之防火墙
通过iptables的设置可以学一些东西 从配置菜单关闭防火墙是不起作用的,索性在安装的时候就不要装防火墙查看防火墙状态:/etc/init.d/iptables status暂时关闭防火墙:/etc ...
- 各种不会。。。。编译和安装hadoop过程中好多命令和工具不会
http://blog.csdn.net/bamuta/article/details/13506893 64位下解决方法(重新编译~~) http://zhidao.baidu.com/link? ...
- C++内存中的封装、继承、多态(下)
上篇讲述了内存中的封装模型,下篇我们讲述一下继承和多态. 二.继承与多态情况下的内存布局 由于继承下的内存布局以及构造过程很多书籍都讲得比较详细,所以这里不细讲.重点讲多态. 继承有以下这几种情况: ...
- C# Process.Kill() 拒绝访问(Access Denied) 的解决方案
需求:很多时候我们需要后台运行几个Console来不停的计算数据,那么部署到客户服务器后,如果出现突发异常,程序挂掉了,那...? 解决方案:封装了一个对后台运行程序不停监测的功能,如果发现程序有异常 ...
- UVa 140 (枚举排列) Bandwidth
题意较复杂,请参见原题=_=|| 没什么好说的,直接枚举每个排列就好了,然后记录最小带宽,以及对应的最佳排列. STL里的next_permutation函数真是好用. 比较蛋疼的就是题目的输入了.. ...
- Arch 常用工具
一.网络浏览 pacman -S firefox firefox-i18n注:该命令中的前者为 Firefox 主程序,后者为语言包.pacman -S opera 二.图像编辑 pacman -S ...
- String的intern方法的用处
今天第一次翻看Effective java,在其第一个item中讲静态工厂方法的有点的时候说到“它们每次被调用 的时候,不要非得创建一个新的对象”并在结尾处提到---"String.inte ...