设计模式-责任链

devtools/2024/11/8 9:03:42/

以下是根据文章内容生成的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 {@Overridepublic 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 {@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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 {@Overridepublic 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);}
}

http://www.ppmy.cn/devtools/132265.html

相关文章

LabVIEW实验室液压制动系统

压制动系统是许多实验设备的重要安全组件&#xff0c;尤其在高负荷、高速实验环境下&#xff0c;制动系统的性能对设备和操作人员的安全至关重要。传统的实验室液压制动系统监测方法存在数据采集实时性差、精度低、故障预警不及时等问题。为了提高实验安全性和设备运行的稳定性…

深入理解 C/C++ 中的不完整类型(Incomplete Type)

在 C/C 编程中&#xff0c;我们有时会遇到 “不完整类型”&#xff08;Incomplete Type&#xff09;的概念&#xff0c;尤其是在跨 C 和 C 代码之间进行集成开发时。不完整类型是编译器中的一种特殊概念&#xff0c;用于提高封装性和减少代码耦合。在这篇博客中&#xff0c;我将…

【LwIP源码学习4】主线程tcpip_thread

前言 本文对lwip的主要线程tcpip_thread进行分析。 正文 tcpip_thread是lwip最主要的线程&#xff0c;其创建在tcpip_init函数中 sys_thread_new(TCPIP_THREAD_NAME, tcpip_thread, NULL, TCPIP_THREAD_STACKSIZE, TCPIP_THREAD_PRIO);tcpip_init函数被TCPIP_Init函数调用。…

SQL用一个字段查询出数据的交集

出个SQL查询的题吧&#xff0c;有兴趣的可以看看 1、问题 下面有一份数据&#xff08;图1&#xff09;&#xff0c;由两部分组成&#xff1a;分析数据和基准数据 分析数据标识列为1&#xff0c;基准数据标识列为1&#xff0c;两字段0,1互斥 要求&#xff1a;按找出两部分数据…

qt QItemSelectionModel详解

1、概述 QItemSelectionModel是Qt框架中提供的一个功能强大且灵活的项选择模型类。它主要用于在用户界面中管理和操作用户选定的项&#xff0c;是实现交互式和响应式应用程序的重要组件。QItemSelectionModel能够帮助开发者创建用户友好和高效的多选列表、表格数据选择等应用场…

探索开放资源上指令微调语言模型的现状

人工智能咨询培训老师叶梓 转载标明出处 开放模型在经过适当的指令调整后&#xff0c;性能可以与最先进的专有模型相媲美。但目前缺乏全面的评估&#xff0c;使得跨模型比较变得困难。来自Allen Institute for AI和华盛顿大学的研究人员们进行了一项全面的研究&#xff0c;探索…

【北京迅为】《STM32MP157开发板嵌入式开发指南》-第七十二章 Debian文件系统

iTOP-STM32MP157开发板采用ST推出的双核cortex-A7单核cortex-M4异构处理器&#xff0c;既可用Linux、又可以用于STM32单片机开发。开发板采用核心板底板结构&#xff0c;主频650M、1G内存、8G存储&#xff0c;核心板采用工业级板对板连接器&#xff0c;高可靠&#xff0c;牢固耐…

如何有效销售和应用低代码软件?探索其市场机会与策略

随着技术的进步&#xff0c;企业对自动化和数字化的需求日益增加。低代码开发平台应运而生&#xff0c;成为企业实现快速应用程序开发的重要工具。然而&#xff0c;在市场上推广和应用低代码软件并非易事&#xff0c;需要深入了解客户需求&#xff0c;提供定制化的解决方案&…