Design mode: responsibility chain mode

Keywords: Java less

The objects that can handle the same kind of requests form a chain, and the submitted requests are passed along the chain. The objects in the chain judge whether they can handle the request one by one

If it can, it will be processed; otherwise, it will be passed to the next object.

For example: the company's administrative approval process, the game of fighting against landlords, relay sports in track and field events, etc. All of them are the application of responsibility chain mode.

The responsibility chain may be a straight line, a ring chain or a part of a tree structure.

Roles involved in the responsibility chain:

Abstract Handler: defines an interface for processing requests. You can define a method to return a reference to the next object.

Concrete handler: after receiving the request, the concrete handler can process the request or pass the request to the next family.

Example:

Simulate a leave processing flow

  

/**
* Leader
* abstract class
*/
public abstract class Leader {
//Subclass implementation
protected String name;
//Next responsibility chain object
protected Leader next;

//Processing request abstraction
abstract void HandleLeaveNotes(LeaveNote note);

public Leader(String name) {
this.name = name;
}

public Leader(String name, Leader next) {
this.name = name;
this.next = next;
}

public String getName() {
return name;
}

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

public Leader getNext() {
return next;
}

public void setNext(Leader next) {
this.next = next;
}
}

  

**
 * Written request for leave
 */
public class LeaveNote {

    private String departMent;

    private String name;

    private Integer days;

    private String reason;

    //...get set
}

  

/**
* Departmental leadership
*/
public class DepartManager extends Leader {


public DepartManager(String name) {
super(name);
}

@Override
void HandleLeaveNotes(LeaveNote note) {
//If it is less than 3 days, the Department Manager has authority
if(note.getDays()<3){
System.out.println("Approval slip: "+ note.getName());
}else{
//Otherwise, transfer to superior leaders
this.next.HandleLeaveNotes(note);
}
}
}

  

/**
* manager
*/
public class Manager extends Leader {
public Manager(String name) {
super(name);
}

@Override
void HandleLeaveNotes(LeaveNote note) {
//If more than 3 days and less than 7 days, the manager has authority
if (note.getDays() >= 3 && note.getDays() <= 7) {
System.out.println("Approval slip: " + note.getName());
} else {
//Otherwise, transfer to the boss
this.next.HandleLeaveNotes(note);
}
}
}

  

/**
* Boss
*/
public class Boss extends Leader {

public Boss(String name) {
super(name);
}

@Override
void HandleLeaveNotes(LeaveNote note) {
System.out.println("Approval slip: "+ note.getName());
}
}

  

public class Client {
    public static void main(String[] args) {
        //
        LeaveNote note = new LeaveNote();
        note.setDays(5);
//        note.setDays(10);
//        note.setDays(20);
        
        note.setName("tom");

        Leader a = new DepartManager("Departmental leadership");
        Leader b = new Manager("General manager");
        Leader c = new DepartManager("Boss");

        //Responsibility chain relationship
        a.setNext(b);
        b.setNext(c);
        //Handling leave business
        a.HandleLeaveNotes(note);
    }
}

The responsibility chain model can decouple the code well when dealing with this multi-level business. When you want to add a new responsibility processing object, you only need to add the responsibility processing object. You don't need to change the original operator code, let alone write if..else

Posted by kostasls on Fri, 06 Dec 2019 07:06:37 -0800