Responsibility chain model of design model

preface

As the name suggests, the Chain of Responsibility Pattern creates a chain of receiver objects for requests. This pattern gives the type of request and decouples the sender and receiver of the request. This type of design pattern belongs to behavioral pattern.

In this pattern, each recipient usually contains a reference to another recipient. If an object cannot process the request, it passes the same request to the next recipient, and so on.

Definition and characteristics of pattern

Definition of Chain of Responsibility mode: in order to avoid coupling the request sender with multiple request handlers, all request handlers are connected into a chain by remembering the reference of the next object through the previous object; when a request occurs, the request can be passed along the chain until an object processes it.

In the responsibility chain mode, the customer only needs to send the request to the responsibility chain, and does not need to care about the processing details of the request and the transmission process of the request. The request will be transmitted automatically. Therefore, the responsibility chain decouples the sender of the request from the handler of the request.

Advantages:

  • Reduce coupling. It decouples the sender and receiver of the request.
  • Simplifies objects. So that the object does not need to know the structure of the chain.
  • Enhance the flexibility of assigning responsibilities to objects. By changing the members in the chain or transferring their order, it is allowed to dynamically add or delete responsibilities.
  • It is convenient to add new request processing classes.

Disadvantages:

  • There is no guarantee that the request will be received.
  • The system performance will be affected to some extent, and it is inconvenient to debug the code, which may cause circular calls.
  • It may not be easy to observe the characteristics of the runtime, which hinders debugging.

Code implementation:

Create RequestMessage class:

public class RequestMessage {
    private int workNum;
    public RequestMessage(int workNum) {
        this.workNum = workNum;
    }
    public int getWorkNum() {
        return workNum;
    }
}

Create class Person (Abstract performer):

public abstract class Person {
    private Person person;
    public void setPerson(Person person) {
        this.person = person;
    }
    public Person getPerson() {
        return person;
    }
    public abstract void doRequest(RequestMessage request);
}

Three implementation classes (specific executors) Person_A, Person_B and Person_C:

public class Person_A extends Person {
    @Override
    public void setPerson(Person person) {
        super.setPerson(person);
    }
    @Override
    public void doRequest(RequestMessage request) {
        if(request.getWorkNum() < 1000){
            System.out.println("Person_A Completed the task!");
        }else {
            getPerson().doRequest(request);
        }
    }
}

public class Person_B extends Person {
    @Override
    public void setPerson(Person person) {
        super.setPerson(person);
    }
    @Override
    public void doRequest(RequestMessage request) {
        if(request.getWorkNum() < 3000){
            System.out.println("Person_B Completed the task!");
        }else {
            getPerson().doRequest(request);
        }
    }
}

public class Person_C extends Person {
    @Override
    public void setPerson(Person person) {
        super.setPerson(person);
    }
    @Override
    public void doRequest(RequestMessage request) {
        if(request.getWorkNum() > 3000){
            System.out.println("Person_C Completed the task!");
        }else {
            getPerson().doRequest(request);
        }
    }
}

Create Client class:

    @Test
    public void test(){
        RequestMessage request = new RequestMessage(200);
        Person_A a = new Person_A();
        Person_B b = new Person_B();
        Person_C c = new Person_C();

        a.setPerson(b);
        b.setPerson(c);
        c.setPerson(a);
        // Let c complete the request, but c can't complete it. Let a complete the task
        c.doRequest(request);
    }

Execution results:

 

Keywords: Java Design Pattern

Added by biohazardep on Tue, 04 Jan 2022 12:54:36 +0200