Design Pattern_Chain Of Responsibility Pattern

Chain Of Responsibility The chain of responsibility model
mainly refers to the chain , and its core is also on the chain ; it is a chain processing line composed of multiple processors, and processing requests are passed along such a chain until until there is an object to handle the request.

Class Diagram:
Write picture description here

This is a demo that has implemented the chain of responsibility model. Some places are not very rigorous and I will point them out below.
ICar: is the interface of the request object.
Car: A specific request object, which defines the corresponding request status and request information.
Handler: abstract request processing class.
Start/Run/Stop: Specific request processing object, request corresponding request.

When a car object makes a request, if the request can be stopped, a chain transfer request is made, judging from the type, from Start to Run and then to Stop. When passed to the Stop class. Processing starts and can be stopped if necessary. This is a chain of request delivery.

When using the chain of responsibility model, you need to pay attention to:
1. Performance issues: Every time it is passed from the head of the chain to the end of the chain, if the chain is relatively long, there will be big performance problems.
2. Debugging: Debugging the chain of responsibility is not very convenient, as you will know if you use it.

Several methods that are very important in the chain of responsibility:
handlerMessage method: request processing method, used to determine whether the current request can be processed.
setNext: The method arranges the chain and sets the next processor.
level: Set the level you can handle. I have written it hard in the code, but in practice I can use a class encapsulation. It is relatively more flexible.
response (or echo): specific processing method. The way the names are called is a little different.

Code:

public interface ICar {

    public int getType();

    public String getRequest();
}
public class Car implements ICar {
    
    

    //定义一个级别描述现在的状况
    private int type = 0 ;

    private String request = "";

    public Car(int  _type, String _request){
        this.type = _type;

        switch (this.type) {
        case 1:
            this.request = "汽车的请求是" + _request;
            break;
        case 2:
            this.request = "汽车的请求是" + _request;
            break;
        case 3:
            this.request = "汽车的请求是" + _request;
            break;

        }
    }

    @Override
    public int getType() {
        return this.type;
    }

    @Override
    public String getRequest() {
        return this.request;
    }

}
public abstract class Handler {
    
    

    public final static int START_LEVEL_REQUEST = 1;

    public final static int RUN_LEVEL_REQUEST = 2;

    public final static int STOP_LEVEL_REQUEST = 3;

    //能处理的级别
    private int level = 0;

    private Handler nextHandler;

    //构造函数,每个类都要声明自己的处理级别.用于处理哪些请求
    public Handler(int _level){
        this.level = _level;
    }

    //判断请求是否属于你的
    public final void HandlerMessage(ICar _car){
        if(_car.getType() == this.level){
            this.response(_car);
        }else{
            if(this.nextHandler != null){
                this.nextHandler.HandlerMessage(_car);
            }else{
                System.out.println("没有后续环节了,车子已报废。");
            }
        }
    }

    /*
     * 如果不属于你的请求、你应该找到下一个环节
     * 汽车已经启动,在像启动环节询问的时候,应该告诉他你可以跑起来环节
     */
    public void setNext(Handler _handler){
        this.nextHandler = _handler;
    }

    //回应部分
    protected abstract void response(ICar _car); 

}
public class Start extends Handler {
    
    

    public Start() {
        super(Handler.START_LEVEL_REQUEST);
    }

    @Override
    protected void response(ICar _car) {
        System.out.println("-----------------汽车未启动,请示启动---------------- \n");
        System.out.println(_car.getRequest());
        System.out.println("-----------------汽车可以启动--------------- \n");
    }
}
public class Run extends Handler {
    
    

    public Run() {
        super(Handler.RUN_LEVEL_REQUEST);
    }

    @Override
    protected void response(ICar _car) {
        System.out.println("-----------------汽车已启动,请示跑路---------------- \n");
        System.out.println(_car.getRequest());
        System.out.println("-----------------汽车可以跑路--------------- \n");
    }
}
public class Stop extends Handler {
    
    

    public Stop() {
        super(Handler.STOP_LEVEL_REQUEST);
    }

    @Override
    protected void response(ICar _car) {
        System.out.println("-----------------汽车已跑路,请示停止---------------- \n");
        System.out.println(_car.getRequest());
        System.out.println("-----------------汽车可以停止--------------- \n");
    }
}
public class Client {

    public static void main(String args[]){
        Handler start = new Start();
        Handler run = new Run();
        Handler stop = new Stop();

        start.setNext(run);
        run.setNext(stop);

        ICar car = new Car(1,"我要启动汽车");

        ICar car1 = new Car(3,"我要停止汽车");

        start.HandlerMessage(car1);
        start.HandlerMessage(car);
    }
}

Under normal circumstances, the client class will be encapsulated and the start object will be returned directly. Just call it directly.

There is a usage scenario. When a user registers, one is a registered member user and the other is a registered guest user. You can use one entrance and handle them separately using a chain of responsibility approach.

Guess you like

Origin blog.csdn.net/my_God_sky/article/details/52567039