一 责任链模式
1.1 概述
在进行请假申请,财务报销申请,需要走部门领导审批,技术总监审批,大领导审批等判断环节。存在请求方和接收方耦合性太强,代码会比较臃肿,不利于扩展和维护。
1.2 责任链模式
针对上面,使用责任链模式,将请求方和接收方的业务进行解耦,客户端发送一个请求,由一个抽象的处理请求的类 Handler 来接受这个请求,而具体怎么处理请求,就由 Handler 的子类来完成处理请求方法的实现,每个子类满足条件则自己处理,不满足调用下一个处理子类进行处理,以此类推,形成一条处理的链条。
1.3 代码实现
1.采购请求类
package com.ljf.day0905;/*** @ClassName: ClientRequest* @Description: TODO* @Author: admin* @Date: 2023/09/05 21:55:41 * @Version: V1.0**/
public class ClientRequest {/* 请求类型 */private int type = 0;/* 请求金额 */private float price = 0.0f;/* 请求编号 */private int id = 0;/*** 全参构造** @param type* @param price* @param id*/public ClientRequest(int type, float price, int id) {this.type = type;this.price = price;this.id = id;}public int getType() {return type;}public float getPrice() {return price;}public int getId() {return id;}}
2.审批抽象类
package com.ljf.day0905;/*** @ClassName: Approver* @Description: TODO* @Author: admin* @Date: 2023/09/05 21:56:49 * @Version: V1.0**/
public abstract class Approver {/* 审批人姓名 */protected String name;/* 下一个审批人 */protected Approver approver;/*** 创建审批人要指定姓名** @param name*/public Approver(String name) {this.name = name;}/*** 指定下一个审批人** @param approver*/public void setApprover(Approver approver) {this.approver = approver;}/*** 抽象的审批方法** @param purchaseRequest*/public abstract void approve(ClientRequest purchaseRequest);}
3.子类来继承抽象审批类
3.1 DeaprtLeader
package com.ljf.day0905;/*** @ClassName: DeaprtLeader* @Description: TODO* @Author: admin* @Date: 2023/09/05 21:58:10 * @Version: V1.0**/
public class DeaprtLeader extends Approver {/*** 创建审批人要指定姓名** @param name*/public DeaprtLeader(String name) {super(name);}/*** 教学主任审批逻辑** @param purchaseRequest*/public void approve(ClientRequest purchaseRequest) {if (purchaseRequest.getPrice() <= 5000) {System.out.println("请求编号:" + purchaseRequest.getId() + "被" +this.name + "处理");} else {approver.approve(purchaseRequest);}}}
2.BossLeader
package com.ljf.day0905;/*** @ClassName: BossLeader* @Description: TODO* @Author: admin* @Date: 2023/09/05 21:59:23 * @Version: V1.0**/
public class BossLeader extends Approver{/*** 创建审批人要指定姓名** @param name*/public BossLeader(String name) {super(name);}/*** 院长审批逻辑** @param purchaseRequest*/public void approve(ClientRequest purchaseRequest) {if (purchaseRequest.getPrice() > 5000 && purchaseRequest.getPrice() <=10000) {System.out.println("请求编号:" + purchaseRequest.getId() + "被" +this.name + "处理");} else {approver.approve(purchaseRequest);}}}
3.VicePrincipal
package com.ljf.day0905;import com.sun.security.ntlm.Client;/*** @ClassName: VicePrincipal* @Description: TODO* @Author: admin* @Date: 2023/09/05 22:09:25 * @Version: V1.0**/public class VicePrincipal extends Approver {/*** 创建审批人要指定姓名** @param name*/public VicePrincipal(String name) {super(name);}/*** 副校长审批逻辑** @param purchaseRequest*/public void approve(ClientRequest purchaseRequest) {if (purchaseRequest.getPrice() > 10000 ) {System.out.println("请求编号:" + purchaseRequest.getId() + "被" +this.name + "处理");} else {approver.approve(purchaseRequest);}}}
4.测试
package com.ljf.day0905;/*** @ClassName: TestZe* @Description: TODO* @Author: admin* @Date: 2023/09/05 22:00:19 * @Version: V1.0**/
public class TestZe {public static void main(String[] args) {// 创建采购请求ClientRequest purchaseRequest = new ClientRequest(1, 33310000, 1);
// 创建审批人Approver departmentApprover = new DeaprtLeader("团主任");Approver dean = new BossLeader("方院长");Approver vicePrincipal = new VicePrincipal("磊副校长");// Approver principal = new Principal("喜校长");
// 设置下一个审批人departmentApprover.setApprover(dean);
// 这里要形成一个环链,避免如果 30000 金额以下的请求
// 直接交给校长处理,会出现空指针
// 当然,如果程序规定只能从主任开始处理
// 一层一层最后到校长处理,形成一个单链,这里就不用了设置了dean.setApprover(vicePrincipal);vicePrincipal.setApprover(departmentApprover);// principal.setApprover(departmentApprover);
// 测试:入口departmentApprover.approve(purchaseRequest);}
}
5.结果
责任链模式(Chain of Responsibility Pattern)_Myovlmx的博客-CSDN博客