Design Pattern Ten: Chain of Responsibility Pattern

       Chain of Responsibility Pattern :

       Definition: 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. (so that multiple objects have a chance Process the request, thereby avoiding the coupling relationship between the sender and receiver of the request, see these objects into a chain, and pass the request along this chain until an object handles it. )

       General class diagram:

       


        advantage:

        A very significant advantage of the Chain of Responsibility pattern is the separation of request and processing. The requester does not need to know who processed it, and the processor does not need to know the whole picture of the request. The decoupling of the two improves the flexibility of the system;

        shortcoming:

        1. Performance issues, each request traverses from the chain head to the chain end, especially when the chain is relatively long, performance is a very big problem.

        2. Debugging is not very convenient, especially when the chain is relatively long and there are many links. Due to the use of a similar recursive method, the logic may be more complicated during debugging.

       accomplish:

       In daily life, the company department needs to apply for a gold fee to hold an event. If the gold fee is <= 1000, the department manager can approve the payment. If the amount exceeds 1000 but <= 5000, the department manager has no power to approve it, and the deputy general manager can approve the payment. If If the amount exceeds 5,000 but <= 10,000, the deputy general manager has no right to approve it. At this time, only the general manager can approve it. If the amount exceeds 10,000, it must be decided at a meeting. For such requirements, the responsibilities of each role are different. If you use the traditional method, you may write an if multi-condition judgment, which can meet the existing needs, but if you add new roles and assign responsibilities later, you need to change the if Structural judgment violates the principle of extension development and modification closure in the design principles. Using the Chain of Responsibility pattern can be easily extended.

    

/// <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);  
    }  
}  

  Well, that's all for this chapter. Welcome everyone to join the QQ group: 280993838. Or pay attention to my official account:

Guess you like

Origin blog.csdn.net/zjw1349547081/article/details/54906445