介绍
职责链模式(Chain of Responsibility):使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
- Handler类,定义一个处理请示的接口
- ConcreteHandler类,具体处理者类,处理它所负责的请求,可访问它的后继者,如果可处理该请求,就处理之,否则就将该请求转发给它的后继者
当客户提交一个请求时,请求是沿链传递直至有一个 Concretehandler对象负责处理它。
接收者和发送者都没有对方的明确信息,且链中的对象自己也并不知道链的结构。结果是职责链可简化对象的相互连接,它们仅需保持一个指向其后继者的引用,而不需保持它所有的候选接受者的引用
随时地增加或修改处理一个请求的结构,增强了给对象指派职责的灵活性
一个请求极有可能到了链的末端都得不到处理,或者因为没有正确配置而得不到处理
代码示例
Handler
/**
* 管理者
*
*/
public abstract class Manager {
protected String name;
public Manager(String name) {
this.name = name;
}
/**
* 上级
*/
protected Manager superior;
public void setSuperior(Manager superior) {
this.superior = superior;
}
/**
* 请求申请
*/
public abstract void requestApplication(Request request);
/**
* 向上级发起请求申请
*
*/
protected void superiorRequestApplication(Request request) {
if (this.superior != null) {
superior.requestApplication(request);
}
}
}
Handler 实现类
/**
* 普通经理
*/
public class CommonManager extends Manager {
public CommonManager(String name) {
super(name);
}
@Override
public void requestApplication(Request request) {
if (request.getType().equals("请假") && request.getNumber() <= 2) {
System.out.println(this.name + ": 批准" + request);
} else {
superiorRequestApplication(request);
}
}
}
/**
* 总监
*/
public class Director extends Manager {
public Director(String name) {
super(name);
}
@Override
public void requestApplication(Request request) {
if (request.getType().equals("请假") && request.getNumber() <= 5) {
System.out.println(this.name + ": 批准" + request);
} else {
superiorRequestApplication(request);
}
}
}
/**
* 总经理
*/
public class GeneralManager extends Manager {
public GeneralManager(String name) {
super(name);
}
@Override
public void requestApplication(Request request) {
if (request.getType().equals("请假")) {
System.out.println(this.name + ": 批准" + request);
}
if (request.getType().equals("加薪") && request.getNumber() <= 5000) {
System.out.println(this.name + ": 批准" + request);
}
if (request.getType().equals("加薪") && request.getNumber() > 5000) {
System.out.println(this.name + ": 不批准" + request);
}
}
}
Request
/**
* 申请
*
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Request {
/**
* 申请类别
*/
private String type;
/**
* 申请内容
*/
private String content;
/**
* 数量
*/
private int number;
}
客户端
public class Test {
public static void main(String[] args) {
CommonManager manager = new CommonManager("普通经理 - 直接上级");
Director director = new Director("总监");
GeneralManager generalManager = new GeneralManager("总经理");
manager.setSuperior(director);
director.setSuperior(generalManager);
Request request1 = new Request("请假", "请假原因", 1);
manager.requestApplication(request1);
Request request2 = new Request("请假", "请假原因", 4);
manager.requestApplication(request2);
Request request3 = new Request("加薪", "加薪原因", 1000);
manager.requestApplication(request3);
Request request4 = new Request("加薪", "加薪原因", 10000);
manager.requestApplication(request4);
}
}
标签:24,requestApplication,请求,流程,Request,20230428,new,public,name
From: https://www.cnblogs.com/huangwenjie/p/17490418.html