2020-08-05

设计模式之责任链模式

责任链模式

职责链(责任链)模式 Chain of Responsibility

Intro

职责链(责任链)模式,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。
发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求,这使得系统可以在不影响客户端的情况下动态地重新组织和分配责任。

Sample

public class Request{ public string RequestContent { get; set; } public int RequestNum { get; set; }}public abstract class Manager{ protected readonly string ManagerName; protected Manager Superior; protected Manager(string managerName) => ManagerName = managerName; public void SetSuperior(Manager superior) {  Superior = superior; } public abstract void RequestApplications(Request request);}public class CommonManager : Manager{ public CommonManager(string managerName) : base(managerName) { } public override void RequestApplications(Request request) {  if (request.RequestNum <= 2)  {   Console.WriteLine($"{ManagerName}: {request.RequestContent} {request.RequestNum}天被批准");  }  else  {   Superior?.RequestApplications(request);  } }}public class Majordomo : Manager{ public Majordomo(string managerName) : base(managerName) { } public override void RequestApplications(Request request) {  if (request.RequestNum <= 5)  {   Console.WriteLine($"{ManagerName}: {request.RequestContent} {request.RequestNum}天被批准");  }  else  {   Superior?.RequestApplications(request);  } }}public class GeneralManager : Manager{ public GeneralManager(string managerName) : base(managerName) { } public override void RequestApplications(Request request) {  if (request.RequestNum <= 10)  {   Console.WriteLine($"{ManagerName}: {request.RequestContent} {request.RequestNum}天被批准");  }  else  {   Console.WriteLine($"{ManagerName}: {request.RequestContent} {request.RequestNum}天不被批准");  } }}var manager = new CommonManager("金利");var manager1 = new Majordomo("宗剑");var manager2 = new GeneralManager("钟精励");manager1.SetSuperior(manager2);manager.SetSuperior(manager1);var request = new Request(){ RequestNum = 4, RequestContent = "小菜请假"};manager.RequestApplications(request);

More

在职责链模式中,多个处理器(也就是刚刚定义中说的"接收对象")依次处理同一个请求。一个请求先经过 A 处理器处理,然后再把请求传递给 B 处理器,B 处理器处理完后再传递给 C 处理器,以此类推,形成一个链条。链条上的每个处理器各自承担各自的处理职责,所以叫作职责链模式(责任链模式)。

有没有觉得 asp.net core 中请求管道中间件和上面的实例有点相像呢,

其实 asp.net core 中请求管道中间件的设计就是这种模式的变形,可以很灵活的处理请求

我封装了一个 PipelineBuilder (https://www.cnblogs.com/weihanli/p/12700006.html) 用来轻松的构建中间件模式的代码,
和 asp.net core 中间件的代码几乎一摸一样,可以看下面的示例:

private class RequestContext{ public string RequesterName { get; set; } public int Hour { get; set; }}public static void Test(){ var requestContext = new RequestContext() {  RequesterName = "Kangkang",  Hour = 12, }; var builder = PipelineBuilder.Create<RequestContext>(context =>   {    Console.WriteLine($"{context.RequesterName} {context.Hour}h apply failed");   })   .Use((context, next) =>   {    if (context.Hour <= 2)    {     Console.WriteLine("pass 1");    }    else    {     next();    }   })   .Use((context, next) =>   {    if (context.Hour <= 4)    {     Console.WriteLine("pass 2");    }    else    {     next();    }   })   .Use((context, next) =>   {    if (context.Hour <= 6)    {     Console.WriteLine("pass 3");    }    else    {     next();    }   })  ; var requestPipeline = builder.Build(); foreach (var i in Enumerable.Range(1, 8)) {  Console.WriteLine();  Console.WriteLine($"--------- h:{i} apply Pipeline------------------");  requestContext.Hour = i;  requestPipeline.Invoke(requestContext);  Console.WriteLine("----------------------------");  Console.WriteLine(); }}

Reference

  • https://github.com/WeihanLi/DesignPatterns/tree/master/BehaviorPattern/ChainOfResponsibilityPattern
  • https://www.cnblogs.com/weihanli/p/12700006.html
设计模式之责任链模式推荐课程旺店的秘密-解密亚马逊店铺运营要点photobucket可怕!产品莫名被下架,连小红旗都没有?跨境卖家怎样利用"礼品指南"提升店铺销售?国际物流上涨后如何更新速卖通运费模板?去西冲海鲜街买海鲜要注意什么?西冲露营需要带什么东西啊?去西冲旅游住哪里比较好?

No comments:

Post a Comment