[创建型模式系列]Chain of Responsibility Pattern 责任链模式
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
part II: implementation codes:
{
private string _name;
protected Approver _successor;
abstract public void ProcessRequest(ClientRequest clientRequest);
public Approver(string name)
{
_name = name;
}
public void SetSuccessor(Approver successor)
{
_successor = successor;
}
public string Name
{
get
{
return _name;
}
}
}
public class Director : Approver
{
public Director(string name)
: base(name)
{
}
public override void ProcessRequest(ClientRequest clientRequest)
{
if (clientRequest.Amount < 2500.0)
Console.Write("your request has been processed by Director {0}, you can buy {1} {2} with the price {3}",
Name, clientRequest.Number, clientRequest.ProductName, clientRequest.Number);
else
_successor.ProcessRequest(clientRequest);
}
}
public class VicePresident : Approver
{
public VicePresident(string name)
: base(name)
{
}
public override void ProcessRequest(ClientRequest clientRequest)
{
if (clientRequest.Amount < 5000.00)
Console.Write("your request has been processed by VicePresident {0}, you can buy {1} {2} with the price {3}",
Name, clientRequest.Number, clientRequest.ProductName, clientRequest.Number);
else
_successor.ProcessRequest(clientRequest);
}
}
public class President : Approver
{
public President(string name)
: base(name)
{
}
public override void ProcessRequest(ClientRequest clientRequest)
{
if (clientRequest.Amount < 10000.00)
Console.Write("your request has been processed by President {0}, you can buy {1} {2} with the price {3}",
Name, clientRequest.Number, clientRequest.ProductName, clientRequest.Number);
else
Console.Write("your request amout is {0}, we need to hold a excuting meeting to detemine", clientRequest.Amount);
}
}
public class ClientRequest
{
private int _number;
private double _amount;
private string _productName;
public ClientRequest(int number, int amount, string productName)
{
_number = number;
_amount = amount;
_productName = productName;
}
public int Number
{
get
{
return _number;
}
}
public double Amount
{
get
{
return _amount;
}
}
public string ProductName
{
get
{
return _productName;
}
}
}
class Program
{
static void Main(string[] args)
{
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);
}