介绍:
职责链模式(Chain of Responsibility):使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
结构图
基本代码
Handler 类,定义一个处理请示的接口
abstract class Handler
{
protected Handler successor;
public void SetSuccessor(Handler successor) //设置继承制
{
this.successor = successor;
}
public abstract void HandleRequest(int request); //处理请求的抽象方法
}
ConcreteHandler 类,具体处理者类,处理它所负责的请求,课访问它的后继者,如果可处理该请求,就处理之,否则就将该请求转发给它的后继者。
ConcreteHandler1,当请求数在0到10之间则有权处理,否则转到下一位。
class ConcreteHandler1 : Handler
{
public override void HandleRequest(int request)
{
if (request>=0 && request <10) //0到10 处理此请求
{
Console.WriteLine("{0} 处理请求 {1}",
this.GetType().Name, request);
}
else if (successor !=null)
{
successor.HandleRequest(request); //转移到下一位
}
}
}
ConcreteHandler2,当请求数在10到20之间则有权处理,否则转到下一位
class ConcreteHandler2 : Handler
{
public override void HandleRequest(int request)
{
if (request >=10 && request <20) //10到20,处理此请求
{
Console.WriteLine("{0} 处理请求 {1}",
this.GetType().Name, request);
}
else if (successor !=null)
{
successor.HandleRequest(request ); //转移到下一位
}
}
}
ConcreteHandler3,当请求数在20到30之间则有权处理,否则转到下一位
class ConcreteHandler3 : Handler
{
public override void HandleRequest(int request)
{
if (request >= 20 && request < 30) //20到30,处理此请求
{
Console.WriteLine("{0} 处理请求 {1}",
this.GetType().Name, request);
}
else if (successor != null)
{
successor.HandleRequest(request); //转移到下一位
}
}
}
例子
小菜在公司干满三个月,马上就要办转正手续了,这时,小菜打算向经理提下加薪的事情。
当小菜向经理提出加薪的事情后,经理先是一顿夸奖,然后告诉小菜说加薪的事情他做不了主,需要向人力资源部的总监汇报,而总监也说加薪的事情做不了主,需要向总经理汇报.......
加薪代码结构图
管理者代码
abstract class Manager
{
protected string name;
//管理者的上级
protected Manager superior;
public Manager(string name)
{
this.name = name;
}
//设置管理者的上级
public void SetSuperior(Manager superior)
{
this.superior = superior;
}
//申请请求
abstract public void RequestApplications(Request request);
}
经理
class CommonManager : Manager
{
public CommonManager(string name)
: base(name)
{ }
public override void RequestApplications(Request request)
{
if (request.RequestType == "请假" && request.Number <= 2)
{
Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestContent, request.Number);
}
else
{
if (superior != null)
superior.RequestApplications(request);
}
}
}
总监
class Majordomo : Manager
{
public Majordomo(string name)
: base(name)
{ }
public override void RequestApplications(Request request)
{
if (request.RequestType == "请假" && request.Number <= 5)
{
Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestContent, request.Number);
}
else
{
if (superior != null)
superior.RequestApplications(request);
}
}
}
总经理
class GeneralManager : Manager
{
public GeneralManager(string name)
: base(name)
{ }
public override void RequestApplications(Request request)
{
if (request.RequestType == "请假")
{
Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestContent, request.Number);
}
else if (request.RequestType == "加薪" && request.Number <= 500)
{
Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestContent, request.Number);
}
else if (request.RequestType == "加薪" && request.Number > 500)
{
Console.WriteLine("{0}:{1} 数量{2} 再说吧", name, request.RequestContent, request.Number);
}
}
}
申请
class Request
{
//申请类别
private string requestType;
public string RequestType
{
get { return requestType; }
set { requestType = value; }
}
//申请内容
private string requestContent;
public string RequestContent
{
get { return requestContent; }
set { requestContent = value; }
}
//数量
private int number;
public int Number
{
get { return number; }
set { number = value; }
}
}
客户端代码
static void Main(string[] args)
{
CommonManager jinli = new CommonManager("金利");
Majordomo zongjian = new Majordomo("宗剑");
GeneralManager zhongjingli = new GeneralManager("钟精励");
jinli.SetSuperior(zongjian);
zongjian.SetSuperior(zhongjingli);
Request request = new Request();
request.RequestType = "请假";
request.RequestContent = "小菜请假";
request.Number = 1;
jinli.RequestApplications(request);
Request request2 = new Request();
request2.RequestType = "请假";
request2.RequestContent = "小菜请假";
request2.Number = 4;
jinli.RequestApplications(request2);
Request request3 = new Request();
request3.RequestType = "加薪";
request3.RequestContent = "小菜请求加薪";
request3.Number = 500;
jinli.RequestApplications(request3);
Request request4 = new Request();
request4.RequestType = "加薪";
request4.RequestContent = "小菜请求加薪";
request4.Number = 1000;
jinli.RequestApplications(request4);
Console.Read();
}
效果图
职责链模式优点
职责链模式非常显著的优点是将请求和处理分开。请求者可以不用知道是谁处理的,处理者可以不用知道请求的全貌两者解耦,提高系统的灵活性。
职责链模式缺点
一是性能问题,每个请求都是从链头遍历到链尾,特别是在链比较长的时候,性能是一个非常大的问题。二是调试很不方便,特别是链条比较长,环节比较多的时候,由于采用了类似递归的方式,调试的时候逻辑可能比较复杂