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;
/**
* 责任链测试
*
* @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 computers2 = q2.query(filter);
System.out.println(JsonUtils.objectToJson(computers2));
}
private static void noUseHandler(ComputerFilter filter) {
ComputerQueryService1 q1 = new ComputerQueryService1();
List computers1 = q1.query(filter);
System.out.println(JsonUtils.objectToJson(computers1));
System.out.println("======================分割线");
}
}
package com.zhaoshuangjian.mode17_责任链模式.mode17;
import com.zhaoshuangjian.utils.IdsMaker;
/**
* 抽象任务链
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
public abstract class AbstractHandler {
protected IdsMaker idsMaker = new IdsMaker(1,31);
/**
* 任务链传递的数据【消费数据And最终数据】
*/
protected TData data;
/**
* 当前任务的下一个任务链,即当前任务执行完成后,交给它下面的任务链继续执行,直到nextHandler == null
*/
protected com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler 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 getNextHandler() {
return nextHandler;
}
public void setNextHandler(com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler nextHandler) {
this.nextHandler = nextHandler;
}
public static 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;
}
}
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;
/**
* 电脑处理事件
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
public class ComputerHandler extends AbstractHandler,ComputerFilter> {
@Override
public List filter(ComputerFilter filter) {
// 由于是模拟数据,不要在乎实现细节 == 数据都是临时拼凑的,正常应该是从数据库中拿
List computers = new ArrayList<>();
HashSet 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;
}
}
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;
/**
* 键盘处理事件
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
public class KeyBordHandler extends AbstractHandler,ComputerFilter> {
@Override
public List filter(ComputerFilter filter) {
List 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;
}
}
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;
/**
* 显示器处理事件
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
public class MonitorHandler extends AbstractHandler,ComputerFilter> {
@Override
public List filter(ComputerFilter filter) {
List 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;
}
}
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;
/**
* 鼠标处理事件
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
public class MouseHandler extends AbstractHandler,ComputerFilter> {
@Override
public List filter(ComputerFilter filter) {
List 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;
}
}
package com.zhaoshuangjian.mode17_责任链模式.mode17.filter;
import java.util.HashSet;
/**
* 抽象过滤器
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
public abstract class AbstractFilter {
/**
* 根据ids查询
*/
private HashSet ids = new HashSet<>();
/**
* 根据names查询
*/
private HashSet names = new HashSet<>();
private Integer pageNum = 1;
private Integer pageSize = 10;
public HashSet getIds() {
return ids;
}
public void setIds(HashSet ids) {
this.ids = ids;
}
public void addIds(Long id){
this.ids.add(id);
}
public HashSet getNames() {
return names;
}
public void setNames(HashSet 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;
}
}
package com.zhaoshuangjian.mode17_责任链模式.mode17.filter;
/**
*
*
* @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;
}
}
package com.zhaoshuangjian.mode17_责任链模式.mode17.model;
/**
* 抽象对象类
*
* @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();
}
package com.zhaoshuangjian.mode17_责任链模式.mode17.model;
/**
* 电脑【组装电脑】
*
* @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("电脑");
}
}
package com.zhaoshuangjian.mode17_责任链模式.mode17.model;
/**
* 键盘
*
* @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("键盘");
}
}
package com.zhaoshuangjian.mode17_责任链模式.mode17.model;
/**
* 显示器
*
* @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("显示器");
}
}
package com.zhaoshuangjian.mode17_责任链模式.mode17.model;
/**
* 鼠标
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
public class Mouse extends AbstractObject{
@Override
public void show() {
System.out.println("鼠标");
}
}
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;
/**
* 普通service业务层实现根据过滤器查询电脑信息
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
@Service
public class ComputerQueryService1 {
private IdsMaker idsMaker = new IdsMaker(1,31);
public List query(ComputerFilter filter){
List computers = getComputers(filter);
loadMonitors(computers,filter);
loadKeyBords(computers,filter);
loadMouses(computers,filter);
return computers;
}
/**
* 根据电脑查询过滤器 == 拿到对应的电脑实体数据
* @param filter
* @return
*/
private List getComputers(ComputerFilter filter){
// 由于是模拟数据,不要在乎实现细节 == 数据都是临时拼凑的,正常应该是从数据库中拿
List computers = new ArrayList<>();
HashSet 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 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 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 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);
}
}
}
}
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;
/**
* 使用了任务链的service业务层,实现根据过滤器查询电脑信息
*
* @Author zhaoshuangjian 2023-03-25 下午21:44
*/
@Service
public class ComputerQueryService2 {
public List 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 computers = (List) com.zhaoshuangjian.mode17_责任链模式.mode17.AbstractHandler.process(computerHandler, filter);
return computers;
}
}