记忆潜处
With great power comes great responsibility
博客园
首页
社区
新随笔
联系
订阅
管理
随笔-42 评论-128 文章-0 trackbacks-4
Core Design Patterns(16) Chain of Responsibility 职责链模式
VS 2008
多个对象构成一个链式结构,客户端向链中的某个对象发出请求,被请求的对象可以处理客户代码的请求,也可以根据业务将请求向下传递,依此类推,直到请求最终被某对象处理,或抛出异常。这就是职责链模式的应用场景。
1. 模式UML图
2. 代码示例
例子仅为演示职责链模式。
软件从业人员,可能分SE, SSE, PL, PM等不同级别,有些工作可能按工作性质需要不同级别的人员才能处理,对于自己不能处理的工作,那么,他需要将请求传递给下家。
IWorker.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
DesignPattern.ChainOfResponsibility.BLL
{
public
interface
IWorker
{
void
DoWork(
int
level);
}
}
SE.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
DesignPattern.ChainOfResponsibility.BLL
{
public
class
SE : IWorker
{
private
IWorker next;
public
SE(IWorker next)
{
this
.next
=
next;
}
IWorker Members
#region
IWorker Members
public
void
DoWork(
int
level)
{
if
(level
==
1
)
{
Console.WriteLine(
"
I am a software engineer, I can handle it
"
);
}
else
{
if
(next
!=
null
)
{
next.DoWork(level);
}
}
}
#endregion
}
}
SSE.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
DesignPattern.ChainOfResponsibility.BLL
{
public
class
SSE : IWorker
{
private
IWorker next;
public
SSE(IWorker next)
{
this
.next
=
next;
}
IWorker Members
#region
IWorker Members
public
void
DoWork(
int
level)
{
if
(level
==
2
)
{
Console.WriteLine(
"
I am a senior software engineer, I can handle it
"
);
}
else
{
if
(next
!=
null
)
{
next.DoWork(level);
}
}
}
#endregion
}
}
PL.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
DesignPattern.ChainOfResponsibility.BLL
{
public
class
PL : IWorker
{
private
IWorker next;
public
PL(IWorker next)
{
this
.next
=
next;
}
IWorker Members
#region
IWorker Members
public
void
DoWork(
int
level)
{
if
(level
==
3
)
{
Console.WriteLine(
"
I am a project leader, I can handle it
"
);
}
else
{
if
(next
!=
null
)
{
next.DoWork(level);
}
}
}
#endregion
}
}
PM.cs
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
DesignPattern.ChainOfResponsibility.BLL
{
public
class
PM : IWorker
{
private
IWorker next;
public
PM(IWorker next)
{
this
.next
=
next;
}
IWorker Members
#region
IWorker Members
public
void
DoWork(
int
level)
{
if
(level
==
4
)
{
Console.WriteLine(
"
I am a project manager, I can handle it
"
);
}
else
{
if
(next
!=
null
)
{
next.DoWork(level);
}
}
}
#endregion
}
}
Client
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
DesignPattern.ChainOfResponsibility.BLL;
namespace
DesignPattern.ChainOfResponsibility
{
class
Program
{
static
void
Main(
string
[] args)
{
IWorker worker
=
new
SE(
new
SSE(
new
PL(
new
PM(
null
))));
worker.DoWork(
3
);
}
}
}
Output
posted on 2008-04-01 21:34
Tristan(GuoZhijian)
阅读(178)
评论(3)
编辑
收藏
所属分类:
Design Pattern
评论:
#1楼
2008-04-22 02:41 |
睿 [未注册用户]
整体上它让我明白那些 Design Pattern 的特性这让我很高兴也很感谢你的帮忙(毕竟在这之前我一直不太了解那些英文版本的注解)。
还有若是能加上一些资料那会更好,比如:该Design Pattern的利与弊和在那种情况下我们会需要用到它。
最后一样,为什么我没有我需要的Command Design Pattern....
回复
引用
#2楼
[
楼主
] 2008-04-22 14:22 |
Tristan(Guozhijian)
@睿
啊,我也是把我理解的写下来,欢迎讨论,互相提高
这个系列还没有写完
你提到的Command模式,可能是下一篇,最近忙于研究jQuery,所以一段时间没有更新模式的内容了
回复
引用
查看
#3楼
2008-05-13 16:45 |
吉日嘎拉 [未注册用户]
16个模式,我从头看到了尾巴,记住了好几个,
接着也背一背,今天没白过,学了你的设计模式。
一直设置为IE的首页,总算都看了一遍了。
回复
引用
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
闪存
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索
相关文章:
小菜编程成长记(十三 设计模式不能戏说!设计模式怎就不能戏说?)
控件之ViewState
.NET设计模式系列文章
设计模式(17)-Chain of Responsibility Pattern
Mmultiple of IE - 多版本 IE 浏览器
新一代处理器将以Core i7之名正式登场
I am get ahead of myself什么意思
"Out of Memory" exception
推荐一些技术书籍
XUnit Test Patterns这本书值得看看
相关链接:
所属分类的其他文章:
Core Design Patterns(16) Chain of Responsibility 职责链模式
Core Design Patterns(15) Template Method 模版方法模式
Core Design Patterns(14) State 状态模式
Core Design Patterns(13) Strategy 策略模式
Core Design Patterns(12) Builder 建造者模式
Core Design Patterns(11) Abstract Factory 抽象工厂模式
Core Design Patterns(10) Singleton 单例模式
Core Design Patterns(9) Factory Method 工厂方法模式
Core Design Patterns(8) Prototype 原型模式
Core Design Patterns(7) Facade 外观模式
最新IT新闻:
Silverlight 2正式版SDK可以下载
Silverlight对Flash 微软打垮Adobe
IBM扩大研究规模 在上海成立研究院
eWeek评Google八大“20%项目”
微软终于发布Silverlight 2正式版
Tristan obtained a master's degree of mathematics at DongHua university in Mar. 2006. Now he works at Cognizant co. as a software engineer. He is currently working with c# mostly in web application development.He has a good understanding of CLR, asp.net, javascript, ajax, xml, xslt, OOP, design pattern, etc.
You can contact him
by email: promiss#live.cn
<
2008年4月
>
日
一
二
三
四
五
六
30
31
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
1
2
3
4
5
6
7
8
9
10
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
(3)
给我留言
查看留言
我参加的小组
博客园上海俱乐部
我参与的团队
上海.NET俱乐部(0/2250)
随笔分类
(42)
Asp.Net(5)
Asp.Net Ajax(14)
C#(3)
Design Pattern(16)
SqlServer2005(2)
Windows Forms App(2)
随笔档案
(42)
2008年4月 (1)
2008年3月 (12)
2008年2月 (18)
2008年1月 (7)
2007年12月 (3)
2007年11月 (1)
.Net Professionals
Ian Griffiths' Weblog
积分与排名
积分 - 35662
排名 - 1213
最新随笔
1. Core Design Patterns(16) Chain of Responsibility 职责链模式
2. Core Design Patterns(15) Template Method 模版方法模式
3. Core Design Patterns(14) State 状态模式
4. Core Design Patterns(13) Strategy 策略模式
5. Core Design Patterns(12) Builder 建造者模式
6. Core Design Patterns(11) Abstract Factory 抽象工厂模式
7. Core Design Patterns(10) Singleton 单例模式
8. Core Design Patterns(9) Factory Method 工厂方法模式
9. Core Design Patterns(8) Prototype 原型模式
10. Core Design Patterns(7) Facade 外观模式
11. Core Design Patterns(6) Adapter 适配器模式
12. Core Design Patterns(5) Flyweight 享元模式
13. Core Design Patterns(4) Composite 组合模式
14. Core Design Patterns(3) Bridge 桥接模式
15. Core Design Patterns(2) Proxy 代理模式
16. Core Design Patterns(1) Decorator 装饰模式
17. 老调重弹:插件式框架开发的一个简单应用
18. Behavior模型应用:可拖动的div容器
19. Microsoft Asp.Net Ajax框架入门(13) PageRequestManager
20. Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
最新评论
1. re: Microsoft Asp.Net Ajax框架入门(3) 操作DOM元素
怎么操作DOM的添加和删除
--kog
2. re: 浅述WinForm多线程编程与Control.Invoke的应用
多谢,多谢,帮我解决了个问题。
--Hecate_Eos
3. re: sqlserver2005数据库还原脚本
在management studio中使用restore时,生成的脚本如下: RESTORE DATABASE IAC FILE = N'IAC' FROM DISK = 'l:\IACSyst...
--小市民
4. re: Behavior模型应用:可拖动的div容器
哥们!加油,这几篇关于ASP.NET AJAX 的我都看了,不错。
--GoodGoodStudy
阅读排行榜
1. 浅述WinForm多线程编程与Control.Invoke的应用(3597)
2. 我的WinForm App自动更新(Live Update)架构(2359)
3. Core Design Patterns(14) State 状态模式(2254)
4. 通过继承ConfigurationSection,在web.config中增加自定义配置(1909)
5. 老调重弹:插件式框架开发的一个简单应用(1807)
评论排行榜
1. 我的WinForm App自动更新(Live Update)架构(36)
2. 浅述WinForm多线程编程与Control.Invoke的应用(24)
3. Microsoft Asp.Net Ajax框架入门(7) 名称空间、类、继承、接口(10)
4. 老调重弹:插件式框架开发的一个简单应用(8)
5. Core Design Patterns(14) State 状态模式(8)