浅谈JAVA工作流的优雅实现方式

网友投稿 328 2023-01-18


浅谈JAVA工作流的优雅实现方式

今天查找线上问题,看到一个让我脑洞大开的工作流实现方式。以前用过责任链模式,也用过模板模式实现类工作流的方式,但是对比这个工具,逊色不少,不卖关子了,就是Apache Commons Chain,它是Command模式与责任链模式的综合体。

1、Apache Commons Chain 中的角色有:chain、context、command。

2、在我们订单系统有这样的业务,就是退票的时候,会根据核损后的订单价格,给客人退钱,但是订单的金额,由几部分组成

有现金、商旅卡、有优惠券。所以根据需求,我们需要一个工作流来走下退款流程,我们的流程流转的步骤是这样的:

先退商旅卡-----如果还有余额退现金-----------还有余额再退优惠券,分析一下这样的需求,刚好可以用这个工具,直接上代码了

先引入包

commons-chain

commons-chain

1.2

编写command

/**

* 退商旅卡Cash

* Created by 一代天骄 on 2018/7/1.

*/

@Slf4j

public class RefundBusinessCardCommand implements Command{

public boolean execute(Context context) throws Exception {

RefundContext refundContext = (RefundContext) context;

log.info("orderId:{} 退款开始,第一步:退商旅卡,金额:{}",refundContext.getOrderId(),"10");

return false;

}

}

/**

* 退现金

* Created by 一代天骄 on 2018/7/1.

*/

@Slf4j

public class RefundCashCommand implements Command {

public boolean execute(Context context) throws Exception {

RefundContext refundContext = (RefundContext) context;

log.info("orderId:{}退款开始,第二步:退现金,金额:{}",refundContext.getOrderId(),"5");

return false;

}

}

/**

* 退优惠券

* Created by 一代天骄 on 2018/7/1.

*/

@Slf4j

public class RefundPromotionCommand implements Command{

public boolean execute(Context context) throws Exception {

RefundContext refundContext = (RefundContext) context;

log.info("orderId:{} 退款开始,第二步:退优惠券,金额:{}",refundContext.getOrderId(),"20");

return false;

}

}

/**

* Created by 一代天骄 on 2018/7/1.

*/

@Data

public class RefundContext extends ContextBase {

/**

* 订单号

*/

private Integer orderId;

}

/**

*

* 退票的WBXEHU工作流实现

* Created by 一代天骄 on 2018/7/1.

*/

public class RefundTicketChain extends ChainBase {

public void init() {

//退商旅卡

this.addCommand(new RefundBusinessCardCommand());

//退现金

this.addCommand(new RefundCashCommand());

//退优惠券

this.addCommand(newhttp:// RefundPromotionCommand());

}

public static void main(String[] args) throws Exception {

RefundTicketChain refundTicketChain = new RefundTicketChain();

refundTicketChain.init();

RefundContext context = new RefundContext();

context.setOrderId(1621940242);

refundTicketChain.execute(context);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mybatis中collection和association的使用区别详解
下一篇:汽车集成研发管理平台(车辆集成信息)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~