“小马,这是你写的代码?”
“没错,这是我写的诗!”
“你管这叫诗?确定不是读三声?”

“好家伙,瞧不起我crud工程师是吧,那么久的流水账代码白写的?我承认,It’s ugly , But useful 啊,you can you up!”
“我来就我来,看好,看好,让你看看我写的诗”

从上面的流水代码中,可以看出,分别从三个行情来源里找数据,优先级1>2>3,找到数据返回就好了。

构建Request对象

/*** @author: mk* @create: 2021-11-22 19:18**/
@Data
@AllArgsConstructor
@ToString
public class Request<T> {private T data;
}

构建结果对象Result

/**  结果对象* @author: mk* @create: 2021-11-22 19:13**/
@Data
@AllArgsConstructor
@ToString
public class Result {private boolean isRatify;private String info;public void append(Result result) {Objects.requireNonNull(result);if (result.getInfo() != null) {this.info = String.join(this.info, result.getInfo());}}
}

定义一个接口处理Request获取Result

public interface Ratify {// 处理请求Result execute(Chain chain);interface Chain<T> {// 获取当前requestRequest<T> request();// 转发requestResult proceed(Request<T> request);}
}

接口的实现

@Data
@AllArgsConstructor
public class RealChain implements Ratify.Chain{private Request request;private List<Ratify> ratifyList;private int index;/*** 返回当前Request对象或者返回当前进行包装后的Request对象** @param* @return 结果*/@Overridepublic Request request() {return request;}/****具体转发功能* @param* @return 结果*/@Overridepublic Result proceed(Request request) {Result proceed = new Result(true, "");if (ratifyList.size() > index) {RealChain realChain = new RealChain(request,ratifyList, index + 1);Ratify ratify = ratifyList.get(index);proceed.append(ratify.execute(realChain));}return proceed;}
}

再来三个责任对象,实现Ratify接口。

@Slf4j
@Component
public class FirstMarketSource implements Ratify {@Autowiredprivate SecuritiesQuotationMapper securitiesQuotationMapper;@Overridepublic Result execute(Chain chain) {Request<MarketSourceDTO> request = chain.request();MarketSourceDTO data = request.getData();if(!StringUtils.isNull(data.getPriceSource1())){return process(data, chain);}else{return chain.proceed(chain.request());}}private Result process(MarketSourceDTO marketSourceDTO, Chain chain) {SecuritiesQuotationDO securitiesQuotationDO = securitiesQuotationMapper.getSecuritiesQuotationByCmd(new GetSecuritiesQuotationQuery(marketSourceDTO.getProductCode(),marketSourceDTO.getStockCode(), marketSourceDTO.getSecurityChildType(), marketSourceDTO.getMarket(), marketSourceDTO.getPriceSource1(), Integer.valueOf(marketSourceDTO.getKeepFigures1()), marketSourceDTO.getBizDate()));if(!Objects.isNull(securitiesQuotationDO)){//有数据 价格来源1取得数据  直接返回PricingDTO pricingDTO = PricingConvertor.INSTANCE.securitiesQuotationDoConvertor(securitiesQuotationDO);return new Result(true, GsonUtil.toJson(pricingDTO));}else{return chain.proceed(new Request(marketSourceDTO));}}
}
@Slf4j
@Component
public class TwoMarketSource implements Ratify {@Autowiredprivate SecuritiesQuotationMapper securitiesQuotationMapper;@Overridepublic Result execute(Chain chain) {Request<MarketSourceDTO> request = chain.request();MarketSourceDTO data = request.getData();if(!StringUtils.isNull(data.getPriceSource2())){return process(data, chain);}else{return chain.proceed(chain.request());}}private Result process(MarketSourceDTO marketSourceDTO, Chain chain) {SecuritiesQuotationDO securitiesQuotationDO =securitiesQuotationMapper.getSecuritiesQuotationByCmd(new GetSecuritiesQuotationQuery(marketSourceDTO.getProductCode(),marketSourceDTO.getStockCode(),marketSourceDTO.getSecurityChildType(),marketSourceDTO.getMarket(),marketSourceDTO.getPriceSource2(),Integer.valueOf(marketSourceDTO.getKeepFigures2()),marketSourceDTO.getBizDate()));if(!Objects.isNull(securitiesQuotationDO)){//有数据 价格来源2取得数据  直接返回PricingDTO pricingDTO = PricingConvertor.INSTANCE.securitiesQuotationDoConvertor(securitiesQuotationDO);return new Result(true, GsonUtil.toJson(pricingDTO));}else{return chain.proceed(new Request(marketSourceDTO));}}
}
@Slf4j
@Component
public class ThreeMarketSource implements Ratify {@Autowiredprivate SecuritiesQuotationMapper securitiesQuotationMapper;@Overridepublic Result execute(Chain chain) {Request<MarketSourceDTO> request = chain.request();MarketSourceDTO data = request.getData();if(!StringUtils.isNull(data.getPriceSource3())){return process(data, chain);}else{return chain.proceed(chain.request());}}private Result process(MarketSourceDTO marketSourceDTO, Chain chain) {SecuritiesQuotationDO securitiesQuotationDO =securitiesQuotationMapper.getSecuritiesQuotationByCmd(new GetSecuritiesQuotationQuery(marketSourceDTO.getProductCode(),marketSourceDTO.getStockCode(),marketSourceDTO.getSecurityChildType(),marketSourceDTO.getMarket(),marketSourceDTO.getPriceSource3(),Integer.valueOf(marketSourceDTO.getKeepFigures3()),marketSourceDTO.getBizDate()));if(!Objects.isNull(securitiesQuotationDO)){//有数据 价格来源3取得数据  直接返回PricingDTO pricingDTO = PricingConvertor.INSTANCE.securitiesQuotationDoConvertor(securitiesQuotationDO);return new Result(true, GsonUtil.toJson(pricingDTO));}else{return chain.proceed(new Request(marketSourceDTO));}}
}

方便调用,再来个该责任链模式的客户端工具类

@Component
public class ChainOfResponsibilityClient {private final FirstMarketSource firstMarketSource;private final TwoMarketSource twoMarketSource;private final ThreeMarketSource threeMarketSource;private final ArrayList<Result> failedResults;public ChainOfResponsibilityClient(FirstMarketSource firstMarketSource, TwoMarketSource twoMarketSource, ThreeMarketSource threeMarketSource) {this.firstMarketSource = firstMarketSource;this.twoMarketSource = twoMarketSource;this.threeMarketSource = threeMarketSource;this.failedResults = new ArrayList<>();}public Result execute(Request<MarketSourceDTO> request) {ArrayList<Ratify> arrayList = new ArrayList<>();arrayList.add(firstMarketSource);arrayList.add(twoMarketSource);arrayList.add(threeMarketSource);RealChain realChain = new RealChain(request,arrayList, 0);return realChain.proceed(request);}}

使用更简单了,注入ChainOfResponsibilityClient

client.execute(new Request<>(marketSourceDTO))

“看到没,看到没,你写的那些流水账代码,我直接一行代码搞定了”
“嗯,cv一下,这代码是我的了…”

可恶啊,被他用责任链给装到了相关推荐

