职责链模式:使多个对象有机会处理请求,从而避免请求的发送者和接收者之间的耦合。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
即用类来处理请求的一个分支,并将下个分支的处理传递到下个类来处理。这样就将请求的不同分支情况切割为各个类来管理,解耦。
先创建一个请求,它存储的是我们条件判断要用的信息,就是if else对应需要信息。
public class Request {
private String requestType;
private String requestContent;
private int number;
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestType() {
return requestType;
}
public void setRequestContent(String requestContent) {
this.requestContent = requestContent;
}
public String getRequestContent() {
return requestContent;
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return 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);
}
具体类
public class CommonManager extends Manager {
public CommonManager(String name) {
super(name);
}
@Override
public void requestApplication(Request request) {
if ("请假" == request.getRequestType() && request.getNumber() <= 2) {
System.out.println(name + ":" + request.getRequestContent() + " 天数:" + request.getNumber() + "被批准");
}
else {
if (null != superior) {
superior.requestApplication(request);
}
}
}
}
public class Majordomo extends Manager {
public Majordomo(String name) {
super(name);
}
@Override
public void requestApplication(Request request) {
if ("请假" == request.getRequestType() && request.getNumber() <= 5) {
System.out.println(name + ":" + request.getRequestContent() + " 天数:" + request.getNumber() + "被批准");
}
else {
if (null != superior) {
superior.requestApplication(request);
}
}
}
}
public class GeneralManager extends Manager {
public GeneralManager(String name) {
super(name);
}
@Override
public void requestApplication(Request request) {
if("请假" == request.getRequestType()) {
System.out.println(name + ":" + request.getRequestContent() + " 天数:" + request.getNumber() + "被批准");
}
else if("加薪" == request.getRequestType() && request.getNumber() <= 500) {
System.out.println(name + ":" + request.getRequestContent() + " 金额:" + request.getNumber() + "被批准");
}
else if("加薪" == request.getRequestType() && request.getNumber() > 500) {
System.out.println(name + ":" + request.getRequestContent() + " 金额:" + request.getNumber() + "再说吧");
}
}
}
有客户端来设置职责链中每个类的访问循序:
CommonManager jinli = new CommonManager("经理");
Majordomo zongjian = new Majordomo("总监");
GeneralManager zongjianli = new GeneralManager("总经理");
//设置访问循序
jinli.setSuperior(zongjian);
zongjian.setSuperior(zongjianli);
Request request = new Request();
request.setRequestType("请假");
request.setRequestContent("小明请假");
request.setNumber(1);
jinli.requestApplication(request);
Request request1 = new Request();
request1.setRequestType("请假");
request1.setRequestContent("小李请假");
request1.setNumber(5);
jinli.requestApplication(request1);
Request request2 = new Request();
request2.setRequestType("加薪");
request2.setRequestContent("小红请求加薪");
request2.setNumber(500);
jinli.requestApplication(request2);
Request request3 = new Request();
request3.setRequestType("加薪");
request3.setRequestContent("小张请求加薪");
request3.setNumber(1000);
zongjian.requestApplication(request3);
可以看到,有了职责链以后就不需要在改需求的时候去看那么多的if else都揉在一个函数里的代码了。只需要相应修改对应类就可以了。