Responsibility chain design mode

Responsibility chain design mode

Tip: this material is only for personal learning reference, not as a systematic learning process, please pay attention to identification!!!

I What is the chain of responsibility?

Baidu Encyclopedia Description:
Responsibility chain model is a behavior model of objects. In the responsibility chain model, many objects are connected by each object's reference to its next family to form a chain. Requests are passed along the chain until an object in the chain decides to process the request. The client issuing the request does not know which object in the chain will eventually process the request, which makes the system dynamically reorganize and allocate responsibilities without affecting the client.

advantage
Let each class focus more on its own responsibilities.
Make the coding process clearer.
Reduce coupling. Reduces code complexity
It is more convenient to add or modify

shortcoming
Processing is delayed
There is no guarantee that the request will be processed
Long responsibility chain will affect system performance
Inconvenient for code debugging

The responsibility chain model mainly includes, for example, the following two roles:

Handler: the interface that handles the request. It is generally designed as an abstract class with abstract request processing methods. To facilitate inheritance by different detailed processors. So as to realize the detailed request processing method. In addition, because the next home of each request handler is still a handler, the abstract handler itself also contains its own reference (success) as its reference to the next home, so as to chain the handlers into a chain;

ConcreteHandler: subclass of the abstract handler. Able to handle user requests. It implements the request processing method defined in the abstract handler.

It is necessary to infer when processing the request in detail if it has the corresponding processing permission. Then deal with it; Otherwise. It forwards the request to the successor. So that later processors can handle it.

In the responsibility chain model, each request handler object is connected by its reference to its next home to form a request processing chain.

The request will continue along this chain. Until a request handler in the chain can process the request. Actually. The client issuing the request does not know which request handler in the chain will process the request. This enables the system to dynamically organize the chain and assign responsibilities again without affecting the client.

II Leave approval Demo

Business requirements:

Less than 3 days: approved by the project manager
3-5 days: approved by Department Manager
More than 5 days: approved by the general manager

Code implementation:

package responsibility;

/**
 * Responsibility chain processor abstract class
 */
public abstract class Handler {

    /**
     * Next processing request object
     */
    protected Handler successor = null;

    /**
     * Gets the next processing request object
     */
    public Handler getSuccessor() {
        return successor;
    }

    /**
     * Set next processing request object
     */
    public void setSuccessor(Handler successor) {
        this.successor = successor;
    }


    /**
     * @param user Personnel asking for leave
     * @param days Leave days
     * @return Approval results
     */
    public abstract String handleFHolidayRequest(String user, int days);
}
package responsibility;

/**
 * Specific handler role - Project Manager
 */
public class ProjectManager extends Handler {
    @Override
    public String handleFHolidayRequest(String user, int days) {
        String temp = "Project manager approval==> Yes%s Leave application for,Leave days are:%d";
        //Less than 3 days: approved by the project manager
        if (days < 3) {
            temp = String.format(temp, user, days);
        } else {
            // If it exceeds 3 days, it will be transferred to a higher-level person for processing
            if (getSuccessor() != null) {
                return getSuccessor().handleFHolidayRequest(user, days);
            }
        }
        return temp;
    }
}

package responsibility;

/**
 * Specific handler role - Department Manager
 */
public class DeptManager extends Handler {
    @Override
    public String handleFHolidayRequest(String user, int days) {
        String temp = "Department manager approval==> Yes%s Leave application for,Leave days are:%d";
        //3-5 days: approved by Department Manager
        if (3 <= days && days <= 5) {
            temp = String.format(temp, user, days);
        } else {
            // For more than 5 days, it will be transferred to a higher-level person for processing
            if (getSuccessor() != null) {
                return getSuccessor().handleFHolidayRequest(user, days);
            }
        }
        return temp;
    }
}

package responsibility;

/**
 * Specific handler role - General Manager
 */
public class GeneralManager extends Handler {
    @Override
    public String handleFHolidayRequest(String user, int days) {
        String temp = "General manager approval==> Yes%s Leave application for,Leave days are:%d";
        //More than 5 days: approved by the general manager
        if (days > 5) {
            temp = String.format(temp, user, days);
        } else {
            // The general manager can approve it
            if (getSuccessor() != null) {
                return getSuccessor().handleFHolidayRequest(user, days);
            }
        }
        return temp;
    }
}

package responsibility;

/**
 * Leave request
 * Business requirements:
 * 3 Less than days: approved by the project manager
 *   3-5 Day: approved by Department Manager
 * 5 More than days: approved by the general manager
 */
public class Client {
    public static void main(String[] args) {
        // Assembly responsibility chain
        Handler generalHandler = new GeneralManager();
        Handler deptHandler = new DeptManager();
        Handler projectHandler = new ProjectManager();
        projectHandler.setSuccessor(deptHandler);
        deptHandler.setSuccessor(generalHandler);

        // Start test
        System.out.println(projectHandler.handleFHolidayRequest("Zhang San", 2));
        System.out.println(projectHandler.handleFHolidayRequest("Li Si", 5));
        System.out.println(projectHandler.handleFHolidayRequest("Wang Wu", 16));
    }
}

Added by supergrover1981 on Sun, 02 Jan 2022 19:01:28 +0200