Design Patterns six: Chain of Responsibility pattern (Command)

In real life, often such a case: a request for more than one object can handle, but different treatment conditions or privileges of each object. For example, employees leave, may grant leave leaders have department heads, deputy general manager, general manager, but on different days every leader can be approved, employees must look for different leadership signature according to the number of days they have to leave, but also that information employees must remember name, phone and address each of the leaders, which increases the difficulty. There are many such examples, such as travel reimbursement looking for leadership, life in the "Drumming pass to spend" games.

In the computer hardware and software is also relevant examples, as in the datagram transfer network bus, each computer to determine whether the received address is the same as the target address of its own; and exception processing, the processing program determines whether it according to the type of exception the exception; there  Struts2  interceptors, JSP  and  Servlet  of filter, etc., all of which can be solved if the chain of responsibility pattern used.

The definition and characteristics of the model

Defined chain of responsibility (Chain of Responsibility) mode: In order to avoid requesting sender and a plurality of handlers are coupled together, all requests processed by reference in their mind that the next object and connected into a chain by the preceding object request; when there is a request occurs, the request is transmitted along the chain until an object handles it so far.

Note: Chain of Responsibility pattern is also called Chain of Responsibility pattern.

In the chain of responsibility pattern, the customer simply send the request to the chain of responsibility to process the request and details of the transfer process without concern for the request, so the sender and handler request chain of responsibility will request decoupled.

Chain of Responsibility pattern is an object behavioral pattern, its main advantages are as follows.

  1. Reducing the coupling between objects. This mode allows an object need not know which in the end is a structure which is the object request and the processing chain, the sender and recipient need not have each other also clear message.
  2. Enhancing the scalability of the system. You can add a new category of request processing as required to satisfy the principle of opening and closing.
  3. Enhancing the flexibility of assigning responsibilities to objects. When the workflow changes, you can dynamically change the members in the chain or mobilize their order can also be dynamically added or deleted responsibility.
  4. Chain of Responsibility simplifies the connection between the objects. Each object just keep a reference to its successor, without reference to keep all other handlers, which avoids the use of a large number of if statements or if ··· else.
  5. Shared responsibility. Each class need only deal with the process of their work, should not be passed to the next object processing is complete, clear all kinds of areas of responsibility, in line with the principle of single responsibility class.


The main drawback as follows.

  1. We can not guarantee that each request must be processed. As the recipient of a request is not clear, we can not guarantee that it will be processed, the request may have been passed to the end of the chain are not processed.
  2. Comparative longer duty chain, processing the request may involve a plurality of processing objects, the system performance will be affected.
  3. Customers rely on the rationality of the established chain of responsibility to ensure the end, increase the complexity of the client, may be due to incorrect settings duty chain and cause system errors, such as may result in the cycle call.

Architecture and Implementation Model

Typically, you may be realized by a data duty chain linked list data structure .

1. Structure Model

Duty chain mainly includes the following roles.

  1. Abstract handler (Handler) Roles: defining a processing request to an interface, and a processing method comprising abstract subsequent connection.
  2. DETAILED handler (Concrete Handler) Cast: abstract handlers implement the processing methods, the process determines whether this request, if the request can be processed, otherwise it forwards the request to the successor.
  3. Customer class (Client) role: to create processing chain, and target specific handler chain first submit a request, it does not care about the details of the transfer process and handle the request.


The structure shown in Figure 1. The client may be provided responsibility chain as shown in FIG. 2.
 

               Chain of Responsibility pattern configuration of FIG.
                            FIG 1 is a configuration diagram of Chain of Responsibility pattern


 

             Chain of Responsibility
                            Figure 2 chain of responsibility

2. Mode of realization

Chain of Responsibility pattern codes are as follows:

package chainOfResponsibility;
public class ChainOfResponsibilityPattern
{
    public static void main(String[] args)
    {
        //组装责任链 
        Handler handler1=new ConcreteHandler1(); 
        Handler handler2=new ConcreteHandler2(); 
        handler1.setNext(handler2); 
        //提交请求 
        handler1.handleRequest("two");
    }
}
//抽象处理者角色
abstract class Handler
{
    private Handler next;
    public void setNext(Handler next)
    {
        this.next=next; 
    }
    public Handler getNext()
    { 
        return next; 
    }   
    //处理请求的方法
    public abstract void handleRequest(String request);       
}
//具体处理者角色1
class ConcreteHandler1 extends Handler
{
    public void handleRequest(String request)
    {
        if(request.equals("one")) 
        {
            System.out.println("具体处理者1负责处理该请求!");       
        }
        else
        {
            if(getNext()!=null) 
            {
                getNext().handleRequest(request);             
            }
            else
            {
                System.out.println("没有人处理该请求!");
            }
        } 
    } 
}
//具体处理者角色2
class ConcreteHandler2 extends Handler
{
    public void handleRequest(String request)
    {
        if(request.equals("two")) 
        {
            System.out.println("具体处理者2负责处理该请求!");       
        }
        else
        {
            if(getNext()!=null) 
            {
                getNext().handleRequest(request);             
            }
            else
            {
                System.out.println("没有人处理该请求!");
            }
        } 
    }
}

Program results are as follows: 

具体处理者2负责处理该请求!

Mode of application examples

