在一些復雜的業(yè)務當中场刑,涉及到某個業(yè)務有多個狀態(tài)鳄乏,按照傳統(tǒng)寫法無非就是if else
,其實還是有一種比較優(yōu)雅的實現(xiàn)方式区丑,就是設計模式中的狀態(tài)機模式拧粪。沒有spring
之前雖然也能實現(xiàn)狀態(tài)機模式,但是并不優(yōu)雅沧侥。下面來說一個用springboot
來實現(xiàn)狀態(tài)機模式的案例可霎。
舉一個例子,比如訂單:訂單當中涉及到多個狀態(tài)的跳轉正什,有的時候還需要對修改狀態(tài)前的邏輯進行判斷啥纸,這個時候用狀態(tài)機模式就是很好的實現(xiàn)号杏。
1婴氮、引入部分依賴
<groupId>org.example</groupId>
<artifactId>order_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.0.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
主要的包結構:
2、訂單類:
/**
* @version 1.0
* @date 2021/6/9 21:33
* 訂單類
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Order implements Serializable {
/**
* id
*/
private Long id;
/**
* 金額
*/
private BigDecimal price;
/**
* 狀態(tài)
*/
private Integer status;
/**
* 創(chuàng)建時間
*/
private Date createTime;
}
3盾致、創(chuàng)建訂單狀態(tài)處理超類
package org.example.bll;
import org.example.pojo.Order;
/**
* @version 1.0
* @date 2021/6/9 21:43
* 訂單狀態(tài)處理超類
*/
public interface OrderState {
/**
* 原來狀態(tài)
* @param newOrder 新訂單
* @param oldOrder 老訂單
*/
void handle(Order newOrder,Order oldOrder);
}
4主经、各個狀態(tài)處理類
package org.example.bll;
import org.example.pojo.Order;
/**
* @version 1.0
* @date 2021/6/9 21:46
* 訂單各狀態(tài)處理
*/
public class OrderStateCommon implements OrderState {
@Override
public void handle(Order newOrder, Order oldOrder) {
}
/**
* 生成日志
*
* @param order 訂單
*/
public void buildOrderLog(Order order) {
//保存日志
System.out.println("保存日志訂單狀態(tài): " + order.getStatus());
}
}
import org.example.pojo.Order;
import org.springframework.stereotype.Component;
import java.util.Objects;
/**
* @version 1.0
* @date 2021/6/9 21:52
* 訂單狀態(tài): 1
*/
@Component(OrderStateFactory.ORDER_STATUS + 1)
public class OrderStateFor1 extends OrderStateCommon {
@Override
public void handle(Order newOrder, Order oldOrder) {
if (Objects.isNull(oldOrder)){
System.out.println("不做操作");
}
System.out.println("訂單狀態(tài):1 處理邏輯==>");
//保存日志
super.buildOrderLog(newOrder);
}
}
package org.example.bll;
import org.example.pojo.Order;
import org.springframework.stereotype.Component;
/**
* @version 1.0
* @date 2021/6/9 21:52
* 訂單狀態(tài): 2
*/
@Component(OrderStateFactory.ORDER_STATUS + 2)
public class OrderStateFor2 extends OrderStateCommon{
@Override
public void handle(Order newOrder, Order oldOrder) {
System.out.println("訂單狀態(tài):2 處理邏輯==>");
//保存日志
super.buildOrderLog(newOrder);
}
}
package org.example.bll;
import org.example.pojo.Order;
import org.springframework.stereotype.Component;
/**
* @version 1.0
* @date 2021/6/9 21:52
* 訂單狀態(tài): 3
*/
@Component(OrderStateFactory.ORDER_STATUS + 3)
public class OrderStateFor3 extends OrderStateCommon{
@Override
public void handle(Order newOrder, Order oldOrder) {
System.out.println("訂單狀態(tài):3 處理邏輯==>");
//保存日志
super.buildOrderLog(newOrder);
}
}
5、主要工廠類獲取主要處理類
package org.example.bll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author wenlinshan
* @version 1.0
* @date 2021/6/9 21:56
*/
@Component
public class OrderStateFactory {
/**
* 自定義bean名字前綴
*/
public static final String ORDER_STATUS = "orderStatus";
/**
* 注入到容器里面
*/
@Autowired
Map<String, OrderState> states = new ConcurrentHashMap<>(4);
/**
* 獲取對應狀態(tài)的執(zhí)行類
*
* @param status 狀態(tài)
* @return 具體狀態(tài)的執(zhí)行類
*/
public OrderState getState(Integer status) {
if (states.containsKey(ORDER_STATUS + status)) {
return states.get(ORDER_STATUS + status);
}
throw new RuntimeException("未找到執(zhí)行狀態(tài)的類");
}
}
6庭惜、主要的入口類代碼
package org.example.service.impl;
import org.example.bll.OrderState;
import org.example.bll.OrderStateFactory;
import org.example.pojo.Order;
import org.example.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Objects;
/**
* @version 1.0
* @date 2021/6/9 21:37
*/
@Service
public class OrderServiceImpl implements IOrderService {
@Autowired
private OrderStateFactory factory;
@Override
public void saveOrUpdateOrder(Order order) {
//查找出原來的訂單
Order oldOrder = this.getById(order.getId());
if (Objects.isNull(oldOrder)) {
//新增
this.save(order);
if (Objects.nonNull(order.getStatus())) {
//調用狀態(tài)機
factory.getState(order.getStatus()).handle(order,null);
}
return;
}
//修改
OrderState orderState = Objects.isNull(order.getStatus()) ? factory.getState(oldOrder.getStatus()) : factory.getState(order.getStatus());
orderState.handle(order,oldOrder);
}
至此罩驻,所有的代碼都已實現(xiàn),下面開始演示‘
7护赊、測試類:
package org.example;
import org.example.pojo.Order;
import org.example.service.IOrderService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.math.BigDecimal;
import java.util.Date;
/**
* @version 1.0
* @date 2021/6/9 22:03
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderTest {
@Autowired
private IOrderService orderService;
@Test
public void testOrder1() {
Order order = new Order(1L, BigDecimal.ONE, 2, new Date());
orderService.saveOrUpdateOrder(order);
}
}
運行結果: