Chain of Responsibility Design Pattern

Definition: Connect objects that can handle the same type of request into a chain, and the submitted requests are passed along the chain, and the objects on the chain judge one by one whether they are capable of handling the request.

Process it if it can, pass it to the next object in the chain if it can't.


scenes to be used:

1. In java, a mechanism is a chain of responsibility model. A try can correspond to multiple catches. When one does not match, the next one will be found.

2. In javascript, the bubbling and capturing mechanism of events. In the JAVA language, the event processing adopts the observer pattern.

3. In servlet development, the chain processing of filters.

4. In struts2, the invocation of the interceptor is also a typical chain of responsibility pattern.


Scenes:

    In the company, the approval process for leave requests:

        1. If the number of days requested is less than 3 days, the director will approve

        2. If the number of days of leave is greater than 3 days, but less than 10 days, the manager will approve it

        3. If it is greater than or equal to 10 days and less than 30 days, the general manager will approve it

        4. If it is greater than or equal to 30 days, prompt rejection

Class Diagram:



The implementation is as follows:

Person asking for leave:

package com.gcxzflgl.chainOfResp;

/**
 * Encapsulate the basic information of leave
 * @author Administrator
 *
 */
public class LeaveRequest {
	private String empName;
	private int leaveDays;
	private String reason;
	
	
	public LeaveRequest(String empName, int leaveDays, String reason) {
		super();
		this.empName = empName;
		this.leaveDays = leaveDays;
		this.reason = reason;
	}
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public int getLeaveDays() {
		return leaveDays;
	}
	public void setLeaveDays(int leaveDays) {
		this.leaveDays = leaveDays;
	}
	public String getReason() {
		return reason;
	}
	public void setReason(String reason) {
		this.reason = reason;
	}
	
	
}

Leader:

package com.gcxzflgl.chainOfResp;

/**
 * abstract class
 * @author Administrator
 *
 */
public abstract class Leader {
	protected String name;
	protected Leader nextLeader; //The successor object on the responsibility chain
	
	public Leader(String name) {
		super();
		this.name = name;
	}
	
	//Set the successor object on the chain of responsibility
	public void setNextLeader(Leader nextLeader) {
		this.nextLeader = nextLeader;
	}
	
	
	/**
	 * The core business method for processing requests
	 * @param request
	 */
	public abstract void handleRequest(LeaveRequest request);
	
	
	
}

director:

package com.gcxzflgl.chainOfResp;

/**
 * director
 * @author Administrator
 *
 */
public class Director extends Leader {

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

	@Override
	public void handleRequest(LeaveRequest request) {
		if(request.getLeaveDays()<3){
			System.out.println("Employee: "+request.getEmpName()+" leave, days: "+request.getLeaveDays()+", reason: "+request.getReason());
			System.out.println("Director: "+this.name+", approved!");
		}else{
			if(this.nextLeader!=null){
				this.nextLeader.handleRequest(request);
			}
		}
	}

}

manager:

package com.gcxzflgl.chainOfResp;

/**
 * manager
 * @author Administrator
 *
 */
public class Manager extends Leader {

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

	@Override
	public void handleRequest(LeaveRequest request) {
		if(request.getLeaveDays()<10){
			System.out.println("Employee: "+request.getEmpName()+" leave, days: "+request.getLeaveDays()+", reason: "+request.getReason());
			System.out.println("Manager: "+this.name+", approved!");
		}else{
			if(this.nextLeader!=null){
				this.nextLeader.handleRequest(request);
			}
		}
	}

}

Deputy General Manager:

package com.gcxzflgl.chainOfResp;

/**
 * Deputy General Manager
 * @author Administrator
 *
 */
public class ViceGeneralManager extends Leader {

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

	@Override
	public void handleRequest(LeaveRequest request) {
		if(request.getLeaveDays()<20){
			System.out.println("Employee: "+request.getEmpName()+" leave, days: "+request.getLeaveDays()+", reason: "+request.getReason());
			System.out.println("Deputy general manager: "+this.name+", approved!");
		}else{
			if(this.nextLeader!=null){
				this.nextLeader.handleRequest(request);
			}
		}
	}

}

General manager:

package com.gcxzflgl.chainOfResp;

/**
 * General manager
 * @author Administrator
 *
 */
public class GeneralManager extends Leader {

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

	@Override
	public void handleRequest(LeaveRequest request) {
		if(request.getLeaveDays()<30){
			System.out.println("Employee: "+request.getEmpName()+" leave, days: "+request.getLeaveDays()+", reason: "+request.getReason());
			System.out.println("General Manager: "+this.name+", approved!");
		}else{
			System.out.println("Could it be"+request.getEmpName()+"I want to resign but ask for leave"+request.getLeaveDays()+"days!");
		}
	}

}

test:

package com.gcxzflgl.chainOfResp;

public class Client {
	public static void main(String[] args) {
		Leader a = new Director("Zhang San");
		Leader b = new Manager("Li Si");
		Leader b2 = new ViceGeneralManager("李小四");
		Leader c = new GeneralManager("Wang Wu");
		//Organize the relationship of the chain of responsibility objects
		a.setNextLeader(b);
		b.setNextLeader(b2);
		b2.setNextLeader(c);
		
		//Start the leave operation
		LeaveRequest req1 = new LeaveRequest("TOM", 15, "Go back to my hometown in the UK to visit relatives!");
		a.handleRequest(req1);
		
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325876734&siteId=291194637