[Example 1] using a Chain of Responsibility pattern design his vacation approval module.

Analysis: if equal to or less than a predetermined students leave two days, the teacher may approve; less than or equal to 7 days, Dean approve; less than or equal to 10 days, Dean may approve; otherwise not approved; suitable for use in this example chain of responsibility implementation pattern.

First, define a leader class (Leader), which is an abstract handler, it contains a pointer to the next leader pointer next and abstract processing method handleRequest (int LeaveDays) a process dummy bar; then, define the class category (ClassAdviser), Head of the class (DepartmentHead) and Dean class (Dean), which is a subclass of the abstract handler, the handler is specific, it is necessary to realize the parent class handleRequest (int LeaveDays) method based on their own power, if not entitled to deal with it the application for leave to the next specific handler until the last; customer class is responsible for creating the processing chain, and specific handler chain for leave to head (teacher). As shown in FIG. 3 is a structural view.
 

             FIG leave requests approval module structure
                          FIG 3 leave requests approval module configuration of FIG.


Code is as follows:

package chainOfResponsibility;
public class LeaveApprovalTest
{
    public static void main(String[] args)
    {
        //组装责任链 
        Leader teacher1=new ClassAdviser();
        Leader teacher2=new DepartmentHead();
        Leader teacher3=new Dean();
        //Leader teacher4=new DeanOfStudies();
        teacher1.setNext(teacher2);
        teacher2.setNext(teacher3);
        //teacher3.setNext(teacher4);
        //提交请求 
        teacher1.handleRequest(8);
    }
}
//抽象处理者:领导类
abstract class Leader
{
    private Leader next;
    public void setNext(Leader next)
    {
        this.next=next; 
    }
    public Leader getNext()
    { 
        return next; 
    }   
    //处理请求的方法
    public abstract void handleRequest(int LeaveDays);       
}
//具体处理者1:班主任类
class ClassAdviser extends Leader
{
    public void handleRequest(int LeaveDays)
    {
        if(LeaveDays<=2) 
        {
            System.out.println("班主任批准您请假" + LeaveDays + "天。");       
        }
        else
        {
            if(getNext() != null) 
            {
                getNext().handleRequest(LeaveDays);             
            }
            else
            {
                  System.out.println("请假天数太多,没有人批准该假条!");
            }
        } 
    } 
}
//具体处理者2:系主任类
class DepartmentHead extends Leader
{
    public void handleRequest(int LeaveDays)
    {
        if(LeaveDays<=7) 
        {
            System.out.println("系主任批准您请假" + LeaveDays + "天。");       
        }
        else
        {
            if(getNext() != null) 
            {
                  getNext().handleRequest(LeaveDays);             
            }
            else
            {
                System.out.println("请假天数太多,没有人批准该假条!");
           }
        } 
    } 
}
//具体处理者3:院长类
class Dean extends Leader
{
    public void handleRequest(int LeaveDays)
    {
        if(LeaveDays<=10) 
        {
            System.out.println("院长批准您请假" + LeaveDays + "天。");       
        }
        else
        {
              if(getNext() != null) 
            {
                getNext().handleRequest(LeaveDays);             
            }
            else
            {
                  System.out.println("请假天数太多,没有人批准该假条!");
            }
        } 
    } 
}
//具体处理者4:教务处长类
class DeanOfStudies extends Leader
{
    public void handleRequest(int LeaveDays)
    {
        if(LeaveDays<=20) 
        {
            System.out.println("教务处长批准您请假"+LeaveDays+"天。");       
        }
        else
        {
              if(getNext()!=null) 
            {
                getNext().handleRequest(LeaveDays);          
            }
            else
            {
                  System.out.println("请假天数太多,没有人批准该假条!");
            }
        } 
    } 
}

Program results are as follows: 

院长批准您请假8天。

If you add a Provost class, students may grant leave for 20 days, it is also very simple, as follows: 

//具体处理者4:教务处长类
class DeanOfStudies extends Leader
{
    public void handleRequest(int LeaveDays)
    {
        if(LeaveDays<=20)
        {
            System.out.println("教务处长批准您请假"+LeaveDays+"天。");
        }
        else
        {
            if(getNext()!=null)
            {
                getNext().handleRequest(LeaveDays);
            }
            else
            {
                System.out.println("请假天数太多,没有人批准该假条!");
            }
        }
    }
}

Application of scene modes

Front have been told about the structure and characteristics of the chain of responsibility pattern, the following describes the application scenario, the chain of responsibility pattern is commonly used in the following situations.

  1. A plurality of object can process the request, which object is automatically determined by the request processing runtime.
  2. Dynamically processing request specifies a set of objects, or add new processor.
  3. Without explicitly request handlers, submit requests to a plurality of handlers.

Extended mode

The following two cases duty chain.

  1. Pure chain of responsibility pattern: a request must be received by one handler object and a specific handler to process a request can only take one of two actions: their own treatment (responsibility); passing the buck to treatment under the house.
  2. Impure duty chain: allowed on a specific object handler to assume responsibility for a part of the case where the request is then passed to the next home responsibilities remaining, and a final request can not be received by any receiver-side object.
Published 136 original articles · won praise 6 · views 1546

Guess you like

Origin blog.csdn.net/weixin_42073629/article/details/104437837