标签:name 处理 Approver request 模式 processRequest public 职责
职责链模式:
OA系统采购审批需求
传统解决方案:
传统方案解决OA审批系统问题分析:
职责链模式基本介绍
职责链模式原理类图
职责链模式解决OA采购审批
代码实现
package com.sky.responsebility;
// 处理者 public abstract class Approver {
Approver approver; //下一个处理者 String name; //名字(具体是 校长,系主任,还是。。。)
// 构造方法 public Approver(String name){
this.name = name; }
//下一个处理者 public void setApprover(Approver approver) { this.approver = approver; }
//处理审批请求的方法,得到一个请求,处理是子类完成,因此该方法做成抽象(因为是要具体的子类去完成请求的处理) public abstract void processRequest(PurchaseRequest request); }
|
package com.sky.responsebility;
public class DepartmentApprover extends Approver{
//构造器 public DepartmentApprover(String name) { super(name); }
@Override public void processRequest(PurchaseRequest request) { if (request.getPrice() <= 5000){ System.out.println(" 请求编号 id = " + request.getId() + " 被 " + this.name + " 处理"); }else { // 如果他处理不了,就让他的后继者进行处理 approver.processRequest(request); } } }
|
package com.sky.responsebility;
public class CollageApprover extends Approver {
//构造器 public CollageApprover(String name) { super(name); }
@Override public void processRequest(PurchaseRequest request) {
if (request.getPrice() > 5000 && request.getPrice() <= 10000) { System.out.println(" 请求编号 id = " + request.getId() + " 被 " + this.name + " 处理"); }else { // 如果他处理不了,就让他的后继者进行处理 approver.processRequest(request); } } }
|
package com.sky.responsebility;
/** * 副校长 */ public class ViceSchoolMasterApprover extends Approver {
//构造器 public ViceSchoolMasterApprover(String name) { super(name); }
@Override public void processRequest(PurchaseRequest request) {
if (request.getPrice() > 10000 && request.getPrice() <= 30000) { System.out.println(" 请求编号 id = " + request.getId() + " 被 " + this.name + " 处理"); }else { // 如果他处理不了,就让他的后继者进行处理 approver.processRequest(request); } } }
|
package com.sky.responsebility;
public class SchoolMasterApprover extends Approver {
//构造器 public SchoolMasterApprover(String name) { super(name); }
@Override public void processRequest(PurchaseRequest request) {
if (request.getPrice() > 30000) { System.out.println(" 请求编号 id = " + request.getId() + " 被 " + this.name + " 处理"); }else { // 如果他处理不了,就让他的后继者进行处理 approver.processRequest(request); } } }
|
package com.sky.responsebility;
/** * 请求类 */ public class PurchaseRequest {
private int type = 0; // 请求类型 private float price = 0.0f; // 请求的金额 private int id = 0; // 默认当前是第一个处理者
// 构造器 public PurchaseRequest(int type, float price, int id) { this.type = type; this.price = price; this.id = id; }
// 添加对应的get方法 public int getType() { return type; }
public float getPrice() { return price; }
public int getId() { return id; } }
|
package com.sky.responsebility;
/** * 客户端 */ public class Client {
public static void main(String[] args) {
// 创建一个请求
// 可以改变金额进行测试 PurchaseRequest purchaseRequest = new PurchaseRequest(1, 30001, 1);
//创建相关的审批人、 Approver departmentApprover = new DepartmentApprover("张主任"); Approver collageApprover = new CollageApprover("李院长"); Approver viceSchoolMasterApprover = new ViceSchoolMasterApprover("王副校长"); Approver schoolMasterApprover = new SchoolMasterApprover("童校长");
//需要将各个审批级别的下一个处理者设置好(处理人构成环形:【否则会报NullPointerException】) departmentApprover.setApprover(collageApprover); collageApprover.setApprover(viceSchoolMasterApprover); viceSchoolMasterApprover.setApprover(schoolMasterApprover); schoolMasterApprover.setApprover(departmentApprover);
//进行请求的处理 departmentApprover.processRequest(purchaseRequest);
} }
|
运行结果:
请求编号 id = 1 被 童校长 处理
|
(职责链模式有在SpringMVC框架中使用)
职责链模式的注意事项
标签:name,
处理,
Approver,
request,
模式,
processRequest,
public,
职责
From: https://www.cnblogs.com/dongyaotou/p/16938027.html