职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
这里发出这个请求的客户端并不知道这当中的哪一个对象最终处理这个请求,这些系统的更改可以在不影响客户端的情况下动态地重新组织和分配责任。
ConcreteHandler类,具体处理类,处理它所负责的请求,可访问它的后继者,如果可处理该请求,就处理,否则就该请求转发给它的后继者
class Program
{
static void Main(string[] args)
{
Handler h1 = new ConcreteHandle1();
Handler h2 = new ConcreteHandle2();
Handler h3 = new ConcreteHandle3();
h1.SetSuccessor(h2);
h2.SetSuccessor(h3);
int[] requests = { 2, 36, 14, 22, 18, 3, 27, 10 };
foreach (int request in requests)
{
h1.HandleRequest(request);
}
Console.ReadLine();
}
}
abstract class Handler
{
protected Handler successor;
public void SetSuccessor(Handler successor)//设置继任者
{
this.successor = successor;
}
public abstract void HandleRequest(int request);//处理请求的抽象方法
}
class ConcreteHandle1 : Handler
{
public override void HandleRequest(int request)
{
if (request >= 0 && request < 10)
{
Console.WriteLine("{0} 处理请求 {1}", this.GetType().Name, request);
}
else if (successor != null)
{
successor.HandleRequest(request);//转移到下一位
}
}
}
class ConcreteHandle2 : Handler
{
public override void HandleRequest(int request)
{
if (request >= 10 && request < 20)
{
Console.WriteLine("{0}处理请求{1}", this.GetType().Name, request);
}
else if (successor != null)
{
successor.HandleRequest(request);
}
}
}
class ConcreteHandle3 : Handler
{
public override void HandleRequest(int request)
{
if (request > 20 && request <= 30)
{
Console.WriteLine("{0}处理请求{1}",this.GetType().Name,request);
}
else if(successor!=null)
{
successor.HandleRequest(request);
}
}
}
职责链的好处:当客户提交一个请求时,请求是沿链传递直至有一个ConcreteHandle对象负责处理它
实例1
这可以让接收者和发送者都没有对方的明确信息,且链中的对象自己也并不知道链的结构。结果是职责链可简化对象的相互连接,它们仅需保持一个指向其后继者的引用,而不需保持它所有的候选接受者的引用。
class Program
{
static void Main(string[] args)
{
CommonManager Staff = new CommonManager("经理");
Majordomo Staff2 = new Majordomo("总监");
GeneralManager Staff3 = new GeneralManager("总经理");
Staff.SetSuperior(Staff2);
Staff2.SetSuperior(Staff3);
Request request = new Request();
request.RequestType = "请假";
request.RequestContent = "小元请假";
request.Number = 1;
Staff.RequestApplications(request);
Request request2 = new Request();
request2.RequestType = "请假";
request2.RequestContent = "小元请假";
request2.Number = 4;
Staff.RequestApplications(request2);
Request request3 = new Request();
request3.RequestType = "加薪";
request3.RequestContent = "小元请求加薪";
request3.Number = 500;
Staff.RequestApplications(request3);
Request request4 = new Request();
request4.RequestType = "加薪";
request4.RequestContent = "小元请求加薪";
request4.Number = 1000;
Staff.RequestApplications(request4);
Console.ReadLine();
}
}
//管理者
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; }
}
}
实例2
在游戏开发中职责链模式也是很常用的,多用于闯关游戏中关卡的控制器
Gof模式让一群对象都有机会来处理一项请求,以减少请求发送者与接收者之间的耦合度(即依赖度)。将所有的接收者对象串接起来,让请求沿着串接传递,直到有一个对象可以处理为止。
当系统发送过关的指令,发送到关卡管理器中,防止反复反应。
IStageHandler:定义可以处理“过关判断”和“关卡开启”的接口,也包含指向下一个关卡对象的引用。
NormalStageHandler:实现关卡接口,负责"常规"关卡的开启和过关条件判断
IStageScore:定义判断通关与否的操作接口
StageScoreEnemyKilledCount:使用当前的“击杀敌方角色数”,作为通关与否的判断
IStageData:定义关卡内容的操作接口
NormalStageData:实现“常规”关卡内容,实际将产生的敌方角色放入战场攻击玩家阵营,以及实现判断关卡是否结束的方法