设计模式----职责链模式
今天介绍一种模式:职责链模式。
正如它的名字,职责链,这是一种链状的结构,在大概学习了它的理念之后我感觉这个设计模式特别类似于数据结构中的单链表。
先看这个模式的UML类图:
处理请示接口:
public abstract class Handler { //继任者 protected Handler successor; //设置继任者 public void setSuccessor(Handler successor){ this.successor = successor; } //处理请求 public abstract void handleRequest(int request); }
具体处理请示类:
public class ConcreteHandler1 extends Handler{ @Override public void handleRequest(int request) { if(request >= 0 && request < 10){ System.out.println(this.getClass().getName() + "处理请求" + request); } else if(successor != null) { successor.handleRequest(request); } } }
public class ConcreteHandler2 extends Handler{ @Override public void handleRequest(int request) { if(request >= 10 && request < 20){ System.out.println(this.getClass().getName() + "处理请求" + request); } else if(successor != null) { successor.handleRequest(request); } } }
public class ConcreteHandler3 extends Handler{ @Override public void handleRequest(int request) { if(request >= 20 && request < 30){ System.out.println(this.getClass().getName() + "处理请求" + request); } else if(successor != null) { successor.handleRequest(request); } else{ System.out.println(this.getClass().getName() + "无法处理请求"); } } }
客户端调用:
public static void main(String[] args) { Handler h1 = new ConcreteHandler1(); Handler h2 = new ConcreteHandler2(); Handler h3 = new ConcreteHandler3(); h1.setSuccessor(h2); h2.setSuccessor(h3); for(int i=0;i < 20;i++){ int randNum = (int) (Math.random() * 40); h1.handleRequest(randNum); } }
输出:
template.ConcreteHandler1处理请求6
template.ConcreteHandler1处理请求2
template.ConcreteHandler2处理请求19
template.ConcreteHandler2处理请求12
template.ConcreteHandler1处理请求7
template.ConcreteHandler3无法处理请求
template.ConcreteHandler3处理请求25
template.ConcreteHandler3无法处理请求
template.ConcreteHandler1处理请求3
template.ConcreteHandler3无法处理请求
template.ConcreteHandler2处理请求17
template.ConcreteHandler3无法处理请求
template.ConcreteHandler2处理请求13
template.ConcreteHandler3无法处理请求
template.ConcreteHandler2处理请求13
template.ConcreteHandler3无法处理请求
template.ConcreteHandler2处理请求17
template.ConcreteHandler2处理请求18
template.ConcreteHandler2处理请求15
template.ConcreteHandler2处理请求19
我们来看一个具体的例子:
一家公司里有这样的规定:经理可以处理低于三天的请假,总监可以处理低于5天的请假,加薪和请假更多天要去总经理那边。
总经理那边能处理低于10天的请假和低于500的加薪。
代码:
请求类:
public class Request { //申请类别 private String requestType; //申请内容 private String requsetContext; //申请数量 private Integer requestNumber; public String getRequestType() { return requestType; } public void setRequestType(String requestType) { this.requestType = requestType; } public String getRequsetContext() { return requsetContext; } public void setRequsetContext(String requsetContext) { this.requsetContext = requsetContext; } public Integer getRequestNumber() { return requestNumber; } public void setRequestNumber(Integer requestNumber) { this.requestNumber = requestNumber; } public Request(String requestType, String requsetContext, Integer requestNumber) { this.requestType = requestType; this.requsetContext = requsetContext; this.requestNumber = requestNumber; } }
处理请求者类:
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 requestApplications(Request request); }
经理类:
//经理类 public class CommonManager extends Manager{ public CommonManager(String name) { super(name); } @Override public void requestApplications(Request request) { if("请假".equals(request.getRequestType()) && request.getRequestNumber() < 3 && request.getRequestNumber() > 0){ System.out.println(this.name + "处理" + request.getRequestType() + "请求,数量 " + request.getRequestNumber() + "批准申请!" ); }else{ if(this.superior != null){ superior.requestApplications(request); } } } }
总监类:
//总监类 public class Majordomo extends Manager{ public Majordomo(String name) { super(name); } @Override public void requestApplications(Request request) { if("请假".equals(request.getRequestType()) && request.getRequestNumber() <=5){ System.out.println(this.name + "处理" + request.getRequestType() + "请求,数量 " + request.getRequestNumber() + "批准申请!" ); }else{ if(this.superior != null){ superior.requestApplications(request); } } } }
总经理类:
//总经理类 public class GeneralManager extends Manager{ public GeneralManager(String name) { super(name); } @Override public void requestApplications(Request request) { if("请假".equals(request.getRequestType()) && request.getRequestNumber() <= 10){ System.out.println(this.name + "处理" + request.getRequestType() + "请求,数量 " + request.getRequestNumber() + "批准申请!" ); } else if ("加薪".equals(request.getRequestType()) && request.getRequestNumber() <=500){ System.out.println(this.name + "处理" + request.getRequestType() + "请求,数量 " + request.getRequestNumber() + "批准申请!" ); } else{ System.out.println(this.name + "处理" + request.getRequestType() + "请求,数量 " + request.getRequestNumber() + ",再说吧!" ); } } }
客户端代码:
public static void main(String[] args) { Manager m1 = new CommonManager("经理"); Manager m2 = new Majordomo("总监"); Manager m3 = new GeneralManager("总经理"); m1.setSuperior(m2); m2.setSuperior(m3); m1.requestApplications(new Request("请假","A家里有事!",4)); m1.requestApplications(new Request("请假","B家里有事!",1)); m1.requestApplications(new Request("加薪","C缺钱!",300)); m1.requestApplications(new Request("加薪","D缺钱!",700)); m1.requestApplications(new Request("请假","E家里有事!",7)); }
控制台输出:
总监处理请假请求,数量 4批准申请! 经理处理请假请求,数量 1批准申请! 总经理处理加薪请求,数量 300批准申请! 总经理处理加薪请求,数量 700,再说吧! 总经理处理请假请求,数量 7批准申请!