今天查找線上問題,看到一個讓我腦洞大開的工作流實現方式。以前用過責任鏈模式,也用過模板模式實現類工作流的方式,但是對比這個工具,遜色不少,不賣關子了,就是apache commons chain,它是command模式與責任鏈模式的綜合體。
1、apache commons chain 中的角色有:chain、context、command。
2、在我們訂單系統有這樣的業務,就是退票的時候,會根據核損后的訂單價格,給客人退錢,但是訂單的金額,由幾部分組成
有現金、商旅卡、有優惠券。所以根據需求,我們需要一個工作流來走下退款流程,我們的流程流轉的步驟是這樣的:
先退商旅卡-----如果還有余額退現金-----------還有余額再退優惠券,分析一下這樣的需求,剛好可以用這個工具,直接上代碼了
先引入包
1
2
3
4
5
|
<dependency> <groupid>commons-chain</groupid> <artifactid>commons-chain</artifactid> <version> 1.2 </version> </dependency> |
編寫command
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * 退商旅卡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 ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * 退現金 * 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 ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** * 退優惠券 * 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 ; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * created by 一代天驕 on 2018/7/1. */ @data public class refundcontext extends contextbase { /** * 訂單號 */ private integer orderid; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
/** * * 退票的工作流實現 * 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); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/vacblog/article/details/80875788