尚硅谷设计模式学习(24)---[职责链模式(Chain of Responsibility Pattern)]
Posted 小智RE0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尚硅谷设计模式学习(24)---[职责链模式(Chain of Responsibility Pattern)]相关的知识,希望对你有一定的参考价值。
尚硅谷传送门==>B站尚硅谷Java设计模式
❤❤❤感谢尚硅谷❤❤❤
最近开始计划学习一下设计模式了,加油!!!
情景引入
首先看一个案例
学校OA系统的采购审批项目:需求是
- 1 采购员采购教学器材
- 2 如果金额 小于等于5000, 由教学主任审批 (0<=x<=5000)
- 3 如果金额 小于等于10000, 由院长审批 (5000<x<=10000)
- 4 如果金额 小于等于30000, 由副校长审批 (10000<x<=30000)
- 5 如果金额 超过30000以上,有校长审批 ( 30000<x)
要是按照传统的方式解决的话.
每次都要进行判断,然后决定具体去找对应的审批检验人,要是到时候判断的条件改了,这个对应关系的依赖也要改动.
采购请求进行处理 和 审批人产生强耦合关系,不利于代码的扩展和维护
那么,来看看职责链模式
职责链模式
每个接收者都包含对另一个接收者的引用;如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,形成一个链条.
(就像过滤器一样)
需要注意的是;要控制链中最大节点数量,节点过多可能会影响性能,一般通过在Handler中设置一个最大节点数量.
原理图
OK,继续回到之前的材料审批问题
需要处理的请求类SchoolRequest
//学校的购买请求;
public class SchoolRequest {
//请求的类型;
private String type = null;
//请求的大概金额;
private float price=0.0f;
//Id;
private int id=0;
//初始化;
public SchoolRequest(String type, float price, int id) {
this.type = type;
this.price = price;
this.id = id;
}
public String getType() {
return type;
}
public float getPrice() {
return price;
}
public int getId() {
return id;
}
}
审批者Approver
/**
* @author by CSDN@小智RE0
*/
//审批者
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 HandlingTheRequest(SchoolRequest schoolRequest);
}
教导主任审批DirectorApprover
/**
* @author by CSDN@小智RE0
*/
//教导主任审批;
public class DirectorApprover extends Approver{
//初始化;
public DirectorApprover(String name) {
super(name);
}
public void HandlingTheRequest(SchoolRequest schoolRequest) {
if(schoolRequest.getPrice()<=5000){
System.out.println(this.name+"表示能处理这个项目-->"+schoolRequest.getType());
System.out.println("项目Id为==>"+schoolRequest.getId());
}
else {
System.out.println(this.name+"表示不行,你去找院长-->");
approver.HandlingTheRequest(schoolRequest);
}
}
}
院长审批DeanApprover
/**
* @author by CSDN@小智RE0
*/
//院长审批;
public class DeanApprover extends Approver{
//初始化;
public DeanApprover(String name) {
super(name);
}
public void HandlingTheRequest(SchoolRequest schoolRequest) {
if(schoolRequest.getPrice()>5000 && schoolRequest.getPrice()<=10000){
System.out.println(this.name+"表示能处理这个项目-->"+schoolRequest.getType());
System.out.println("项目Id为==>"+schoolRequest.getId());
}else {
System.out.println(this.name+"表示不行,你去找副校长吧");
approver.HandlingTheRequest(schoolRequest);
}
}
}
副校长审批VicePresidentApprover
/**
* @author by CSDN@小智RE0
*/
//副校长审批;
public class VicePresidentApprover extends Approver {
//初始化;
public VicePresidentApprover(String name) {
super(name);
}
public void HandlingTheRequest(SchoolRequest schoolRequest) {
if( schoolRequest.getPrice()>10000 && schoolRequest.getPrice()<=30000){
System.out.println(this.name+"表示能处理这个项目-->"+schoolRequest.getType());
System.out.println("项目Id为==>"+schoolRequest.getId());
}else {
System.out.println(this.name+"表示不行,你去找校长吧");
approver.HandlingTheRequest(schoolRequest);
}
}
}
校长审批PresidentApprover
/**
* @author by CSDN@小智RE0
*/
//校长审批;
public class PresidentApprover extends Approver{
//初始化;
public PresidentApprover(String name) {
super(name);
}
public void HandlingTheRequest(SchoolRequest schoolRequest) {
if(schoolRequest.getPrice()>30000){
System.out.println(this.name+"表示能处理这个项目-->"+schoolRequest.getType());
System.out.println("项目Id为==>"+schoolRequest.getId());
}else {
approver.HandlingTheRequest(schoolRequest);
}
}
}
模拟客户端
/**
* @author by CSDN@小智RE0
*/
//模拟客户端;
public class Client {
public static void main(String[] args) {
//提出请求;
SchoolRequest schoolRequest = new SchoolRequest("采购教学用品",60000,0);
Approver directorApprover = new DirectorApprover("教导主任");
Approver deanApprover = new DeanApprover("院长");
Approver vicePresidentApprover = new VicePresidentApprover("副校长");
Approver presidentApprover = new PresidentApprover("校长");
//设置好下一个处理的人;
directorApprover.setApprover(deanApprover);
deanApprover.setApprover(vicePresidentApprover);
vicePresidentApprover.setApprover(presidentApprover);
presidentApprover.setApprover(directorApprover);
//教导主任收到请求时;
directorApprover.HandlingTheRequest(schoolRequest);
}
}
以上是关于尚硅谷设计模式学习(24)---[职责链模式(Chain of Responsibility Pattern)]的主要内容,如果未能解决你的问题,请参考以下文章