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

所属分类: 软件编程 / java 阅读数: 24
收藏 0 赞 0 分享

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

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

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

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

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

先引入包

 <dependency>
      <groupId>commons-chain</groupId>
      <artifactId>commons-chain</artifactId>
      <version>1.2</version>
    </dependency>

编写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;
 
 
}
/**
 *
 * 退票的工作流实现
 * Created by 一代天骄 on 2018/7/1.
 */
public class RefundTicketChain extends ChainBase {
 
  public void init() {
    //退商旅卡
    this.addCommand(new RefundBusinessCardCommand());
    //退现金
    this.addCommand(new RefundCashCommand());
    //退优惠券
    this.addCommand(new 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);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Spring Boot 配置 IDEA和DevTools 热部署的方法

这篇文章主要介绍了Spring Boot 配置 IDEA和DevTools 热部署的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot使用Redis缓存的实现方法

这篇文章主要介绍了SpringBoot使用Redis缓存的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot中自定义参数绑定步骤详解

这篇文章主要介绍了SpringBoot中自定义参数绑定步骤详解,非常不错,具有参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Java实现abc字符串排列组合

这篇文章主要为大家详细介绍了JAVA实现abc字符串的排列组合,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java中后台线程实例解析

这篇文章主要介绍了Java中后台线程实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

ehcache模糊批量移除缓存的方法

本篇文章主要介绍了ehcache模糊批量移除缓存的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java多线程join方法实例代码

这篇文章主要介绍了Java多线程join方法实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现字符串排列组合问题

这篇文章主要为大家详细介绍了java实现字符串排列组合问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java排列组合字符串的方法

这篇文章主要介绍了Java排列组合字符串的方法
收藏 0 赞 0 分享

Java语言中的自定义类加载器实例解析

这篇文章主要介绍了Java语言中的自定义类加载器实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多