“小马,这是你写的代码?”
“没错,这是我写的诗!”
“你管这叫诗?确定不是读三声?”
“好家伙,瞧不起我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> {
// 获取当前request
Request<T> request();
// 转发request
Result 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 结果
*/
@Override
public Request request() {
return request;
}
/**
*
*具体转发功能
* @param
* @return 结果
*/
@Override
public 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 {
@Autowired
private SecuritiesQuotationMapper securitiesQuotationMapper;
@Override
public 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 {
@Autowired
private SecuritiesQuotationMapper securitiesQuotationMapper;
@Override
public 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 {
@Autowired
private SecuritiesQuotationMapper securitiesQuotationMapper;
@Override
public 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一下,这代码是我的了…”