随笔:63 文章:1 评论:34 引用:1
Cameo Chen
Ready for a new day
博客园
首页
发新随笔
发新文章
联系
订阅
管理
10. Decorator 装饰(结构型模式)
动机(Motivation)
上述描述的问题根源在于我们“
过度地使用了继承来扩展对象的功能
”,
由于继承为类型引入的静态特质
,使得这种扩展方式缺乏灵活性;并且随着子类的增多(扩展功能的增多),
各种子类的组合(扩展功能的组合)会导致更多子类的膨胀(多继承)。
如何使“对象功能的扩展”能够根据需要来动态地实现?同时避免“扩展功能的增多”带来的子类膨胀问题?从而使得任何“功能扩展变化”所导致的影响将为最低?
意图(Intent)
动态地给一个对象
增加一些额外的职责
。
就增加功能而言,
Decorator模式比生成子类更为灵活。
——《设计模式》GoF
类的继承是实现继承(IS-a 的关系),而接口的继续是合同的继承
(DO As [ or do like]行为上是什么样).
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
Decorator
{
Main Class
#region
Main Class
public
abstract
class
Tank
{
public
abstract
void
Shot();
public
abstract
void
Run();
}
public
class
T50 : Tank
{
public
override
void
Shot()
{
}
public
override
void
Run()
{
}
}
#endregion
Decorator
#region
Decorator
public
abstract
class
Decorator : Tank
{
private
Tank tank;
public
Decorator(Tank tank)
{
this
.tank
=
tank;
}
public
override
void
Shot()
{
tank.Shot();
}
public
override
void
Run()
{
tank.Run();
}
}
public
class
DecoratorA :Decorator
{
public
DecoratorA(Tank tank)
:
base
(tank)
{
}
public
override
void
Shot()
{
//
红外功能扩展
//
do shot..
base
.Shot();
}
public
override
void
Run()
{
//
红外功能扩展
//
do run..
base
.Run();
}
}
public
class
DecoratorB : Decorator
{
public
DecoratorB(Tank tank)
:
base
(tank)
{
}
public
override
void
Shot()
{
//
卫星定位功能扩展
//
do shot
base
.Shot();
}
public
override
void
Run()
{
//
卫星定位功能扩展
//
do run
base
.Run();
}
}
public
class
DecoratorC : Decorator
{
public
DecoratorC(Tank tank)
:
base
(tank)
{
}
public
override
void
Shot()
{
//
两栖功能扩展
base
.Shot();
}
public
override
void
Run()
{
//
两栖功能扩展
base
.Run();
}
}
#endregion
Client Application
#region
Client Application
class
Program
{
static
void
Main(
string
[] args)
{
Tank tank
=
new
T50();
//
Decorator模式实现了在运行时动态地扩展对象功能的能力
DecoratorA da
=
new
DecoratorA(tank);
//
红外
DecoratorB db
=
new
DecoratorB(da);
//
红外,卫星
DecoratorC dc
=
new
DecoratorC(db);
//
红外,卫星,两栖
}
}
#endregion
}
Decorator模式的几个要点
• 通过采用组合、而非继承的手法, Decorator模式实现了
在运行时动态地扩展对象功能的能力
,而且可以根据需要
扩展多个功能
。避免了单独使用继承带来的“灵活性差”和“多子类衍生问题”。
• Component类在Decorator模式中
充当抽象接口的角色
,不应该去实现具体的行为。
而且Decorator类对于Component类应该透明
——换言之Component类无需知道Decorator类,
Decorator类是从外部来扩展Component类的功能
。
• Decorator类在接口上表现为is-a Component的继承关系,即Decorator类继承了Component类所具有的接口。但在实现上又表现为has-a Component的组合关系,即Decorator类又使用了另外一个Component类。我们可以使用一个或者多个Decorator对象来“装饰”一个Component对象,且装饰后的对象仍然是一个Component对象。
• Decorator模式并
非解决“多子类衍生的多继承”问题
,Decorator模式应用的要点在于解决“
主体类在多个方向上的扩展功能
”——是为“装饰”的含义。
.NET框架中的Decorator应用
using
System.IO;
using
System.Security.Cryptography;
namespace
NetDecorator
{
class
Program
{
static
void
Main(
string
[] args)
{
MemoryStream ms
=
new
MemoryStream(
new
byte
[]
{
100
,
203
,
102
,
52
,
34
,
187
}
);
//
主体类
//
装饰类
BufferedStream buff
=
new
BufferedStream(ms);
//
扩展缓冲功能
CryptoStream crypto
=
new
CryptoStream(buff);
//
扩展缓冲,加密功能
}
}
}
发表于 2008-07-04 13:38
Cameo
阅读(56)
评论(0)
编辑
收藏
网摘
所属分类:
设计模式
图书专题
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
人才
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2008-07-15 20:32 编辑过
Google站内搜索
[推荐职位]上海盛大网络招聘架构师
China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》
相关文章:
相关链接:
所属分类的其他文章:
15. Command 命令(行为型模式)
14. Template Method模板方法(行为型模式)
13. Proxy代理(结构型模式)
12. Flyweight享元(结构型模式)
11. Facade外观[门面模式](结构型模式)
10. Decorator 装饰(结构型模式)
9. Composite 组合(结构型模式)
8. Bridge 桥接(结构型模式)
7. Adapter 适配器(结构型模式)
6. Prototype 原型(创建型模式)
最新IT新闻:
Google浏览器Chrome 2.0测试版发布
微软中国被指变相裁员 微软称确有过人事调整
“新一代互联网”研究:中国走在世界前列
苏宁首战告捷 销售同比增六成
福布斯:虚拟化技术登陆Mac平台
CALENDER
<
2008年7月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
公告
网名:
Cameo Chen
位置:
中国 上海
MSN:
xinxiu_chen@hotmail.com
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
(2)
给我留言
查看留言
我管理的小组
VSTO技术组
我参加的小组
设计模式
我参与的团队
VSTO开发团队(0/12)
随笔分类
asp.net 2.0(22)
(rss)
Castle
(rss)
CLR(4)
(rss)
Div+Css(5)
(rss)
Enterprise Library(1)
(rss)
Nbear
(rss)
Nhibernate.Net
(rss)
SQL(8)
(rss)
SSIS
(rss)
UML(1)
(rss)
VSTO(2)
(rss)
WorkFlow(2)
(rss)
程序人生
(rss)
设计模式(15)
(rss)
书籍收藏(1)
(rss)
随笔档案
2008年12月 (1)
2008年10月 (3)
2008年9月 (1)
2008年8月 (1)
2008年7月 (6)
2008年6月 (8)
2008年5月 (2)
2008年2月 (1)
2008年1月 (5)
2007年12月 (1)
2007年11月 (2)
2007年8月 (1)
2007年7月 (1)
2007年6月 (2)
2007年4月 (1)
2007年3月 (1)
2007年2月 (3)
2007年1月 (13)
2006年12月 (10)
常用链接
asp.net
Codeplex
Codeproject
Csharp-source
MSDN WebCast
TechNet
Terrylee
最新评论
1. re: VSTO开发团队成立
我也正在學習VSTO和VBA,多多交流啊!
--絮
2. re: VSTO开发团队成立
申请加入,学习/交流!
--菌哥
3. re: VSTO开发团队成立
留下
--该显示名称已经被使用,请更换其他显示名称!
4. re: VSTO开发团队成立
@netexplorer @humin @guofu @Joshua Zhu
谢谢支持
--Cameo
5. re: VSTO开发团队成立
加入加入,学习学习
--Joshua Zhu
阅读排行榜
1. Visual studio tools for office体系结构(1842)
2. 值类型与引用类型(1183)
3. 推荐一个.net chart开源组件ZedGraph(724)
4. GridView里面的内嵌的服务器控件如LinkButton如何传值?(635)
5. ParameterDirection参数类型(546)
评论排行榜
1. VSTO开发团队成立(8)
2. Visual studio tools for office体系结构(7)
3. .NET 中间语言MSIL(5)
4. 值类型与引用类型(5)
5. 推荐一个.net chart开源组件ZedGraph(3)
Powered By:
博客园
模板提供
:
沪江博客