C#面向对象设计模式纵横谈——20 Chain of Responsibility职责链模式

主讲:李建忠

来源:http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/consyscourse/CsharpOOD.aspx

 

clip_image002

clip_image004

clip_image006

clip_image008

clip_image010

 

   1:  abstract class BaseHandler
   2:  {
   3:      public BaseHandler(BaseHandler next)
   4:      {
   5:          this.next=next;
   6:      }
   7:   
   8:      protected abstract bool CanHandleRequest();
   9:      public abstract void HandleRequest(Request request)
  10:      {
  11:          if(this.next!=null)
  12:          {
  13:              this.next.HandleRequest(request);
  14:          }
  15:      }
  16:   
  17:      private BaseHandler next;
  18:   
  19:      public BaseHandler Next
  20:      {
  21:          get
  22:          {
  23:              return this.next;
  24:          }
  25:          set
  26:          {
  27:              this.next=value;
  28:          }
  29:      }
  30:  }
  31:   
  32:  public class AHandler:BaseHandler
  33:  {
  34:      public AHandler(BaseHandler next):base(next)
  35:      {
  36:          
  37:      }
  38:   
  39:      pprotected override bool CanHandleRequest()
  40:      {
  41:   
  42:      }
  43:   
  44:      public override void HandleRequest(Request request)
  45:      {
  46:          if(this.CanHandleRequest())
  47:          {
  48:   
  49:          }
  50:          else
  51:          {
  52:              base.HandleRequest(request);
  53:          }
  54:      }
  55:  }
  56:   
  57:  public class BHandler:BaseHandler
  58:  {
  59:      public BHandler(BaseHandler next):base(next)
  60:      {
  61:          
  62:      }
  63:   
  64:      pprotected override bool CanHandleRequest()
  65:      {
  66:   
  67:      }
  68:   
  69:      public override void HandleRequest(Request request)
  70:      {
  71:          if(this.CanHandleRequest())
  72:          {
  73:   
  74:          }
  75:          else
  76:          {
  77:              base.HandleRequest(request);
  78:          }
  79:      }
  80:  }
  81:   
  82:  public class CHandler:BaseHandler
  83:  {
  84:      public CHandler(BaseHandler next):base(next)
  85:      {
  86:          
  87:      }
  88:   
  89:      pprotected override bool CanHandleRequest()
  90:      {
  91:   
  92:      }
  93:   
  94:      public override void HandleRequest(Request request)
  95:      {
  96:          if(this.CanHandleRequest())
  97:          {
  98:   
  99:          }
 100:          else
 101:          {
 102:              base.HandleRequest(request);
 103:          }
 104:      }
 105:  }
 106:   
 107:  public class Sender
 108:  {
 109:      public void Process()
 110:      {
 111:          Request request=new Request();
 112:   
 113:          Handler handler1=new AHandler(null);
 114:          Handler handler2=new BHandler(handler1);
 115:          Handler handler3=new CHandler(handler2);
 116:   
 117:          handler3.HandleRequest(request);
 118:      }
 119:  }

 

clip_image012

clip_image014

你可能感兴趣的:(chain)