責任鏈模式(Chain of Responsibility Pattern):
定義:Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request .Chain the receiving objects and pass the request along the chain until an object handles it.(使多個對象都有機會處理請求,從而避免了請求的發送者和接收者之間的耦合關系,見這些對象連成一條鏈,并沿著這條鏈傳遞該要求,直到有對象處理它為止。)
通用類圖:
       
優點:
責任鏈模式非常顯著的優點是將請求和處理分開。請求者可以不用知道是誰處理的,處理者可以不用知道請求的全貌,兩者解耦,提高系統的靈活性;
缺點:
1、性能問題,每個請求都是從鏈頭遍歷到鏈尾,特別是在鏈比較長的時候,性能是一個非常大的問題。
2、調試不是很方便,特別是鏈條比較長,環節比較多的時候,由于采用了類似遞歸的方式,調試的時候邏輯可能比較復雜。
實現:
在生活中,公司部門舉辦活動需要申請金費,如果金費<=1000,部門經理就可以批準發放,如果金額超過1000而<=5000,部門經理無權力批準,副總經理可以批準發放,如果金額超過5000而<=10000,副總經理沒有權力批準,此時只有總經理可以批準,而超過10000,就要就行會議決定。對于這樣的需求,每個角色的責任是不一樣的,如果使用傳統的方式,可能是寫個if多條件判斷,可以滿足現有的需要,但是后面增加新的角色賦予責任,就需要改變if結構判斷,違背設計原則中的對擴展開發,對修改關閉原則。使用責任鏈模式可以很方便的擴展。
/// <summary>  /// 行為請求  /// </summary>  public class BehaviourRequest  {      /// <summary>      /// 金額      /// </summary>      public double Money { get; set; }        /// <summary>      /// 活動名稱      /// </summary>      public string ActiveName { get; set; }        public BehaviourRequest(double money, string activename)       {          this.Money = money;          this.ActiveName = activename;      }  } /// <summary>  /// 角色抽象類  /// </summary>  public abstract class RoleAbstract  {      public RoleAbstract NextRole { get; set; }      public string name { get; set; }      public RoleAbstract(string name)      { this.name = name; }        /// <summary>      /// 該角色的執行行為      /// </summary>      public abstract void Behaviour(BehaviourRequest request);  }/// <summary>  /// 部門經理  /// </summary>  public class ManagerRole:RoleAbstract  {      public ManagerRole(string name) : base(name) { }        public override void Behaviour(BehaviourRequest request)      {          if (request.Money <= 1000)          {              Console.WriteLine("{0}的請求批準得到{1}批準,需要金額:{2}", request.ActiveName,this.name, request.Money);          }          else if (NextRole != null)          {              Console.WriteLine("{0}無權力批準,給上級{0}處理!", this.name, NextRole.name);              NextRole.Behaviour(request);          }      }  } /// <summary>  /// 副總經理角色  /// </summary>  public class PResidentRole:RoleAbstract  {      public PresidentRole(string name) : base(name) { }        public override void Behaviour(BehaviourRequest request)      {          if (request.Money <= 5000)           {              Console.WriteLine("{0}的請求批準得到{1}批準,需要金額:{2}", request.ActiveName, this.name, request.Money);          }          else if (NextRole != null)          {              Console.WriteLine("{0}無權力批準,給上級{0}處理!", this.name, NextRole.name);              NextRole.Behaviour(request);          }      }  }  /// <summary>  /// 總經理  /// </summary>  public class PresidengtRole:RoleAbstract  {      public PresidengtRole(string name) : base(name) { }        public override void Behaviour(BehaviourRequest request)      {          if (request.Money <= 10000)          {              Console.WriteLine("{0}的請求批準得到{1}批準,需要金額:{2}", request.ActiveName, this.name, request.Money);          }          else           {              Console.WriteLine("這個活動需要進行會議討論決定");          }      }  } /// <summary>  /// C#設計模式-責任鏈模式  /// </summary>  class Program  {      static void Main(string[] args)      {          //活動信息          BehaviourRequest behavior = new BehaviourRequest(10000, "部門招商活動");                    //對該活動的審批可能涉及的角色          RoleAbstract manager = new ManagerRole("部門經理");          RoleAbstract vp = new PresidentRole("副總經理");          RoleAbstract pre = new PresidengtRole("總經理");            //設置責任鏈          manager.NextRole = vp;          vp.NextRole = pre;            //請求處理          manager.Behaviour(behavior);      }  }   好了,這一章就寫到這,歡迎大家加入QQ群:280993838 。或者關注我的公眾號:
新聞熱點
疑難解答