工作中最喜欢的事情就是加薪了,但获得加薪可不容易,得经过层层审核。下面来代码模拟一下。
请求类
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 class Manager { protected String name; public Manager(String name){ this.name = name; } public void getResult(String managerLevel, Request request){ if(managerLevel.equals("经理")){ if(request.getRequestType().equals("请假") && request.getNumber() <=2){ System.out.println(name + " : " + request.getRequestContent() + "数量 " +request.getNumber() + " 被批准"); }else{ System.out.println(name + " : " + request.getRequestContent() + "数量 " +request.getNumber() + " 我无权处理"); } }else if(managerLevel.equals("总监")){ if(request.getRequestType().equals("请假") && request.getNumber() <=5){ System.out.println(name + " : " + request.getRequestContent() + "数量 " +request.getNumber() + " 被批准"); }else{ System.out.println(name + " : " + request.getRequestContent() + "数量 " +request.getNumber() + " 我无权处理"); } }else if(managerLevel.equals("总经理")){ 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() + " 再说吧"); } } } }
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Manager m1 = new Manager("zhangsan"); Manager m2 = new Manager("lisi"); Manager m3 = new Manager("wangmazi"); //加薪请求 Request request = new Request(); request.setRequestType("加薪"); request.setRequestContent("xiaohe请求加薪"); request.setNumber(600); m1.getResult("经理", request); m2.getResult("总监", request); m3.getResult("总经理", request); //请假请求 Request request2 = new Request(); request2.setRequestType("请假"); request2.setRequestContent("xiaohe打算请假"); request2.setNumber(3); m1.getResult("经理", request2); m2.getResult("总监", request2); m3.getResult("总经理", request2); } }
可以发现,管理者类中的getResult方法中有太多的分支判断,是非常不好的设计。如果要增加其他管理者,那就需要更改这个类,这个类承担了太多的责任,违背了单一职责原则和开放封闭原则。那如何重构呢?
职责链模式:使多个对象都有机会处理请求,从而避免请求发送者和请求接收者之间的耦合关系。将这个对象连城一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
下面重构一下上面的代码:
public abstract class Manager1 { protected String name; protected Manager1 superior; public Manager1(String name){ this.name = name; } public void setSuperior(Manager1 manager){ this.superior = manager; } public abstract void requestApplications(Request request); public String getName() { return name; } public void setName(String name) { this.name = name; } public Manager1 getSuperior() { return superior; } }
public class CommonManager extends Manager1{ public CommonManager(String name){ super(name); } @Override public void requestApplications(Request request) { // TODO Auto-generated method stub if(request.getRequestType().equals("请假") && request.getNumber() <= 2){ System.out.println(name + " : " + request.getRequestContent() + "数量 " +request.getNumber() + " 被批准"); }else{ if(superior != null){ superior.requestApplications(request); } } } }
public class Majordomo extends Manager1{ public Majordomo(String name){ super(name); } @Override public void requestApplications(Request request) { // TODO Auto-generated method stub if(request.getRequestType().equals("请假") && request.getNumber() <= 5){ System.out.println(name + " : " + request.getRequestContent() + "数量 " +request.getNumber() + " 被批准"); }else{ if(superior != null){ superior.requestApplications(request); } } } }
public class GeneralManager extends Manager1{ public GeneralManager(String name){ super(name); } @Override public void requestApplications(Request request) { // TODO Auto-generated method stub 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() + " 再说吧"); } } }
public class Test3 { public static void main(String[] args) { // TODO Auto-generated method stub CommonManager jinli = new CommonManager("jinli"); Majordomo zongjian = new Majordomo("zongjian"); GeneralManager zongjinli = new GeneralManager("zongjinli"); jinli.setSuperior(zongjian); zongjian.setSuperior(zongjinli); Request request = new Request(); request.setRequestType("加薪"); request.setRequestContent("xiaohe请求加薪"); request.setNumber(300); jinli.requestApplications(request); System.out.println("---------------------------"); Request request2 = new Request(); request2.setRequestType("请假"); request2.setRequestContent("xiaohe打算请假"); request2.setNumber(10); zongjian.requestApplications(request2); } }
这就很好的解决了大量的分支判断造成难维护、灵活性差的问题。我们可以随时增加或修改处理一个请求的结构,增强了给对象指派职责的灵活性。不过也要担心,一个请求极有可能到了链的末端也得不到处理,或者因为没有正确配置而得不到处理,这就糟糕了,所以得考虑全面。