java's responsibility chain pattern (big talk design pattern)

At present, many OA office automation software accelerate the office efficiency and simplify the process. I believe many of my friends have used it. Most of the bottom implementation processes of office software that I have learned are based on Activity or JBPM framework.

Today, I want to talk about a design mode similar to the process, the responsibility mode.

Look at the class diagram first

Dahua design pattern class diagram

After reading the class diagram, I don't know if you think the responsibility chain model is familiar. I think this mode is similar to the state mode. However, the author thinks that the responsibility mode is more flexible, and the process can be specified on the client side. The state pattern is that the process is dead at the time of implementation.

A brief look at the writer's Demo

/**
 * request
 */
public class Request {

    private int breakDay;

    private String requestName;

    public Request(int breakDay, String requestName) {
        super();
        this.breakDay = breakDay;
        this.requestName = requestName;
    }

    public int getBreakDay() {
        return breakDay;
    }

    public void setBreakDay(int breakDay) {
        this.breakDay = breakDay;
    }

    public String getRequestName() {
        return requestName;
    }

    public void setRequestName(String requestName) {
        this.requestName = requestName;
    }
}
/**
 * Abstract parent class
 */
public abstract class Handler {

    private Handler handler;

    public Handler(Handler handler) {
        super();
        this.handler = handler;
    }

    public abstract void handleRequest(Request request);

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }
}
/**
 * manager
 */
public class Manager extends Handler{

    public Manager(Handler handler) {
        super(handler);
    }

    @Override
    public void handleRequest(Request request) {
        if (request.getBreakDay() < 10) {
            System.out.println("The manager approved you to have a rest");
        } else {
            System.out.println("Manager has no authority to deal with");
            super.getHandler().handleRequest(request);
        }
    }

}
/**
 * general manager
 */
public class AllManager extends Handler {

    public AllManager(Handler handler) {
        super(handler);
    }

    @Override
    public void handleRequest(Request request) {
        if (request.getBreakDay() < 20) {
            System.out.println("The general manager approved you to have a rest");
        } else {
            System.out.println("The general manager has no right to deal with it");
            super.getHandler().handleRequest(request);
        }
    }

}
/**
 * Boss
 */
public class Boss extends Handler {

    public Boss(Handler handler) {
        super(handler);
    }

    @Override
    public void handleRequest(Request request) {
        System.out.println("BOSS You are authorized to take a rest. Be careful!");
    }

}
/**
 * client
 */
public class Test {

    public static void main(String[] args) {
        Request request = new Request(40, "Ask for leave and rest");
        Handler boos = new Boss(null);
        Handler allManager = new AllManager(boos);
        Handler manager = new Manager(allManager);
        manager.handleRequest(request);
    }
}

 

The output is

Manager has no authority to deal with
 The general manager has no right to deal with it
 BOSS approved you to have a rest. Be careful!

 

The above is the author's understanding of the responsibility chain mode, hoping to help the partners of learning responsibility company understand this mode.

Tags: Java REST

Posted on Tue, 05 May 2020 01:10:28 -0700 by xcandiottix