Chain of Responsibility pattern small test

  Chain of Responsibility pattern is a very easy to understand, easy to use mode.

  Please indicate the source: http: //www.cnblogs.com/zrtqsk/p/3812454.html, thank you!

 

I. INTRODUCTION

  Let's look at the "grind design pattern" is defined on the chain of responsibility pattern - make more than one object a chance to handle the request, thus avoiding the coupling relationship between the sender and recipient of the request. These objects together into a chain, and pass the request along the chain until an object handles it so far.

  Chain of Responsibility pattern is the essence - separation of duties and dynamic combination.

  Chain of Responsibility pattern is used to solve any problems? When we have more than one object can handle the same requests, but by the concrete which object to process the request is determined dynamically at run time, this time using the Chain of Responsibility pattern. The core chain of responsibility pattern is a request Handler abstract class that holds a pointer to the next request for a quote. When it meets certain conditions, by their own to deal with, otherwise, let the next request to deal with. It means a processor holds a reference to another processor, if they can not handle, let another processor. We can see that, in essence, the chain of responsibility pattern is more like the strategy pattern, but also to achieve a multiplexing method, but it is more follow-up treatment program.

 

 

Second, the structure

Handler: responsibilities defined interfaces to define determination process handlerRequest processing request (), while a reference point to a next process's successor.

ConcreteHandler: realization of duties classes. Processing requests implemented within its purview. Without treatment, it is forwarded to the subsequent handler.

Client: call duty interfaces, submit a request like Handler.

 

Third, I realized

  Assume that students need to leave, if less than three days off, handled by counselors, if false, need to be addressed by the Dean of more than three days, if more than seven days off, need to be addressed by the school principal. However, students who do not know they need to leave treatment, only know to look for counselors. Faced with this situation, how to achieve it?

1, first of all we need a request class, a parent class of the request, the request package common attributes. as follows:

package chain_of_responsibility.me;

public class Request {

    private String type; // 类型

    private String name;    //请求者姓名

    private String description; // 描述

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Request(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

 

 2, it inherits leave requests, as follows:

package chain_of_responsibility.me;

/*
 * 请假请求
 */
public class RequestLeave extends Request {

    public static final String LEAVE_TYPE = "ask_for_leave";

    private int days;

    public RequestLeave() {
        super(LEAVE_TYPE);
    }

    public int getDays() {
        return days;
    }

    public void setDays(int datys) {
        this.days = datys;
    }

}

 

 3, then, our focus is an abstract class processor, to process the request, as follows:

package chain_of_responsibility.me;

public abstract class Handler {

    protected Handler successor;

    public void setSuccessor(Handler successor) {
        this.successor = successor;
    }
    
    protected abstract String handlerRequst(Request request);
    
}

4, the next step is the implementation class processor, three processors - counselor, dean, principal, as follows:

Package chain_of_responsibility.me; 

/ * 
 * facilitator 
 * / 
public  class HandlerCounsellor the extends Handler { 

    @Override 
    protected String handlerRequst (the Request Request) { 

        IF (. request.getType () the equals (RequestLeave.LEAVE_TYPE)) { // if the leave request, before processing 

            return handlerLeaveRequest (request); 

        } the else { 
            
            return "facilitator unable to process the request!" ; 
            
        } 
    } 

    Private String handlerLeaveRequest (the request request) { 

        String returnStr = "" ; 

        RequestLeave rl is an= (RequestLeave) Request; 

        IF (rl.getDays () <=. 3) { // once in 3 days, all agree 

            returnStr = "counselors agreed" + rl.getName () + "to leave!" ; 

        } the else { 

            IF (by successor =! null ) { 

                returnStr = successor.handlerRequst (Request); 

            } the else { 

                returnStr = "superiors not, are unable to process!" ; 

            } 
        } 

        return returnStr; 

    } 
}

 

Package chain_of_responsibility.me; 

/ * 
 * Dean 
 * / 
public  class HandlerDean the extends Handler { 

    @Override 
    protected String handlerRequst (the Request Request) { 

        IF (. request.getType () the equals (RequestLeave.LEAVE_TYPE)) { // if the leave request, before processing 

            return handlerLeaveRequest (request); 

        } the else { 
            
            return "Dean unable to process the request!" ; 
            
        } 
    } 

    Private String handlerLeaveRequest (the request request) { 

        String returnStr = "" ; 

        RequestLeave rl is an= (RequestLeave) Request; 

        IF (rl.getDays () <=. 7) { // Once within seven days, all agree 

            returnStr = "Dean agreed" + rl.getName () + "to leave!" ; 

        } the else { 

            IF (by successor =! null ) { 

                returnStr = successor.handlerRequst (Request); 

            } the else { 

                returnStr = "superiors not, are unable to process!" ; 

            } 
        } 

        return returnStr; 

    } 
}

 

Package chain_of_responsibility.me; 

/ * 
 * Principal 
 * / 
public  class HandlerPresident the extends Handler { 

    @Override 
    protected String handlerRequst (the Request Request) { 

        IF {(request.getType () the equals (RequestLeave.LEAVE_TYPE).) // if leave request before processing 

            return handlerLeaveRequest (request); 

        } the else { 

            return "principal unable to process the request!" ; 

        } 
    } 

    Private String handlerLeaveRequest (the request request) { 

        String returnStr = "" ; 

        RequestLeave rl is an= (RequestLeave) Request; 

        returnStr = "principals agreed" + rl.getName () + "leave of absence!"; // do not ask for several days, all agree! 

        return returnStr; 

    } 
}

4, OK, the following test:

Package chain_of_responsibility.me; 

public  class Client { 

    public  static  void main (String [] args) { 

        Handler Counselor = new new HandlerCounsellor (); // counselor 

        Handler Dean = new new HandlerDean (); // Dean 

        Handler President = new new HandlerPresident ( ); // principal 

        // assembling processing chain 
        counsellor.setSuccessor (Dean); 

        dean.setSuccessor (president); 

        // . 3 different requests 
        RequestLeave REQUEST1 = new new RequestLeave();

        RequestLeave request2 = new RequestLeave();

        RequestLeave request3 = new RequestLeave();

        request1.setDays(2);

        request1.setName("张三");

        request2.setDays(5);

        request2.setName("李四");

        request3.setDays(9);

        request3.setName("王五");

        // 请求
        System.out.println(counsellor.handlerRequst(request1));
        
        System.out.println(counsellor.handlerRequst(request2));
        
        System.out.println(counsellor.handlerRequst(request3));

    }
}

 

5, the following results:

Joe Smith's counselor agreed to leave! 
Dean John Doe agreed to leave! 
Five principals agreed to leave the king!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Reproduced in: https: //www.cnblogs.com/zrtqsk/p/3812454.html

Guess you like

Origin blog.csdn.net/weixin_33757609/article/details/94151779