[创建型模式系列]Chain of Responsibility Pattern 责任链模式
2008-05-07 11:35
519 查看
1 Chain of Responsibility Concepts 责任链基础
Chain of Responsibility is a kind of behaviour pattern for object. In the COR pattern, many objects which are referenced to next form a responsibility chain. and the client request can be transfered on the chain until it has been processed. The requester who send s out the request will not know and does not need to know who will process his request. that makes it possible to re-allocate the tasks to process specific request without modifying the client codes.
责任链模式是一种对象的行为模式。在责任链模式里,很多对象由其对下家的引用而连接起来从而形成一条链。请求在这条链上传递,直到链上的某个对象处理这个请求为止。当然,发出请求的客户端并不知道也不需要知道链上的哪一个对象处理了他的请求,这使得系统可以在不需要修改客户端的情况下重新分配和处理责任。
2 some samples in real life现实生活中的责任链模式
1)Transfering flower while rataplaning击鼓传花
It's an old and tranditional game in China. Many people just sit down as a circle. then one people is given a flower. while rataplan begins, the follower is transfering from himself to his neighbour without break, this process continues only if the rataplan is going on; when the rataplan stops, the people who is catching the flower will get punished to drinking. haha , is it interesting?
这是中国一个古老和好玩的游戏了。若干人坐成一圈,然后一个人开始拿着一朵花。鼓声响起,花开始不间断传递给其邻居。当鼓声停止时,手上执花者将接受惩罚--喝酒 :)
2)Getting Approval level by level逐级审批
In real life, different official position has different rights and responsibilties. for instance,President keeps catch with VicePresident; VicePresident keeps catch with Managers; Managers from respectice groups keeps catch with normal staffs. when a staff has a request, he will send out it to his corresponding boss. and then the request will b
e transfered on the responsiility chain.
在现实生活中,不同的办公室职位都有着不同的权限和责任。例如,董事长管理副董事长,副董事长管理若干经理;相应的经理管理各自部门的员工。如果某个员工有某个请求需要审批,那么他会把请求递交给其上司,然后该请求开始在责任链上开始处理。 如果其经理能够处理,那么经理处理。如果不行,该请求传递给Vice President. 如果VicePresnent不能处理,则传递给President. 不同级别的人都有其相应的处理方法。
3. Chain of Responsibility Diagram and Code inplementation
part I: class hirarchy diagram
public abstract class Approver
public class Director : Approver
public class VicePresident : Approver
public class President : Approver
public class ClientRequest
class Program
{
{
President Herengang = new President("He Ren Gang");
VicePresident Guliqun = new VicePresident("Gu Li Qun");
Director Guqunjie = new Director("Gu Qun Jie");
Guqunjie.SetSuccessor(Guliqun);
Guliqun.SetSuccessor(Herengang);
ClientRequest request = new ClientRequest(100, 9999, " Hasee Laptop");
Guqunjie.ProcessRequest(request);
}
Chain of Responsibility is a kind of behaviour pattern for object. In the COR pattern, many objects which are referenced to next form a responsibility chain. and the client request can be transfered on the chain until it has been processed. The requester who send s out the request will not know and does not need to know who will process his request. that makes it possible to re-allocate the tasks to process specific request without modifying the client codes.
责任链模式是一种对象的行为模式。在责任链模式里,很多对象由其对下家的引用而连接起来从而形成一条链。请求在这条链上传递,直到链上的某个对象处理这个请求为止。当然,发出请求的客户端并不知道也不需要知道链上的哪一个对象处理了他的请求,这使得系统可以在不需要修改客户端的情况下重新分配和处理责任。
2 some samples in real life现实生活中的责任链模式
1)Transfering flower while rataplaning击鼓传花
It's an old and tranditional game in China. Many people just sit down as a circle. then one people is given a flower. while rataplan begins, the follower is transfering from himself to his neighbour without break, this process continues only if the rataplan is going on; when the rataplan stops, the people who is catching the flower will get punished to drinking. haha , is it interesting?
这是中国一个古老和好玩的游戏了。若干人坐成一圈,然后一个人开始拿着一朵花。鼓声响起,花开始不间断传递给其邻居。当鼓声停止时,手上执花者将接受惩罚--喝酒 :)
2)Getting Approval level by level逐级审批
In real life, different official position has different rights and responsibilties. for instance,President keeps catch with VicePresident; VicePresident keeps catch with Managers; Managers from respectice groups keeps catch with normal staffs. when a staff has a request, he will send out it to his corresponding boss. and then the request will b
e transfered on the responsiility chain.
在现实生活中,不同的办公室职位都有着不同的权限和责任。例如,董事长管理副董事长,副董事长管理若干经理;相应的经理管理各自部门的员工。如果某个员工有某个请求需要审批,那么他会把请求递交给其上司,然后该请求开始在责任链上开始处理。 如果其经理能够处理,那么经理处理。如果不行,该请求传递给Vice President. 如果VicePresnent不能处理,则传递给President. 不同级别的人都有其相应的处理方法。
3. Chain of Responsibility Diagram and Code inplementation
part I: class hirarchy diagram
public abstract class Approver
public class Director : Approver
public class VicePresident : Approver
public class President : Approver
public class ClientRequest
class Program
{
{
President Herengang = new President("He Ren Gang");
VicePresident Guliqun = new VicePresident("Gu Li Qun");
Director Guqunjie = new Director("Gu Qun Jie");
Guqunjie.SetSuccessor(Guliqun);
Guliqun.SetSuccessor(Herengang);
ClientRequest request = new ClientRequest(100, 9999, " Hasee Laptop");
Guqunjie.ProcessRequest(request);
}
相关文章推荐
- 极速理解设计模式系列:12.责任链模式(Chain of Responsibility Pattern)
- 极速理解设计模式系列:12.责任链模式(Chain of Responsibility Pattern)
- 乐在其中设计模式(C#) - 责任链模式(Chain of Responsibility Pattern)
- 23种设计模式(22)_行为型_责任链模式(Chain of Responsibility Pattern)
- 我所理解的设计模式(C++实现)——责任链模式(Chain Of Responsibility Pattern)
- 我所理解的设计模式(C++实现)——责任链模式(Chain Of Responsibility Pattern)
- 行为型模式:责任链模式(Chain of Responsibility Pattern)
- 1.1设计模式在Unity中应用—Behavioral Pattern— Chain of Responsibility Pattern 责任链模式
- java设计模式——责任链模式(Chain of Responsibility Pattern)
- 24种设计模式--责任链模式【Chain ofResponsibility Pattern】
- 设计模式【责任链模式Chain of Responsibility Pattern】
- java与模式 责任链模式 Chain of Responsibility pattern
- 我所理解的设计模式(C++实现)——责任链模式(Chain Of Responsibility Pattern)
- 如何让孩子爱上设计模式 ——22.责任链模式(Chain of Responsibility Pattern)
- [重构到模式-Chain of Responsibility Pattern]把Fizz Buzz招式重构到责任链模式
- 设计模式(十四)-责任链模式(Chain of Responsibility Pattern)——推卸责任
- 设计模式 - Chain of Responsibility Pattern(责任链模式)
- 【设计模式】责任链模式(Chain of Responsibility Pattern)
- 设计模式6--责任链模式(The chain of responsibility pattern)
- [重构到模式-Chain of Responsibility Pattern]把Fizz Buzz招式重构到责任链模式