1.责任链测试
package com.zhaoshuangjian.mode17_责任链模式; import com.zhaoshuangjian.mode17_责任链模式.mode17.filter.ComputerFilter; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.Computer; import com.zhaoshuangjian.mode17_责任链模式.mode17.query.ComputerQueryService1; import com.zhaoshuangjian.mode17_责任链模式.mode17.query.ComputerQueryService2; import com.zhaoshuangjian.utils.JsonUtils; import java.util.List; /** * <p>责任链测试</p> * * @Author */ public class HandlerTest { public static void main(String[] args) { ComputerFilter filter = new ComputerFilter(); // 查询电脑编号等于1001和1002的信息 filter.addIds(1001L); filter.addIds(1002L); // 载入显示器信息 filter.setLoadMonitor(true); // 载入鼠标信息 filter.setLoadMouse(true); // 不使用任务链模式进行查询 noUseHandler(filter); // 使用任务链模式进行查询 useHandler(filter); /** * 通过两种查询对比,你会发现: * 1、查询结果都一样 * 2、没有使用任务链设计模式的service层代码非常臃肿,而使用了任务链的service层看起来很棒 * 3、如果一个service只干一件事的话,无疑第一种service干了很多事情,比如把电脑组件的加载都放在了当前类中 * 一旦这个service的代码越来越多,后期维护起来会很困难,不如每个组件的加载单独交给一个任务来处理, * 反正数据和参数都是固定的【这句话很重要,值得深思,如果参数不固定,要怎么改造呢? 自己尝试吧...】 * 4、使用了任务链的service,在查询结果的时候,只要知道参与此次查询的任务都有哪些,任务之间的衔接关系是什么 * 至于任务链中的每个任务都干了什么,不在当前service层中体现出来 * 总之,给我数据和条件,剩下的你就不用管了,我最后给你返回正确的结果就行了 */ } private static void useHandler(ComputerFilter filter) { ComputerQueryService2 q2 = new ComputerQueryService2(); List<Computer> computers2 = q2.query(filter); System.out.println(JsonUtils.objectToJson(computers2)); } private static void noUseHandler(ComputerFilter filter) { ComputerQueryService1 q1 = new ComputerQueryService1(); List<Computer> computers1 = q1.query(filter); System.out.println(JsonUtils.objectToJson(computers1)); System.out.println("======================分割线"); } }
2.抽象任务链
package com.zhaoshuangjian.mode17_责任链模式.mode17; import com.zhaoshuangjian.utils.IdsMaker; /** * <p>抽象任务链</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public abstract class AbstractHandler <TData,TFilter>{ protected IdsMaker idsMaker = new IdsMaker(1,31); /** * 任务链传递的数据【消费数据And最终数据】 */ protected TData data; /** * 当前任务的下一个任务链,即当前任务执行完成后,交给它下面的任务链继续执行,直到nextHandler == null */ protected com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler<TData,TFilter> nextHandler = null; /** * 抽象过滤器方法 == 具体实现交由具体的任务类执行,参数是具体的查询过滤器 * @param filter * @return */ public abstract TData filter(TFilter filter); public TData getData() { return data; } public void setData(TData data) { this.data = data; } public com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler<TData,TFilter> getNextHandler() { return nextHandler; } public void setNextHandler(com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler<TData,TFilter> nextHandler) { this.nextHandler = nextHandler; } public static <TFilter> Object process(com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler handler , TFilter filter){ com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler nextHandler = handler; Object data = handler.getData(); do{ nextHandler.setData(data); data = nextHandler.filter(filter); nextHandler = nextHandler.getNextHandler(); }while (nextHandler !=null); return data; } }
3.电脑处理事件
package com.zhaoshuangjian.mode17_责任链模式.Handler; import com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler; import com.zhaoshuangjian.mode17_责任链模式.mode17.filter.ComputerFilter; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.Computer; import java.util.ArrayList; import java.util.HashSet; import java.util.List; /** * <p>电脑处理事件</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public class ComputerHandler extends AbstractHandler<List<Computer>,ComputerFilter> { @Override public List<Computer> filter(ComputerFilter filter) { // 由于是模拟数据,不要在乎实现细节 == 数据都是临时拼凑的,正常应该是从数据库中拿 List<Computer> computers = new ArrayList<>(); HashSet<Long> ids = filter.getIds(); for (Long id : ids) { Computer computer = new Computer("苹果公司",13488.00); computer.setId(id); computer.setName("Apple MacBook Pro 13.3英寸笔记本电脑 深空灰色 2018新款 -- "+id); computers.add(computer); } return computers; } }
4.键盘处理事件
package com.zhaoshuangjian.mode17_责任链模式.Handler; import com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler; import com.zhaoshuangjian.mode17_责任链模式.mode17.filter.ComputerFilter; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.Computer; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.KeyBord; import java.util.List; /** * <p>键盘处理事件</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public class KeyBordHandler extends AbstractHandler<List<Computer>,ComputerFilter> { @Override public List<Computer> filter(ComputerFilter filter) { List<Computer> computers = this.getData(); Boolean loadKeyBord = filter.getLoadKeyBord(); if(loadKeyBord){ for (Computer computer : computers) { KeyBord keyBord = new KeyBord("无线"); keyBord.setId(idsMaker.nextId()); keyBord.setName("雷柏键盘"); computer.setKeyBord(keyBord); } } return computers; } }
5.显示器处理事件
package com.zhaoshuangjian.mode17_责任链模式.Handler; import com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler; import com.zhaoshuangjian.mode17_责任链模式.mode17.filter.ComputerFilter; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.Computer; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.Monitor; import java.util.List; /** * <p>显示器处理事件</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public class MonitorHandler extends AbstractHandler<List<Computer>,ComputerFilter> { @Override public List<Computer> filter(ComputerFilter filter) { List<Computer> computers = this.getData(); Boolean loadMonitor = filter.getLoadMonitor(); if(loadMonitor){ for (Computer computer : computers) { Monitor monitor = new Monitor(); monitor.setId(idsMaker.nextId()); monitor.setSize(23); monitor.setName("三星显示器"); computer.setMonitor(monitor); } } return computers; } }
6.鼠标处理事件
package com.zhaoshuangjian.mode17_责任链模式.Handler; import com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler; import com.zhaoshuangjian.mode17_责任链模式.mode17.filter.ComputerFilter; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.Computer; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.Mouse; import java.util.List; /** * <p>鼠标处理事件</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public class MouseHandler extends AbstractHandler<List<Computer>,ComputerFilter> { @Override public List<Computer> filter(ComputerFilter filter) { List<Computer> computers = this.getData(); Boolean loadMouse = filter.getLoadMouse(); if(loadMouse){ for (Computer computer : computers) { Mouse mouse = new Mouse(); mouse.setId(idsMaker.nextId()); mouse.setName("罗技鼠标"); computer.setMouse(mouse); } } return computers; } }
7.抽象过滤器
package com.zhaoshuangjian.mode17_责任链模式.mode17.filter; import java.util.HashSet; /** * <p>抽象过滤器</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public abstract class AbstractFilter { /** * 根据ids查询 */ private HashSet<Long> ids = new HashSet<>(); /** * 根据names查询 */ private HashSet<String> names = new HashSet<>(); private Integer pageNum = 1; private Integer pageSize = 10; public HashSet<Long> getIds() { return ids; } public void setIds(HashSet<Long> ids) { this.ids = ids; } public void addIds(Long id){ this.ids.add(id); } public HashSet<String> getNames() { return names; } public void setNames(HashSet<String> names) { this.names = names; } public Integer getPageNum() { return pageNum; } public void setPageNum(Integer pageNum) { this.pageNum = pageNum; } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } }
8.电脑过滤器
package com.zhaoshuangjian.mode17_责任链模式.mode17.filter; /** * <p></p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public class ComputerFilter extends AbstractFilter { /** * 是否加载显示器 */ private Boolean loadMonitor = false; /** * 是否加载键盘 */ private Boolean loadKeyBord = false; /** * 是否加载鼠标 */ private Boolean loadMouse = false; public ComputerFilter(){ } public Boolean getLoadMonitor() { return loadMonitor; } public void setLoadMonitor(Boolean loadMonitor) { this.loadMonitor = loadMonitor; } public Boolean getLoadKeyBord() { return loadKeyBord; } public void setLoadKeyBord(Boolean loadKeyBord) { this.loadKeyBord = loadKeyBord; } public Boolean getLoadMouse() { return loadMouse; } public void setLoadMouse(Boolean loadMouse) { this.loadMouse = loadMouse; } }
9.抽象对象类
package com.zhaoshuangjian.mode17_责任链模式.mode17.model; /** * <p>抽象对象类</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public abstract class AbstractObject { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract void show(); }
10.组装电脑
package com.zhaoshuangjian.mode17_责任链模式.mode17.model; /** * <p>电脑【组装电脑】</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public class Computer extends AbstractObject{ /** * 公司 -- 如联想、华硕、苹果...etc */ private String company; /** * 价钱 */ private Double price; /** * 组成 -- 显示器 */ private Monitor monitor; /** * 组成 -- 键盘 */ private KeyBord keyBord; /** * 组成 -- 鼠标 */ private Mouse mouse; public Computer(String company,Double price){ this.company = company; this.price = price; } public Monitor getMonitor() { return monitor; } public void setMonitor(Monitor monitor) { this.monitor = monitor; } public KeyBord getKeyBord() { return keyBord; } public void setKeyBord(KeyBord keyBord) { this.keyBord = keyBord; } public Mouse getMouse() { return mouse; } public void setMouse(Mouse mouse) { this.mouse = mouse; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } @Override public void show() { System.out.println("电脑"); } }
11.键盘
package com.zhaoshuangjian.mode17_责任链模式.mode17.model; /** * <p>键盘</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public class KeyBord extends AbstractObject{ /** * 键盘类型 */ private String type; public KeyBord(String type){ this.type = type; } /** * @return the type */ public String getType() { return type; } /** * @param type the type to set */ public void setType(String type) { this.type = type; } @Override public void show() { System.out.println("键盘"); } }
12.显示器
package com.zhaoshuangjian.mode17_责任链模式.mode17.model; /** * <p>显示器</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public class Monitor extends AbstractObject{ /** * 尺寸 */ private Integer size; public Monitor(){ } public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } @Override public void show() { System.out.println("显示器"); } }
13.鼠标
package com.zhaoshuangjian.mode17_责任链模式.mode17.model; /** * <p>鼠标</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ public class Mouse extends AbstractObject{ @Override public void show() { System.out.println("鼠标"); } }
14.普通service业务层实现根据过滤器查询电脑信息
package com.zhaoshuangjian.mode17_责任链模式.mode17.query; import com.zhaoshuangjian.mode17_责任链模式.mode17.filter.ComputerFilter; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.Computer; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.KeyBord; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.Monitor; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.Mouse; import com.zhaoshuangjian.utils.IdsMaker; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashSet; import java.util.List; /** * <p>普通service业务层实现根据过滤器查询电脑信息</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ @Service public class ComputerQueryService1 { private IdsMaker idsMaker = new IdsMaker(1,31); public List<Computer> query(ComputerFilter filter){ List<Computer> computers = getComputers(filter); loadMonitors(computers,filter); loadKeyBords(computers,filter); loadMouses(computers,filter); return computers; } /** * 根据电脑查询过滤器 == 拿到对应的电脑实体数据 * @param filter * @return */ private List<Computer> getComputers(ComputerFilter filter){ // 由于是模拟数据,不要在乎实现细节 == 数据都是临时拼凑的,正常应该是从数据库中拿 List<Computer> computers = new ArrayList<>(); HashSet<Long> ids = filter.getIds(); for (Long id : ids) { Computer computer = new Computer("苹果公司",13488.00); computer.setId(id); computer.setName("Apple MacBook Pro 13.3英寸笔记本电脑 深空灰色 2018新款 -- "+id); computers.add(computer); } return computers; } /** * 根据查询过滤器加载电脑显示器 * @param computers 电脑集合 * @param filter 电脑查询过滤器 */ private void loadMonitors(List<Computer> computers ,ComputerFilter filter){ Boolean loadMonitor = filter.getLoadMonitor(); if(loadMonitor){ for (Computer computer : computers) { Monitor monitor = new Monitor(); monitor.setId(idsMaker.nextId()); monitor.setSize(23); monitor.setName("三星显示器"); computer.setMonitor(monitor); } } } /** * 根据查询过滤器加载电脑键盘 * @param computers 电脑集合 * @param filter 电脑查询过滤器 */ private void loadKeyBords(List<Computer> computers ,ComputerFilter filter){ Boolean loadKeyBord = filter.getLoadKeyBord(); if(loadKeyBord){ for (Computer computer : computers) { KeyBord keyBord = new KeyBord("无线"); keyBord.setId(idsMaker.nextId()); keyBord.setName("雷柏键盘"); computer.setKeyBord(keyBord); } } } /** * 根据查询过滤器加载电脑鼠标 * @param computers 电脑集合 * @param filter 电脑查询过滤器 */ private void loadMouses(List<Computer> computers ,ComputerFilter filter){ Boolean loadMouse = filter.getLoadMouse(); if(loadMouse){ for (Computer computer : computers) { Mouse mouse = new Mouse(); mouse.setId(idsMaker.nextId()); mouse.setName("罗技鼠标"); computer.setMouse(mouse); } } } }
15.使用了任务链的service业务层,实现根据过滤器查询电脑信息
package com.zhaoshuangjian.mode17_责任链模式.mode17.query; import com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler; import com.zhaoshuangjian.mode17_责任链模式.mode17.filter.ComputerFilter; import com.zhaoshuangjian.mode17_责任链模式.mode17.model.Computer; import com.zhaoshuangjian.mode17_责任链模式.Handler.ComputerHandler; import com.zhaoshuangjian.mode17_责任链模式.Handler.KeyBordHandler; import com.zhaoshuangjian.mode17_责任链模式.Handler.MonitorHandler; import com.zhaoshuangjian.mode17_责任链模式.Handler.MouseHandler; import org.springframework.stereotype.Service; import java.util.List; /** * <p>使用了任务链的service业务层,实现根据过滤器查询电脑信息</p> * * @Author zhaoshuangjian 2023-03-25 下午21:44 */ @Service public class ComputerQueryService2 { public List<Computer> query(ComputerFilter filter){ // 逐个创建任务 ComputerHandler computerHandler = new ComputerHandler(); MonitorHandler monitorHandler = new MonitorHandler() ; KeyBordHandler keyBordHandler = new KeyBordHandler() ; MouseHandler mouseHandler = new MouseHandler(); // 设置任务之间的关系 == 设置任务之间的链条 computerHandler.setNextHandler(monitorHandler); monitorHandler.setNextHandler(keyBordHandler); keyBordHandler.setNextHandler(mouseHandler); //执行任务链,从第一个任务事件开始执行,最终执行完拿到结果 List<Computer> computers = (List<Computer>) com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler.process(computerHandler, filter); return computers; } }