Design mode: responsibility chain mode

reference resources
Behavioral model (I): responsibility chain model

Responsibility chain model

Type: behavioral

In order to avoid coupling the request sender with multiple request processors, all request processors are connected into a chain by remembering the reference of the next object through the previous object; When a request occurs, it can be passed along the chain until an object processes it.

Role:

  1. Abstract Handler role: define an interface for processing requests, including abstract processing methods and a subsequent connection.
  2. Concrete Handler role: implement the processing method of the abstract handler to judge whether the request can be processed. If the request can be processed, process it. Otherwise, transfer the request to its successor.
  3. Client role: create a processing chain and submit a request to the specific handler object of the chain head. It does not care about the processing details and the transmission process of the request.

UML


Application scenario of pattern

  1. There are multiple objects that can handle a request, and which object handles the request is automatically determined by the runtime.
  2. You can dynamically specify a set of objects to process requests, or add new processors.
  3. When the request handler is not explicitly specified, the request is submitted to one of multiple handlers.

Package structure

Handler base class

package Responsibility chain model.handler;

public abstract class Handler {
    protected Handler nextHandler;
    public void setNextHandler(Handler nextHandler){
        this.nextHandler=nextHandler;
    }
    public Handler getNextHandler(){
        return this.nextHandler;
    }
    public abstract String handleReq(double reqMoney);
}

Processors with small to large permissions

package Responsibility chain model.handler;

public class SmallHandler extends Handler{
    private double permission = 1e3;

    @Override
    public String handleReq(double reqMoney) {
        if(reqMoney<=permission){
            System.out.println("Small leaders still have this ability");
            return "The little leader did it";
        }else{
            System.out.println("The small leader doesn't have this authority. Go to the middle leader");
            return this.getNextHandler().handleReq(reqMoney);
        }
    }
}
//
package Responsibility chain model.handler;

public class MediumHandler extends Handler{
    private double permission = 1e4;

    @Override
    public String handleReq(double reqMoney) {
        if(reqMoney<=permission){
            System.out.println("Chinese leaders still have this ability");
            return "The central leadership Office";
        }else{
            System.out.println("The middle leader doesn't have this authority. Go to the big leader");
            return this.getNextHandler().handleReq(reqMoney);
        }
    }
}
//
package Responsibility chain model.handler;

public class BigHandler extends Handler{
    private double permission = 1e5;

    @Override
    public String handleReq(double reqMoney) {
        if(reqMoney<=permission){
            System.out.println("Big leaders still have this ability");
            return "The big leader did it";
        }else{
            System.out.println("Big leaders don't have this authority. Go find bigger leaders");
            return this.getNextHandler().handleReq(reqMoney);
        }
    }
}
//
package Responsibility chain model.handler;

public class BiggerHandler extends Handler{
    private double permission = 1e6;

    @Override
    public String handleReq(double reqMoney) {
        if(reqMoney<=permission){
            System.out.println("Bigger leaders still have this ability");
            return "A bigger leadership office";
        }else{
            System.out.println("Why do you want so much money? Not approve");
            return "The money was not approved";
        }
    }
}
//

client

package Responsibility chain model.client;

import Responsibility chain model.handler.BigHandler;
import Responsibility chain model.handler.BiggerHandler;
import Responsibility chain model.handler.MediumHandler;
import Responsibility chain model.handler.SmallHandler;

public class Client {
    public static void main(String[] args) {
        BiggerHandler biggerHandler = new BiggerHandler();
        BigHandler bigHandler = new BigHandler();
        MediumHandler mediumHandler = new MediumHandler();
        SmallHandler smallHandler = new SmallHandler();
        //Set next first
        bigHandler.setNextHandler(biggerHandler);
        mediumHandler.setNextHandler(bigHandler);
        smallHandler.setNextHandler(mediumHandler);
        //Start processing
        for(double i=100;i<=1e6;i=i*10){
            double req = i+1;
            System.out.println("----The requested funding is"+req+"----");
            String resp= smallHandler.handleReq(req);
            System.out.println("In the end"+resp+'\n');
        }
    }
}

effect

----The amount requested is 101.0----
Small leaders still have this ability
 Finally, the small leader did it

----The amount requested is 1001.0----
The small leader doesn't have this authority. Go to the middle leader
 Chinese leaders still have this ability
 Finally, it was the central leadership office

----The amount requested is 10001.0----
The small leader doesn't have this authority. Go to the middle leader
 The middle leader doesn't have this authority. Go to the big leader
 Big leaders still have this ability
 Finally, the big leaders did it

----The requested funding is 100001.0----
The small leader doesn't have this authority. Go to the middle leader
 The middle leader doesn't have this authority. Go to the big leader
 Big leaders don't have this authority. Go find bigger leaders
 Bigger leaders still have this ability
 In the end, it's a bigger leader

----The requested funding is 10000001.0----
The small leader doesn't have this authority. Go to the middle leader
 The middle leader doesn't have this authority. Go to the big leader
 Big leaders don't have this authority. Go find bigger leaders
 Why do you want so much money? Not approve
 In the end, the money was not approved


Process finished with exit code 0

Mode extension

There are two situations in the responsibility chain mode:
1. Pure responsibility chain mode: a request must be received by a handler object, and a specific handler can only process a request by one of the following two behaviors: self processing (assuming responsibility); Put the blame on the next family.
2. Impure responsibility chain mode: it allows a specific handler object to transfer the remaining responsibility to the next home after assuming part of the responsibility of the request, and a request can not be received by any receiving end object.

Tags: Design Pattern

Posted on Sat, 20 Nov 2021 09:44:02 -0500 by jkarr