责任链模式的结构图责任链模式(Chain of Responsibility),使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,知道有一个对象处理它为止。
责任链模式中的角色:
- Handler:定义一个处理请示的接口
- ConcreteHandler:具体处理者类,处理它所负责的请求,可访问它的后继者,如果可处理该请求,就处理之,否则就将该请求转发给它的后继者。
责任链模式的简单实现
(1)处理请示的接口
public class Request {
private String requestType;
private String requestContent;
private int number;
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestContent() {
return requestContent;
}
public void setRequestContent(String requestContent) {
this.requestContent = requestContent;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
public abstract class Manager {
protected String name;
protected Manager superior;
public Manager(String name) {
this.name = name;
}
public void setSuperior(Manager superior) {
this.superior = superior;
}
public abstract void requestApplication(Request request);
}
(2)具体处理者类
public class CommonManager extends Manager {
public CommonManager(String name) {
super(name);
}
@Override
public void requestApplication(Request request) {
if (request.getRequestType().equals("请假") && request.getNumber() <= 2) {
System.out.println(name + ":" + request.getRequestContent() + "数量" + request.getNumber() + "被批准");
} else{
if (superior != null) {
superior.requestApplication(request);
}
}
}
}
public class Majordomo extends Manager {
public Majordomo(String name) {
super(name);
}
@Override
public void requestApplication(Request request) {
if (request.getRequestType().equals("请假") && request.getNumber() <= 5) {
System.out.println(name + ":" + request.getRequestContent() + "数量" + request.getNumber() + "被批准");
} else{
if (superior != null) {
superior.requestApplication(request);
}
}
}
}
public class GeneralManager extends Manager {
public GeneralManager(String name) {
super(name);
}
@Override
public void requestApplication(Request request) {
if (request.getRequestType().equals("请假")) {
System.out.println(name + ":" + request.getRequestContent() + "数量" + request.getNumber() + "被批准");
} else if (request.getRequestType().equals("加薪") && request.getNumber() <= 500) {
System.out.println(name + ":" + request.getRequestContent() + "数量" + request.getNumber() + "被批准");
} else if (request.getRequestType().equals("加薪") && request.getNumber() > 500) {
System.out.println(name + ":" + request.getRequestContent() + "数量" + request.getNumber() + "再说吧");
}
}
}
(3)客户端调用
public class Client {
public static void main(String[] args) {
CommonManager commonManager = new CommonManager("经理");
Majordomo majordomo = new Majordomo("总监");
GeneralManager generalManager = new GeneralManager("总经理");
commonManager.setSuperior(majordomo);
majordomo.setSuperior(generalManager);
Request request = new Request();
request.setRequestType("请假");
request.setRequestContent("yuzhiyi请假");
request.setNumber(1);
commonManager.requestApplication(request);
Request request1 = new Request();
request1.setRequestType("请假");
request1.setRequestContent("yuzhiyi请假");
request1.setNumber(4);
commonManager.requestApplication(request1);
Request request2 = new Request();
request1.setRequestType("加薪");
request1.setRequestContent("yuzhiyi请求加薪");
request1.setNumber(500);
commonManager.requestApplication(request1);
Request request3 = new Request();
request1.setRequestType("加薪");
request1.setRequestContent("yuzhiyi请求加薪");
request1.setNumber(1000);
commonManager.requestApplication(request1);
}
}
(4)输出
经理:yuzhiyi请假数量1被批准
总监:yuzhiyi请假数量4被批准
总经理:yuzhiyi请求加薪数量500被批准
总经理:yuzhiyi请求加薪数量1000再说吧
责任链模式的使用场景
使用场景:
当客户提交一个请求时,请求时沿链传递直至有一个 ConcreteHandler 对象负责处理它。
责任链的优缺点
优点:
接收者和发送者都没有对方的明确信息,且链中的对象自己也并不知道链的结构。结果是责任链可简化对象的相互连接,它们仅需保持一个指向其后继者的引用,而不需保持它所有的候选者的引用。
随时地增加或修改一个请求的结构。增强了给对象指派职责的灵活性。
缺点:
一个请求极有可能到了链的末端都得不到处理,或者因为没有正确配置而得不到处理。
网友评论