责任链模式
使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链处理该请求,直到有一个对象处理它为止。
优点
1、降低耦合度。它将请求的发送者和接收者解耦。
2、简化了对象。使得对象不需要知道链的结构。
3、增强给对象指派职责的灵活性。通过改变链内的成员或者调动它们的次序,允许动态地新增或者删除责任。 4、增加新的请求处理类很方便。
缺点
1、不能保证请求一定被接收。
2、系统性能将受到一定影响,而且在进行代码调试时不太方便,可能会造成循环调用。
3、可能不容易观察运行时的特征,有碍于除错。
使用场景
1、有多个对象可以处理同一个请求,具体哪个对象处理该请求由运行时刻自动确定。
2、在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。 3、可动态指定一组对象处理请求。
具体实现代码
待处理类
package com.chenpt.designModel.responsibilityModel;/** * @Author: chen * @Description:待处理请求 * @Date: created in 2018/8/28 * @Modified By: */public class Request { String requestType; String requestContent; int numbers; 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 getNumbers() { return numbers; } public void setNumbers(int numbers) { this.numbers = numbers; }}
管理者
package com.chenpt.designModel.responsibilityModel;/** * @Author: chen * @Description: 管理者 * @Date: created in 2018/8/28 * @Modified By: */public abstract class Manager { protected String name; protected Manager superManager;//管理者的上级 Manager(String name){ this.name=name; } public void setSuperManager(Manager superManager){ this.superManager=superManager; } //申请请求 public abstract void requestApplication(Request request);}
具体管理者1
package com.chenpt.designModel.responsibilityModel;/** * @Author: chen * @Description: 经理 * @Date: created in 2018/8/28 * @Modified By: */public class CommonManager extends Manager{ CommonManager(String name){ super(name); } @Override public void requestApplication(Request request) { if(request.getRequestType().equals("请假") && request.getNumbers()<=2){ System.out.println("经理回复:已批准"+request.getRequestContent()); }else { if(superManager!=null){ superManager.requestApplication(request); } } }}
具体管理者2
package com.chenpt.designModel.responsibilityModel;/** * @Author: chen * @Description: 总监 * @Date: created in 2018/8/28 * @Modified By: */public class MajorManager extends Manager { MajorManager(String name){ super(name); } @Override public void requestApplication(Request request) { if(request.getRequestType().equals("请假") && request.getNumbers()<=5){ System.out.println("总监回复:已批准"+request.getRequestContent()); }else { if(superManager!=null){ superManager.requestApplication(request); } } }}
具体管理者3
package com.chenpt.designModel.responsibilityModel;/** * @Author: chen * @Description: 总经理 * @Date: created in 2018/8/28 * @Modified By: */public class GeneralManager extends Manager { GeneralManager(String name){ super(name); } @Override public void requestApplication(Request request) { if(request.getRequestType().equals("请假")){ System.out.println("总经理回复:已批准"+request.getRequestContent()); }else if(request.getRequestType().equals("加薪") && request.getNumbers()<=500){ System.out.println("总经理回复:加薪已批准"+request.getRequestContent()); }else if(request.getRequestType().equals("加薪") && request.getNumbers()>500){ System.out.println("总经理回复:加薪 回头再说吧"); } }}
客户端
package com.chenpt.designModel.responsibilityModel;/** * @Author: chen * @Description: * @Date: created in 2018/8/28 * @Modified By: */public class MainTest { public static void main(String[] args){ CommonManager commonManager = new CommonManager("景丽"); MajorManager majorManager = new MajorManager("宗建"); GeneralManager generalManager = new GeneralManager("宗景丽"); //设置上级 commonManager.setSuperManager(majorManager); majorManager.setSuperManager(generalManager); Request request = new Request(); request.setRequestType("请假"); request.setRequestContent("小菜请假"); request.setNumbers(1); commonManager.requestApplication(request);//客户端的申请都由经理直接发起 request.setNumbers(3); commonManager.requestApplication(request); request.setRequestType("加薪"); request.setRequestContent("小菜要加薪"); request.setNumbers(500); commonManager.requestApplication(request); request.setNumbers(1000); commonManager.requestApplication(request); }}//执行结果经理回复:已批准小菜请假总监回复:已批准小菜请假总经理回复:加薪已批准小菜要加薪总经理回复:加薪 回头再说吧