概述
注:示例来自《大话设计模式》
假如现有如下场景 员工向经理申请加薪或请假 经理没权利 然后向总监上报 总监也没权限 向总经理上报 我们用代码来实现这个场景 简单代码实现如下
申请类
package Test24;
//申请
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;
}
}
管理者类
package Test24;
//管理者
public class Manager {
protected String name;
public Manager(String name)
{
this.name = name;
}
//得到结果
public void GetResult(ManagerLevel managerLevel, Request request)
{
if (managerLevel == ManagerLevel.经理)
{
if (request.getRequestType() == "请假" && request.getNumber() <= 2)
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 被批准");
}
else
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 我无权处理");
}
}
else if (managerLevel == ManagerLevel.总监)
{
if (request.getRequestType() == "请假" && request.getNumber() <= 5)
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 被批准");
}
else
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 我无权处理");
}
}
else if (managerLevel == ManagerLevel.总经理)
{
if (request.getRequestType() == "请假")
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 被批准");
}
else if (request.getRequestType() == "加薪" && request.getNumber() <= 500)
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 被批准");
}
else if (request.getRequestType() == "加薪" && request.getNumber() > 500)
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 再说吧");
}
}
}
}
管理者级别类
package Test24;
public enum ManagerLevel {
经理,总监,总经理
}
客户端代码
package Test24;
public class Program {
public static void main(String[] args) {
Manager jinli = new Manager("金利");
Manager zongjian = new Manager("宗剑");
Manager zhongjingli = new Manager("钟精励");
Request request = new Request();
request.setRequestType("加薪");
request.setRequestContent("小菜请求加薪");
request.setNumber(1000);
jinli.GetResult(ManagerLevel.经理, request);
zongjian.GetResult(ManagerLevel.总监, request);
zhongjingli.GetResult(ManagerLevel.总经理, request);
Request request2 = new Request();
request2.setRequestType("请假");
request2.setRequestContent("小菜请假");
request2.setNumber(3);
jinli.GetResult(ManagerLevel.经理, request2);
zongjian.GetResult(ManagerLevel.总监, request2);
zhongjingli.GetResult(ManagerLevel.总经理, request2);
}
}
上面的写法 管理这类里面的结果方法比较长 加上有太多的分支判断 是非常不好的设计 违背了单一职责原则和开放-封闭原则 下面我们使用职责链模式进行重构 代码如下
管理者类
package Test24;
//管理者
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;
}
//申请请求
abstract public void RequestApplications(Request request);
}
经理类
package Test24;
//经理
public class CommonManager extends Manager {
public CommonManager(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
if (request.getRequestType() == "请假" && request.getNumber() <= 2)
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 被批准");
}
else
{
if (superior != null)
superior.RequestApplications(request);
}
}
}
总监类
package Test24;
//总监
public class Majordomo extends Manager {
public Majordomo(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
if (request.getRequestType() == "请假" && request.getNumber() <= 5)
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 被批准");
}
else
{
if (superior != null)
superior.RequestApplications(request);
}
}
}
总经理类
package Test24;
//总经理
public class GeneralManager extends Manager {
public GeneralManager(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
if (request.getRequestType() == "请假")
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 被批准");
}
else if (request.getRequestType() == "加薪" && request.getNumber() <= 500)
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 被批准");
}
else if (request.getRequestType() == "加薪" && request.getNumber() > 500)
{
System.out.println(name+":"+request.getRequestType()+" 数量"+request.getNumber()+" 再说吧");
}
}
}
客户端代码
package Test24;
public class Program {
public static void main(String[] args) {
CommonManager jinli = new CommonManager("金利");
Majordomo zongjian = new Majordomo("宗剑");
GeneralManager zhongjingli = new GeneralManager("钟精励");
jinli.SetSuperior(zongjian);
zongjian.SetSuperior(zhongjingli);
Request request = new Request();
request.setRequestType("请假");
request.setRequestContent("小菜请假");
request.setNumber(1);
jinli.RequestApplications(request);
Request request2 = new Request();
request2.setRequestType("请假");
request2.setRequestContent("小菜请假");
request2.setNumber(4);
jinli.RequestApplications(request2);
Request request3 = new Request();
request3.setRequestType("加薪");
request3.setRequestContent("小菜请求加薪");
request3.setNumber(500);
jinli.RequestApplications(request3);
Request request4 = new Request();
request4.setRequestType("加薪");
request4.setRequestContent("小菜请求加薪");
request4.setNumber(1000);
jinli.RequestApplications(request4);
}
}
这样就很好地解决了原来大量的分支判断造成的难维护 灵活性差的问题
职责链模式 使多个对象都有机会处理请求 从而避免请求的发送者和接收者之间的耦合关系 将这个对象连成一条链 并沿着这条链传递该请求 直到有一个对象处理它为止
职责链的好处 当客户提交一个请求时 请求是沿链传递直至有一个ConcreteHandler对象负责处理它
接收者和发送者都没有对方的明确信息 且链中的对象自己也并不直到链的结构 结果是职责链可简化对象的相互连接 它们仅需保持一个指向其后继者的引用 而不需保持它所有的候选接受者的引用
可以随时地增加或修改处理一个请求的结构 增强了给对象指派职责的灵活性
缺点 一个请求极有可能到了链的末端都得不到处理 或者因为没有正确配置而得不到处理
最后
以上就是落后大神为你收集整理的大话设计模式之职责链模式总结-java实现的全部内容,希望文章能够帮你解决大话设计模式之职责链模式总结-java实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复