Design Pattern Eighteen, Chain of Responsibility Pattern

Chain of Responsibility mode: Allow multiple objects to have the opportunity to process the request, thereby avoiding the coupling relationship between the sender of the request and the receiver. Connect this object into a chain and pass the request along this chain until an object handles it.

The advantage is that neither the receiver nor the sender has clear information about each other, and the objects in the chain themselves do not know the structure of the chain. As a result, the responsibility chain can simplify the interconnection of objects. They only need to maintain a reference to their successor. It does not need to keep the references of all its candidate recipients.

Adding or modifying the structure of processing a request at any time enhances the flexibility of assigning responsibilities to objects.

It is very likely that a request will not be processed at the end of the chain, or it will not be processed because it is not properly configured.

//定义一个处理请示的接口
abstract public class Handler {
    
    
    protected Handler successor;

    //设置继任者
    public void setSuccessor(Handler successor) {
    
    
        this.successor = successor;
    }

    //处理请求的抽象方法
    public abstract void HandleRequest(int request);
}

public class ConcreteHandler1 extends Handler {
    
    
    @Override
    public void HandleRequest(int request) {
    
    
        if (request >= 0 && request < 10) {
    
                     //0到10,这里处理
            System.out.println(this.getClass().getName() + "处理请求" + request);
        } else if (this.successor != null) {
    
    
            this.successor.HandleRequest(request);      //转移到下一位
        }
    }
}

public class ConcreteHandler2 extends Handler {
    
    
    @Override
    public void HandleRequest(int request) {
    
    
        if (request >= 10 && request < 20) {
    
                     //10到20,这里处理
            System.out.println(this.getClass().getName() + "处理请求" + request);
        } else if (this.successor != null) {
    
    
            this.successor.HandleRequest(request);      //转移到下一位
        }
    }
}

public class ConcreteHandler3 extends Handler {
    
    
    @Override
    public void HandleRequest(int request) {
    
    
        if (request >= 20 && request < 30) {
    
                     //20到30,这里处理
            System.out.println(this.getClass().getName() + "处理请求" + request);
        } else if (this.successor != null) {
    
    
            this.successor.HandleRequest(request);      //转移到下一位
        }
    }

    public static void main(String[] args) {
    
    
        Handler handler1 = new ConcreteHandler1();
        Handler handler2 = new ConcreteHandler2();
        Handler handler3 = new ConcreteHandler3();

        handler1.setSuccessor(handler2);            //设置上下家
        handler2.setSuccessor(handler3);

        int[] Request = new int[]{
    
    2, 5, 14, 22, 18, 3, 27, 20};

        for (int num : Request) {
    
    
            handler1.HandleRequest(num);
        }
    }
}

Output

day16chainofresponsibility.ConcreteHandler1处理请求2
day16chainofresponsibility.ConcreteHandler1处理请求5
day16chainofresponsibility.ConcreteHandler2处理请求14
day16chainofresponsibility.ConcreteHandler3处理请求22
day16chainofresponsibility.ConcreteHandler2处理请求18
day16chainofresponsibility.ConcreteHandler1处理请求3
day16chainofresponsibility.ConcreteHandler3处理请求27
day16chainofresponsibility.ConcreteHandler3处理请求20

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/weixin_45401129/article/details/114629553