4.1 Chain of Responsibility(职责链)

【返回目录】

当我们去肯德基就餐(只为假设场景,本人并不提倡),假设该肯德基餐厅有3个服务窗口,我们会发现这样一个现象:1号服务员柜台前排有3名顾客,而2、3号服务员柜台前并没有顾客,这时候还不等您自己挪位置,二号服务员会主动说:“请顾客到这边来排队点餐!”;当2号服务员柜台前排了几位顾客后,3号服务员又会向大家说到:“请顾客到这边来排队点餐!”

这样的服务机制给肯德基、麦当劳等世界先进的餐饮企业带来了很高的工作效率和很好的用户评价,那么我们就来分析一下这种机制吧。

肯德基里的每一位服务员都会为顾客提供服务,当一位顾客走进肯德基的时候,他并不确定哪位服务员会为他服务,在肯德基和麦当劳,没有VIP一说,他会在顾客队列中排队,当当前队列的服务员正在忙着处理前面顾客的请求而旁边有空闲的服务员时,该服务员会将后面的一些用户请求发送给当前具有处理能力的服务员,后者将会处理这些顾客的请求,并且以此类推。也就是说,您可以将柜台后这一排服务员看作一个对象队列,您发送请求给第一个对象,如果它有能力处理它就会处理,如果它没有能力处理,那么它会把这个请求向下一个对象传递,直到找到一个对象可以来处理您的请求。

这也就是职责链模式的设计思路:

   1: using System;
   2:  
   3: namespace Autumoon.DesignPatterns.ChainOfResponsibility
   4: {
   5:     public abstract class Handler
   6:     {
   7:         protected Handler successorHandler = null;
   8:  
   9:         abstract public void HandleRequest(Request request);
  10:  
  11:         public void SetSuccessor(Handler sucessor)
  12:         {
  13:             successorHandler = sucessor;
  14:         }
  15:     }
  16:  
  17:     public class ConcreteHandlerA : Handler
  18:     {
  19:         override public void HandleRequest(Request request)
  20:         {
  21:             if (request.RequestType == 1)
  22:             {
  23:                 Console.WriteLine("Request handled in ConcreteHandlerA");
  24:  
  25:                 return;
  26:             }
  27:  
  28:             if (this.successorHandler != null)
  29:             {
  30:                 successorHandler.HandleRequest(request);
  31:             }
  32:         }
  33:     }
  34:  
  35:     public class ConcreteHandlerB : Handler
  36:     {
  37:         override public void HandleRequest(Request request)
  38:         {
  39:             if (request.RequestType == 2)
  40:             {
  41:                 Console.WriteLine("Request handled in ConcreteHandlerB");
  42:  
  43:                 return;
  44:             }
  45:  
  46:             if (this.successorHandler != null)
  47:             {
  48:                 successorHandler.HandleRequest(request);
  49:             }
  50:         }
  51:     }
  52:  
  53:     public class ConcreteHandlerC : Handler
  54:     {
  55:         override public void HandleRequest(Request request)
  56:         {
  57:             if (request.RequestType == 3)
  58:             {
  59:                 Console.WriteLine("Request handled in ConcreteHandlerC");
  60:  
  61:                 return;
  62:             }
  63:  
  64:             if (this.successorHandler != null)
  65:             {
  66:                 successorHandler.HandleRequest(request);
  67:             }
  68:         }
  69:     }
  70:  
  71:     public class Request
  72:     {
  73:         public int RequestType { get; private set; }
  74:         public string RequestParameters { get; private set; }
  75:  
  76:         public Request(int requestType, string requestParameters)
  77:         {
  78:             this.RequestType = requestType;
  79:             this.RequestParameters = requestParameters;
  80:         }
  81:     }
  82: }

我建议你来调用这个程序看看效果,就能够很好地理解这条链了:

   1: static void Main(string[] args)
   2: {
   3:     #region Chain of Responsibility
   4:     Handler handlerA = new ConcreteHandlerA();
   5:     Handler handlerB = new ConcreteHandlerB();
   6:     Handler handlerC = new ConcreteHandlerC();
   7:     handlerA.SetSuccessor(handlerB);
   8:     handlerB.SetSuccessor(handlerC);
   9:  
  10:     Request request = new Request(2, "This are the request parameters.");
  11:     handlerA.HandleRequest(request);
  12:     #endregion
  13:  
  14:     Console.ReadLine();
  15: }

KFC

posted @ 2008-02-21 17:14  麦机长  阅读(632)  评论(0编辑  收藏  举报