设计模式之十:责任链模式

来源:互联网 发布:linux退出vi不保存 编辑:程序博客网 时间:2024/06/06 15:52

       责任链模式(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 。或者关注我的公众号:

0 0
原创粉丝点击