一、借贷平台代码分析
1、借贷平台的订单,有审核-发布-抢单 等步骤,随着操作的不同,会改变订单的状态,项目中的这个模块实现就会使用到状态模式;
2、通常使用 if/else 判断订单的状态,从而实现不同的逻辑,伪代码如下:
二、代码分析
1、类图
2、代码
状态接口类:
1 /**
2 * 状态接口
3 * @author Administrator
4 *
5 */
6 public interface State {
7
8 /**
9 * 电审
10 */
11 void checkEvent(Context context);
12
13 /**
14 * 电审失败
15 */
16 void checkFailEvent(Context context);
17
18 /**
19 * 定价发布
20 */
21 void makePriceEvent(Context context);
22
23 /**
24 * 接单
25 */
26 void acceptOrderEvent(Context context);
27
28 /**
29 * 无人接单失效
30 */
31 void notPeopleAcceptEvent(Context context);
32
33 /**
34 * 付款
35 */
36 void payOrderEvent(Context context);
37
38 /**
39 * 接单有人支付失效
40 */
41 void orderFailureEvent(Context context);
42
43 /**
44 * 反馈
45 */
46 void feedBackEvent(Context context);
47
48
49 String getCurrentState();
50 }
抽象状态类:
1 public abstract class AbstractState implements State {
2
3 protected static final RuntimeException EXCEPTION = new RuntimeException("操作流程不允许");
4
5 //抽象类,默认实现了 State 接口的所有方法
6 //该类的所有方法,其子类(具体的状态类),可以有选择的进行重写
7
8 @Override
9 public void checkEvent(Context context) {
10 throw EXCEPTION;
11 }
12
13 @Override
14 public void checkFailEvent(Context context) {
15 throw EXCEPTION;
16 }
17
18 @Override
19 public void makePriceEvent(Context context) {
20 throw EXCEPTION;
21 }
22
23 @Override
24 public void acceptOrderEvent(Context context) {
25 throw EXCEPTION;
26 }
27
28 @Override
29 public void notPeopleAcceptEvent(Context context) {
30 throw EXCEPTION;
31 }
32
33 @Override
34 public void payOrderEvent(Context context) {
35 throw EXCEPTION;
36 }
37
38 @Override
39 public void orderFailureEvent(Context context) {
40 throw EXCEPTION;
41 }
42
43 @Override
44 public void feedBackEvent(Context context) {
45 throw EXCEPTION;
46 }
47 }
各个状态实现类:
1 //各种具体状态类
2 class FeedBackState extends AbstractState {
3
4 @Override
5 public String getCurrentState() {
6 return StateEnum.FEED_BACKED.getValue();
7 }
8 }
9
10 class GenerateState extends AbstractState {
11
12 @Override
13 public void checkEvent(Context context) {
14 context.setState(new ReviewState());
15 }
16
17 @Override
18 public void checkFailEvent(Context context) {
19 context.setState(new FeedBackState());
20 }
21
22 @Override
23 public String getCurrentState() {
24 return StateEnum.GENERATE.getValue();
25 }
26 }
27
28 class NotPayState extends AbstractState {
29
30 @Override
31 public void payOrderEvent(Context context) {
32 context.setState(new PaidState());
33 }
34
35 @Override
36 public void feedBackEvent(Context context) {
37 context.setState(new FeedBackState());
38 }
39
40 @Override
41 public String getCurrentState() {
42 return StateEnum.NOT_PAY.getValue();
43 }
44 }
45
46 class PaidState extends AbstractState {
47
48 @Override
49 public void feedBackEvent(Context context) {
50 context.setState(new FeedBackState());
51 }
52
53 @Override
54 public String getCurrentState() {
55 return StateEnum.PAID.getValue();
56 }
57 }
58
59 class PublishState extends AbstractState {
60
61 @Override
62 public void acceptOrderEvent(Context context) {
63 //把当前状态设置为 NotPayState。。。
64 //至于应该变成哪个状态,有流程图来决定
65 context.setState(new NotPayState());
66 }
67
68 @Override
69 public void notPeopleAcceptEvent(Context context) {
70 context.setState(new FeedBackState());
71 }
72
73 @Override
74 public String getCurrentState() {
75 return StateEnum.PUBLISHED.getValue();
76 }
77 }
78
79 class ReviewState extends AbstractState {
80
81 @Override
82 public void makePriceEvent(Context context) {
83 context.setState(new PublishState());
84 }
85
86 @Override
87 public String getCurrentState() {
88 return StateEnum.REVIEWED.getValue();
89 }
90
91 }
状态枚举类:
1 /**
2 * 状态枚举类
3 * @author Administrator
4 *
5 */
6 public enum StateEnum {
7
8 //订单生成
9 GENERATE(1, "GENERATE"),
10
11 //已审核
12 REVIEWED(2, "REVIEWED"),
13
14 //已发布
15 PUBLISHED(3, "PUBLISHED"),
16
17 //待付款
18 NOT_PAY(4, "NOT_PAY"),
19
20 //已付款
21 PAID(5, "PAID"),
22
23 //已完结
24 FEED_BACKED(6, "FEED_BACKED");
25
26 private int key;
27 private String value;
28
29 StateEnum(int key, String value) {
30 this.key = key;
31 this.value = value;
32 }
33 public int getKey() {return key;}
34 public String getValue() {return value;}
35 }
Context 上下文对象:
1 //环境上下文
2 public class Context extends AbstractState {
3 //当前的状态 state, 根据我们的业务流程处理,不停的变化
4 private State state;
5
6 @Override
7 public void checkEvent(Context context) {
8 state.checkEvent(this);
9 getCurrentState();
10 }
11
12 @Override
13 public void checkFailEvent(Context context) {
14 state.checkFailEvent(this);
15 getCurrentState();
16 }
17
18 @Override
19 public void makePriceEvent(Context context) {
20 state.makePriceEvent(this);
21 getCurrentState();
22 }
23
24 @Override
25 public void acceptOrderEvent(Context context) {
26 state.acceptOrderEvent(this);
27 getCurrentState();
28 }
29
30 @Override
31 public void notPeopleAcceptEvent(Context context) {
32 state.notPeopleAcceptEvent(this);
33 getCurrentState();
34 }
35
36 @Override
37 public void payOrderEvent(Context context) {
38 state.payOrderEvent(this);
39 getCurrentState();
40 }
41
42 @Override
43 public void orderFailureEvent(Context context) {
44 state.orderFailureEvent(this);
45 getCurrentState();
46 }
47
48 @Override
49 public void feedBackEvent(Context context) {
50 state.feedBackEvent(this);
51 getCurrentState();
52 }
53
54 public State getState() {
55 return state;
56 }
57
58 public void setState(State state) {
59 this.state = state;
60 }
61
62 @Override
63 public String getCurrentState() {
64 System.out.println("当前状态 : " + state.getCurrentState());
65 return state.getCurrentState();
66 }
67 }
客户端:
1 /**测试类*/
2 public class ClientTest {
3
4 public static void main(String[] args) {
5 //创建context 对象
6 Context context = new Context();
7 //将当前状态设置为 PublishState
8 context.setState(new PublishState());
9 System.out.println(context.getCurrentState());
10
11 // //publish --> not pay
12 context.acceptOrderEvent(context);
13 // //not pay --> paid
14 context.payOrderEvent(context);
15 // // 失败, 检测失败时,会抛出异常
16 // try {
17 // context.checkFailEvent(context);
18 // System.out.println("流程正常..");
19 // } catch (Exception e) {
20 // System.out.println(e.getMessage());
21 // }
22
23 }
24
25 }