在电商系统中 订单的状态包含很多种,而且状态之间的切换逻辑也比较多。
这里通过状态模式给出简化代码中 if else 这种判断逻辑的方式。
不同的状态通过 状态内部提供的方法 跳转。
如下(真是情况比较复杂,这里只是一个 demon):
状态 | 方法 | 状态 |
---|---|---|
neworder state | pay () | payed state |
neworder state | cancel () | cancel state |
payed state | ship () | shiped state |
payed state | cancel () | cancel state |
ship state | cancel () | cancel state |
代码如下
订单基类
/**
* @author river
* @date 2019/3/11 17:55
**/
@Data
public abstract class OrderState {
private Order order;
public OrderState(Order order) {
this.order = order;
}
public OrderState newOrder(){
System.out.println("you can't do this");
return this;
}
public OrderState cancel(){
System.out.println("you can't do this");
return this;
}
public OrderState ship(){
System.out.println("you can't do this");
return this;
}
public OrderState pay(){
System.out.println("you can't do this");
return this;
}
}
状态子类
新订单状态:
/**
* @author river
* @date 2019/3/11 17:54
**/
public class NewOrderState extends OrderState{
public NewOrderState(Order order) {
super(order);
}
@Override
public OrderState pay() {
System.out.println("new ===> payed");
return new PayedState(this.getOrder());
}
@Override
public OrderState cancel() {
System.out.println("new ===> cancel");
return new CancelState(this.getOrder());
}
}
已支付状态:
/**
* 已支付状态
* @author river
*/
public class PayedState extends OrderState {
public PayedState(Order order) {
super(order);
}
@Override
public OrderState ship() {
System.out.println("payed ===> ship");
return new ShipState(this.getOrder());
}
@Override
public OrderState cancel() {
return new CancelState(this.getOrder());
}
}
已邮递状态
/**
* @author river
* @date 2019/3/11 18:19
**/
public class ShipedState extends OrderState {
public ShipedState(Order order) {
super(order);
}
@Override
public OrderState cancel() {
return new CancelState(this.getOrder());
}
}
订单类
/**
* @author river
* @date 2019/3/11 18:13
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Order {
private String id;
private String desc;
private OrderState currState;
public Order(String id, String desc) {
this.id = id;
this.desc = desc;
}
public void newOrder(){
System.out.println("order:newOrder");
currState=new NewOrderState(this);
}
public void pay(){
System.out.println("order:pay");
currState=currState.pay();
}
public void ship(){
System.out.println("order:ship");
currState=currState.ship();
}
public void cancel(){
System.out.println("order:cancel");
currState = currState.cancel();
}
}
测试demon
/**
* @author river
* @date 2019/3/11 18:22
**/
public class StateDemon {
public static void main(String[] args) {
Order order = new Order("QT001235","测试订单");
order.setCurrState(new NewOrderState(order));
order.newOrder();
order.pay();
order.ship();
order.cancel();
//取消后 不可以邮递、支付
order.ship();
order.pay();
}
}
测试输出
order:newOrder
order:pay
new ===> payed
order:ship
payed ===> ship
order:cancel
order:ship
you can’t do this
order:pay
you can’t do this
看到结果按照我们定义的规则输出了。
父类中方法定义 默认是不可以跳转。
只有子类重写了父类的方法才可以。
通过这个demon ,读者可以 为父类添加不同的跳转方法 和 提供更多的状态子类 来重写父类的方法,满足业务上复杂的需求。