重看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:/ ...
随机推荐
- 绕过图片格式限制上传木马获取WebShell
思路: 图片上传功能中,前端页面上传的是.png格式的图片文件,但是抓包Request中修改图片后缀为.php 可以绕过对上传文件格式的限制,文件的上传路径可以在上传后的页面或查看上传成功后的resp ...
- JS复制对象
CSSCommonJS.DeepCopy = function (json) { if (typeof json == 'number' || typeof json == 'string' || t ...
- C++重载输入和输出操作符以及IO标准库中的刷新输入缓冲区残留字符问题
今天在做C++ Primer习题的14.11时,印象中应该挺简单的一题,结果却费了很长时间. 类定义: typedef string Date; class CheckoutRecord{ publi ...
- How to download apk for google play online?
http://apps.evozi.com/apk-downloader/ Online APK Downloader http://apkpure.com/ apkpure http://www.c ...
- How to learn C++ and find all STL Algorithm reference
You can find all cpp references on websites: http://zh.cppreference.com/ http://www.cplusplus.com/re ...
- Hibernate个人总结
编写Hibernate第一个程序 Hibernate是目前最流行的持久层框架,专注于数据库操作.使用Hibernate框架能够使开发人员从繁琐的SQL语句和复杂的JDBC中解脱出来.Hibernate ...
- sizeof(结构体)的计算
摘要: 经常被计算结构体的sizeof给搞晕,于是找了个时间,静下心来,搞定它. 一.为什么结构体计算这么乱? 答案是字节对齐,计算机存储系统中以Byte为单位存储数据,不同数据类型所占的空间不同,如 ...
- maven打包源代码sources.jar和javadoc.jar帮助文档
maven中如何打包源代码 *-sources.jar 方式一 : 命令行方式 进入cmd命令行,进入项目工程pom.xml所在路径目录,运行mvn source:jar 和 mvn javado ...
- 斜率优化dp(POJ1180 Uva1451)
学这个斜率优化dp却找到这个真心容易出错的题目,其中要从n倒过来到1的确实没有想到,另外斜率优化dp的算法一开始看网上各种大牛博客自以为懂了,最后才发现是错了. 不过觉得看那些博客中都是用文字来描述, ...
- 对于requirejs AMD模块加载的理解
个人人为使用模块化加载的优点有三: 1,以我的项目为例:90%为图表展示,使用的是echarts,此文件较大,requirejs可以在同一个版本号(urlArgs)之下缓存文件,那么我就可以在访问登陆 ...