职责链模式

前言

  职责链模式(Chain of Responsibility):使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

一、申请类

  

1     public class Request
2     {
3         public string RequestType { get; set; }
4         public string RequestContent { get; set; }
5         public int Number { get; set; }
6     }

 

二、管理者抽象类

  

 1   public abstract class Manager
 2     {
 3         protected string name;
 4         //管理者的上级
 5         protected Manager superior;
 6         public Manager(string name)
 7         {
 8             this.name = name;
 9         }
10         //设置管理者的上级
11         public void SetSuperior(Manager superior)
12         {
13             this.superior = superior;
14         }
15         //申请请求
16         public abstract void RequestApplication(Request request);
17     }

 

三、具体的管理类

  

 1 //经理类
 2     public class CommonManager : Manager
 3     {
 4         public CommonManager(string name) : base(name)
 5         {
 6         }
 7         public override void RequestApplication(Request request)
 8         {
 9             //经理所能有的权限就是可批准下属两天内的假期
10             if (request.RequestType == "请假" && request.Number <= 2)
11             {
12                 Console.WriteLine("{0}:{1} 数量{2} 被比准", name, request.RequestContent, request.Number);
13             }
14             else
15             {
16                 //其余的申请都需要转到上级
17                 if (superior != null)
18                 {
19                     superior.RequestApplication(request);
20                 }
21             }
22         }
23     }

 

 1  //总监
 2     public class Majordomo : Manager
 3     {
 4         public Majordomo(string name) : base(name)
 5         {
 6         }
 7 
 8         public override void RequestApplication(Request request)
 9         {
10             //总监所能有的权限就是可批准下属一周内的假期
11             if (request.RequestType == "请假" && request.Number <= 5)
12             {
13                 Console.WriteLine("{0}:{1} 数量{2} 被比准", name, request.RequestContent, request.Number);
14             }
15             else
16             {//其余的申请都需要转到上级
17                 if (superior != null)
18                 {
19                     superior.RequestApplication(request);
20                 }
21             }
22         }
23     }

 

 1  //总经理
 2     public class GeneralManager : Manager
 3     {
 4         public GeneralManager(string name) : base(name)
 5         {
 6         }
 7 
 8         public override void RequestApplication(Request request)
 9         {
10             if (request.RequestType == "请假")
11             {
12                 Console.WriteLine("{0}:{1} 数量{2} 被比准", name, request.RequestContent, request.Number);
13             }
14             else if (request.RequestType == "加薪" && request.Number <= 500)
15             {
16                 Console.WriteLine("{0}:{1} 数量{2} 被比准", name, request.RequestContent, request.Number);
17             }
18             else if (request.RequestType == "加薪" && request.Number > 500)
19             {
20                 Console.WriteLine("{0}:{1} 数量{2} 滚蛋吧你", name, request.RequestContent, request.Number);
21             }
22         }
23     }

 

四、客户端

  

 1  class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             CommonManager nini = new CommonManager("剑圣");
 6             Majordomo dada = new Majordomo("夫子");
 7             GeneralManager yeye = new GeneralManager("昊天");
 8             
 9             //设置上级,可以根据实际需求更改
10             nini.SetSuperior(dada);
11             dada.SetSuperior(yeye);
12 
13 
14             /*
15              * 客户端的申请都是由 ’经理‘ 发起,但实际谁来决策由具体管理类来处理,客户端不知道
16              */ 
17             Request request = new Request();
18             request.RequestType = "请假";
19             request.RequestContent = "剑圣娶亲";
20             request.Number = 1;
21             nini.RequestApplication(request);
22 
23             Request request2 = new Request();
24             request2.RequestType = "请假";
25             request2.RequestContent = "夫子娶亲";
26             request2.Number = 1;
27             nini.RequestApplication(request2);
28 
29             Request request3 = new Request();
30             request3.RequestType = "加薪";
31             request3.RequestContent = "夫子加钱";
32             request3.Number = 500;
33             nini.RequestApplication(request3);
34 
35             Request request4 = new Request();
36             request4.RequestType = "加薪";
37             request4.RequestContent = "昊天加钱";
38             request4.Number = 1000;
39             nini.RequestApplication(request4);
40 
41             Console.ReadKey();
42         }
43     }

 

总结

  1、最关键的是当客户提交一个请求时,请求是沿链传递直至一个ConcreteHandler对象负责处理它。

  2、接收者和发送者都没有对方的明确信息,且链中的对象自己也并不知道链的结构。结果是职责链可简化对象的相互连接,他们仅需保持一个向其后继者的引用,而不需保持它所有的候选接受者的引用。

 

  参考书籍:大话设计模式

posted @ 2018-11-19 17:26  hyunbar  阅读(191)  评论(0编辑  收藏  举报