Behavioral Mode-Chain of Responsibility Mode

Video explanation link (under review ...)

1. Definition

  In order to avoid the coupling of the request sender and multiple request processors, all the request processors are linked into a chain by remembering the reference of the next object of the previous object; when a request occurs, the request can be followed This chain passes until an object handles it. The most important point in the chain of responsibility model is that after the client submits the request, the request is passed down the chain until a processor processes it, where the client does not need to care which processor handles its request, anyway there is always a processing The person will process its request. Neither the receiver nor the sender has explicit information about each other, and the receiver does not know the structure in the chain of responsibility. So the chain of responsibilities can simplify the interconnection of objects. They only need to save a reference to their successors, not all the candidates. At the same time, it is possible to add or change the structure of a processing request anytime, anywhere, and even change the order of the processors, which increases the flexibility of the system. Processing flexibility is increased, but sometimes a request may not be processed anyway, and it will be placed at the end of the chain. This is both the advantage and disadvantage of the responsibility chain.

2. Structure and implementation

2.1 Structure

  The responsibilities chain model mainly includes the following roles: abstract handler (Handler) role: defines an interface for processing requests, including abstract processing methods and a subsequent connection; concrete handler (Concrete Handler) role: implements abstract handler processing methods, judgment Whether this request can be processed, if it can be processed, otherwise it will be transferred to its successor; Client role: Create a processing chain and submit the request to the specific processor object at the head of the chain, it does not Care about processing details and request delivery process. The UML diagram of the chain of responsibility is as follows:

 

 

 2.2 Implementation

  If the student is required to leave less than or equal to 2 days, the class teacher can approve; less than or equal to 7 days, the department head can approve; less than or equal to 10 days, the dean can approve; other cases are not approved; this example is suitable for using the chain of responsibility mode . The flow chart is as follows:

 

   Define a leader class (Leader), which is an abstract handler, which contains a pointer next to the next leader and an abstract processing method handleRequest (int LeaveDays) that handles fake strips; then, define the class leader class (ClassAdviser), department head Class (DepartmentHead) and Dean class (Dean), they are subclasses of abstract processors, and are concrete processors. They must implement the handleRequest (int LeaveDays) method of the parent class according to their own power. Hand it over to the next specific processor until the end; the client class is responsible for creating the processing chain and handing the fake strip to the specific processor at the head of the chain (class teacher). The class diagram is as follows:

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

class ClassAdviser extends Leader
{
    public void handleRequest(int LeaveDays)
    {
        if(LeaveDays<=2) 
        {
            System.out.println ( "Class teacher approves you for leave" + LeaveDays + "Day." );        
        } 
        Else 
        { 
            if (getNext ()! = Null ) 
            { 
                getNext (). HandleRequest (LeaveDays);              
            } 
            else 
            { 
                  System.out. println ( "Too many days of leave, no one approves the leave!" ); 
            } 
        } 
    } 
} 

class DepartmentHead extends Leader 
{ 
    public  void handleRequest ( int LeaveDays) 
    { 
        if(LeaveDays <= 7 ) 
        { 
            System.out.println ( "Department Director approves you for leave" + LeaveDays + "Day." );        
        } 
        Else 
        { 
            if (getNext ()! = Null ) 
            { 
                  getNext (). HandleRequest (LeaveDays) ;              
            } 
            else 
            { 
                System.out.println ( "Too many days for leave, no one approves the leave!" ); 
           } 
        } 
    } 
} 

class Dean extends Leader 
{ 
    public  void handleRequest ( intLeaveDays) 
    { 
        if (LeaveDays <= 10 ) 
        { 
            System.out.println ( "The dean approves your leave" + LeaveDays + "Day." );        
        } 
        Else 
        { 
              if (getNext ()! = Null ) 
            { 
                getNext (). handleRequest (LeaveDays);              
            } 
            else 
            { 
                  System.out.println ( "There are too many days for leave, no one has approved the leave!" ); 
            } 
        } 
    } 
}

 

3. Advantages and disadvantages and expansion

3.1 Advantages

  Reduce the coupling. It decouples the sender and receiver of the request; the object is simplified. The object does not need to know the structure of the chain; enhance the flexibility of assigning responsibilities to the object. By changing the members in the chain or mobilizing their order, it is possible to add or delete responsibilities dynamically; it is convenient to add new request processing classes.

 3.2 Disadvantages

  There is no guarantee that the request will be received; the system performance will be affected to some extent, and it is not convenient when debugging the code; it may cause loop calls; it may not be easy to observe the characteristics of the runtime, which hinders debugging.

 3.3 Extension

  The chain of responsibility mode has the following two situations: One is the pure chain of responsibility mode: a request must be received by a processor object, and a specific processor can only use one of the following two actions to process a request: Handle it yourself (take responsibility); push responsibility to the next house; second, impure chain of responsibility model: allow a specific processor object to take part of the responsibility of the request and then pass the remaining responsibility to the next house Situation, and a request may not be received by any receiving end object.

 

Reference link: https://wenku.baidu.com/view/04cc624d6bd97f192379e919.html

     https://max.book118.com/html/2017/0711/121752783.shtm

     http://c.biancheng.net/view/1383.html

     https://www.cnblogs.com/cxxjohnson/p/6403849.html

     https://blog.csdn.net/m0_37691414/article/details/80672007

     https://blog.csdn.net/qq_38005943/article/details/82147987

     https://www.jianshu.com/p/85801c01e05c

 

Guess you like

Origin www.cnblogs.com/kyl626/p/12700412.html