设计模式-责任链

以下是根据文章内容生成的Java责任链设计模式的代码示例,包含两种不同的使用场景:

场景一:请求过滤

  1. 抽象处理器
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public interface MyFilter {
    boolean doFilter(MyRequest request, MyResponse response, MyFilterChain chain);
}
  1. 具体处理器 - 登录过滤器
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public class LoginFilter implements MyFilter {
    @Override
    public boolean doFilter(MyRequest request, MyResponse response, MyFilterChain chain) {
        String reqMsg = request.getReqMsg();
        if (!reqMsg.contains("username") ||!reqMsg.contains("password")) {
            System.out.println("登录失败,请重新登录");
            return false;
        }
        String resMsg = response.getResMsg();
        resMsg += "--登录过滤器返回响应消息";
        response.setResMsg(resMsg);
        return true;
    }
}
  1. 具体处理器 - 业务过滤器
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public class BusinessFilter implements MyFilter {
    @Override
    public boolean doFilter(MyRequest request, MyResponse response, MyFilterChain chain) {
        String reqMsg = request.getReqMsg();
        if (!reqMsg.contains("/filter")) {
            System.out.println("请求的接口路径不存在");
            return false;
        }
        String resMsg = response.getResMsg();
        resMsg += "--业务逻辑处理完返回响应消息";
        response.setResMsg(resMsg);
        return true;
    }
}
  1. 处理器链
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
import java.util.ArrayList;
import java.util.List;

public class MyFilterChain {
    private List<MyFilter> filterList = new ArrayList<>();

    public MyFilterChain addFilter(MyFilter filter) {
        filterList.add(filter);
        return this;
    }

    public void doFilter(MyRequest request, MyResponse response, MyFilterChain chain) {
        for (MyFilter filter : filterList) {
            if (!filter.doFilter(request, response, chain)) {
                break;
            }
        }
    }
}
  1. 实体类 - 请求类
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public class MyRequest {
    private String reqMsg;

    public String getReqMsg() {
        return reqMsg;
    }

    public void setReqMsg(String reqMsg) {
        this.reqMsg = reqMsg;
    }
}
  1. 实体类 - 响应类
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public class MyResponse {
    private String resMsg;

    public String getResMsg() {
        return resMsg;
    }

    public void setResMsg(String resMsg) {
        this.resMsg = resMsg;
    }
}
  1. 测试类
java 复制代码
package com.design.mode.responsibilityChain.responsibility01;
public class RespChainTest01 {
    public static void main(String[] args) {
        String reqMsg = "{username:andy,password:123456,url:\"http://ip:port/filter\"}";
        MyRequest request = new MyRequest();
        request.setReqMsg(reqMsg);
        MyResponse response = new MyResponse();
        MyFilterChain chain = new MyFilterChain();
        MyFilter loginFilter = new LoginFilter();
        MyFilter businessFilter = new BusinessFilter();
        chain.addFilter(loginFilter).addFilter(businessFilter);
        chain.doFilter(request, response, chain);
        String resMsg = response.getResMsg();
        System.out.println("得到响应结果=======" + resMsg);
    }
}

场景二:请假审批

  1. 实体类 - 请假请求类
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class LeaveRequest {
    private Integer days;
    private String reason;

    public LeaveRequest() {
    }

    public Integer getDays() {
        return days;
    }

    public void setDays(Integer days) {
        this.days = days;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    @Override
    public String toString() {
        return "Leave{" +
                "days=" + days +
                ", reason='" + reason + '\'' +
                '}';
    }
}
  1. 实体类 - 审批结果类
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class Result {
    private boolean isRatify;
    private String info;

    public boolean isRatify() {
        return isRatify;
    }

    public void setRatify(boolean ratify) {
        isRatify = ratify;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Result{" +
                "isRatify=" + isRatify +
                ", info='" + info + '\'' +
                '}';
    }
}
  1. 抽象处理器
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public interface Ratify {
    Result handle(LeaveRequest request, Result result, MyChain chain);
}
  1. 具体处理器 - 组长审批
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class GroupLeader implements Ratify {
    @Override
    public Result handle(LeaveRequest request, Result result, MyChain chain) {
        Integer days = request.getDays();
        if (days <= 2) {
            result.setRatify(true);
            result.setInfo("组长同意,早去早回。");
        } else {
            chain.doFilter(request, result, chain);
        }
        return result;
    }
}
  1. 具体处理器 - 主管审批
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class DepartmentHead implements Ratify {
    @Override
    public Result handle(LeaveRequest request, Result result, MyChain chain) {
        Integer days = request.getDays();
        if (days <= 4) {
            result.setRatify(true);
            result.setInfo("主管同意,路上注意安全。");
        } else {
            chain.doFilter(request, result, chain);
        }
        return result;
    }
}
  1. 具体处理器 - 经理审批
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class DepartmentManager implements Ratify {
    @Override
    public Result handle(LeaveRequest request, Result result, MyChain chain) {
        Integer days = request.getDays();
        if (days <= 7) {
            result.setRatify(true);
            result.setInfo("经理同意,回去安心办事。");
        } else {
            result.setRatify(false);
            result.setInfo("经理不同意,时间太久。");
            chain.doFilter(request, result, chain);
        }
        return result;
    }
}
  1. 处理器链
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
import java.util.ArrayList;
import java.util.List;

public class MyChain {
    private int index = 0;
    private List<Ratify> filterList = new ArrayList<>();

    public MyChain addFilter(Ratify ratify) {
        filterList.add(ratify);
        return this;
    }

    public void doFilter(LeaveRequest request, Result result, MyChain chain) {
        if (index == filterList.size()) {
            return;
        }
        Ratify ratify = filterList.get(index);
        index++;
        ratify.handle(request, result, chain);
    }
}
  1. 测试类
java 复制代码
package com.design.mode.responsibilityChain.responsibility02;
public class RespChainTest02 {
    public static void main(String[] args) {
        LeaveRequest request = new LeaveRequest();
        request.setDays(3);
        Result result = new Result();
        MyChain chain = new MyChain();
        Ratify leader = new GroupLeader();
        Ratify head = new DepartmentHead();
        Ratify manager = new DepartmentManager();
        chain.addFilter(leader).addFilter(head).addFilter(manager);
        chain.doFilter(request, result, chain);
        System.out.println("请假结果======" + result);
    }
}
相关推荐
汇匠源1 分钟前
共享无人系统,从出行到生活全面覆盖
java·生活
Vincent(朱志强)6 分钟前
设计模式详解(十二):单例模式——Singleton
android·单例模式·设计模式
小灰灰要减肥1 小时前
装饰者模式
java
张铁铁是个小胖子1 小时前
MyBatis学习
java·学习·mybatis
诸葛悠闲2 小时前
设计模式——桥接模式
设计模式·桥接模式
Yan.love2 小时前
开发场景中Java 集合的最佳选择
java·数据结构·链表
椰椰椰耶2 小时前
【文档搜索引擎】搜索模块的完整实现
java·搜索引擎
大G哥2 小时前
java提高正则处理效率
java·开发语言
智慧老师2 小时前
Spring基础分析13-Spring Security框架
java·后端·spring
lxyzcm2 小时前
C++23新特性解析:[[assume]]属性
java·c++·spring boot·c++23