  1. 一起学设计模式 - 责任链模式

    责任链模式(ChainOfResponsibilityPattern)属于 行为型模式的一种,将请求沿着一条链传递,直到该链上的某个对象处理它为止. 概述 定义如下:一个请求有多个对象来处理,这些对象 ...

  2. 一文带你玩转设计模式之「责任链」

    前言 对于已经工作了的小伙伴,你应该是见过"责任链"这种面向对象的设计模式的,还在上学的小伙伴也不用着急,你迟早会接触到的.本文旨在让小白同学和不太熟悉责任链的朋友能够迅速对这一设 ...

  3. [转]《JAVA与模式》之责任链模式

    http://www.cnblogs.com/java-my-life/archive/2012/05/28/2516865.html 在阎宏博士的<JAVA与模式>一书中开头是这样描述责 ...

  4. 《JAVA与模式》之责任链模式

    2019独角兽企业重金招聘Python工程师标准>>> 详细请访问原博客:http://www.cnblogs.com/java-my-life/archive/2012/05/28 ...

  5. java设计模式之责任链模式以及在java中作用

    责任链模式是一种对象的行为模式.在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链上的哪一个 ...

  6. JAVA责任链设计模式

    <JAVA与模式>之责任链模式 在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模 ...

  7. android 链式结构,Android 架构师之路10 设计模式之责任链模式

    1. 责任链模式概念 1.1 介绍 客户端发出一个请求,链上的对象都有机会来处理这一请求,而客户端不需要知道谁是具体的处理对象.这样就实现了请求者和接受者之间的解耦,并且在客户端可以实现动态的组合职责 ...

  8. 门面模式、调停者模式、责任链模式

    简介: 1.门面模式(facade) 门面模式:比如政府部门,你需要提交一些资料证明你自己是你自己,可能会从这个部门跑到那个部门,串来串去都办不好,但是如果有一个部门来统一协调这些部门之间的关系,而你 ...

  9. 秒懂设计模式之责任链模式(Chain Of Responsibility Pattern)

    [版权申明] 非商业目的注明出处可自由转载 博文地址:https://blog.csdn.net/ShuSheng0007/article/details/116138433 出自:shusheng0 ...

最新文章

  1. centos使用yum快速安装java的方法
  2. what's the 回撤
  3. matlab清除所有数据,清除仿真数据检查器中的所有数据
  4. python转csv_python如何将列表存储为csv文件
  5. HandlerThread
  6. 【转】matlab练习程序(奇异值分解压缩图像)
  7. php如何利用soap查看函数,使用PHP soap函数的自定义标题
  8. python 录制网易云登陆_小白都能看懂:Python爬取网易云音乐下载教程
  9. 计算机基础98均9,第三章 计算机基础 Windows98 (第二讲).ppt
  10. Python全栈之路Day16
  11. 用计算机考试时怎么返回桌面快捷键,win10系统如何使用返回桌面快捷键?快速返回到电脑桌面的方法...
  12. VTM10.0代码学习18:xCheckRDCostMerge2Nx2N()
  13. 康华光电子技术基础第六版习题答案
  14. 【领英如何一键批量有效地加好友?】
  15. iOS亮屏解锁命令【iOS自动化测试】--使用ssh
  16. Java Web 开发后续(二)
  17. Excel VBA Sheet1和Sheets(1)的区别
  18. Bzoj3261/洛谷P4735 最大异或和(可持久化Trie)
  19. matlab中的addemup是什么,毕业论文-rsa密码体制的设计及matlab语言下的实现
  20. linux源代码阅读组合vim tags taglist

热门文章

  1. 如何对质量数据进行分析?
  2. ACM训练日记—2月7日
  3. 私人定制情人节告白网站并且部署上线,谁说程序员没有爱!超详细步骤!源码分享!
  4. 模拟电路仿真LTspice(1):二极管特性曲线
  5. 华为p9 html尺寸,华为P9 Plus的屏幕尺寸是多少
  6. rancher2.0 beta版本 Kubernetes安装指南
  7. CTF解题记录-Misc-当铺秘密
  8. 底部孕线形态有哪些?底部孕线形态特征是什么?
  9. boost库在工作(23)任务之三
  10. Python-Web之前端基础介绍(上)