开发业务Demo逻辑处理之策略模式场景使用
程序员文章站
2024-03-25 23:51:04
...
代码如下:
1.TestItemBizService 和 TestOrderBizService 这两个类是真实业务逻辑处理
/**
*
* @Author:xuliangliang
* @Description:模拟订单业务逻辑处理
* @Date 2020/8/28
*/
@Service
public class TestOrderBizService {
/**
* 业务逻辑1
* @param order
* @return
*/
public ResultResponse bizOne(OrderPO order) {
//业务逻辑处理
ResultResponse<OrderPO> resultEntity = new ResultResponse(order.getId()+"业务逻辑1");
resultEntity.setIsSuccess(Boolean.TRUE);
resultEntity.setErrorCode("200");
resultEntity.setData(order);
return resultEntity;
}
/**
* 业务逻辑2
* @param order
* @return
*/
public ResultResponse bizTwo(OrderPO order) {
ResultResponse<OrderPO> resultEntity = new ResultResponse(order.getId()+"业务逻辑2");
resultEntity.setIsSuccess(Boolean.TRUE);
resultEntity.setErrorCode("200");
resultEntity.setData(order);
return resultEntity;
}
/**
* 业务逻辑3
* @param order
* @return
*/
public ResultResponse bizThree(OrderPO order) {
ResultResponse<OrderPO> resultEntity = new ResultResponse(order.getId()+"业务逻辑3");
resultEntity.setIsSuccess(Boolean.TRUE);
resultEntity.setErrorCode("200");
resultEntity.setData(order);
return resultEntity;
}
}
/**
*
* @Author:xuliangliang
* @Description:模拟商品业务逻辑处理单元
* @Date 2020/8/28
*/
@Service
public class TestItemBizService {
/**
* 业务逻辑1
* @param item
* @return
*/
public ResultResponse bizOne(ItemPO item) {
//业务逻辑处理
ResultResponse<ItemPO> resultEntity = new ResultResponse(item.getId()+"业务逻辑1");
resultEntity.setIsSuccess(Boolean.TRUE);
resultEntity.setErrorCode("200");
resultEntity.setData(item);
return resultEntity;
}
/**
* 业务逻辑2
* @param item
* @return
*/
public ResultResponse bizTwo(ItemPO item) {
ResultResponse<ItemPO> resultEntity = new ResultResponse(item.getId()+"业务逻辑2");
resultEntity.setIsSuccess(Boolean.TRUE);
resultEntity.setErrorCode("200");
resultEntity.setData(item);
return resultEntity;
}
/**
* 业务逻辑3
* @param item
* @return
*/
public ResultResponse bizThree(ItemPO item) {
ResultResponse<ItemPO> resultEntity = new ResultResponse(item.getId()+"业务逻辑3");
resultEntity.setIsSuccess(Boolean.TRUE);
resultEntity.setErrorCode("200");
resultEntity.setData(item);
return resultEntity;
}
}
2.ItemBizContext 和 OrderBizContext 这两个类是业务处理上下文,主要作用是为了根据传入参数标识位进行动态选择不通的业务逻辑处理点
/**
*
* @Author:xuLiangLiang
* @Description:订单上下文
* @Date 2020/9/9
*/
@AllArgsConstructor
@Component
public class OrderBizContext implements ContextProxyInterface<OrderPO, ResultResponse> {
private final TestOrderBizService testOrderBizService;
/**
*存储策略方法 测试订单业务逻辑
*/
private Map<ServiceFlagEnum, Function<OrderPO, ResultResponse>> checkResultDispatcherOrderComX = new ConcurrentHashMap<>();
/**
* 初始化 业务逻辑分派Map 其中value 存放的是 lambda表达式
*/
@PostConstruct
private void checkResultDispatcherComXInit() {
//订单业务
checkResultDispatcherOrderComX.put(ServiceFlagEnum.ORDER_MARK001, order -> testOrderBizService.bizOne(order));
checkResultDispatcherOrderComX.put(ServiceFlagEnum.ORDER_MARK002, order -> testOrderBizService.bizTwo(order));
checkResultDispatcherOrderComX.put(ServiceFlagEnum.ORDER_MARK003, order -> testOrderBizService.bizThree(order));
}
/**
* 获取策略逻辑
* @param order
* @param mark
* @return
*/
private ResultResponse getCheckResultOrderComX(OrderPO order, String mark) {
ServiceFlagEnum flagStrategy = ServiceFlagEnum.getFlagStrategy(mark);
if(flagStrategy == null){
return new ResultResponse("没有["+mark+"]标识业务");
}
Function<OrderPO, ResultResponse> result = checkResultDispatcherOrderComX.get(flagStrategy);
if (result != null) {
//执行这段表达式获得的结果
return result.apply(order);
}
return new ResultResponse("不在处理的逻辑中返回业务错误");
}
@Override
public ResultResponse doExecute(OrderPO orderEntity) {
return getCheckResultOrderComX(orderEntity, orderEntity.getMark());
}
}
/**
*
* @Author:xuliangliang
* @Description:商品上下文
* @Date 2020/8/28
*/
@Component
public class ItemBizContext implements ContextProxyInterface<ItemPO, ResultResponse> {
@Autowired
private TestItemBizService testItemBizService;
/**
* 存储策略方法 商品业务逻辑
*/
private Map<ServiceFlagEnum, ServiceStrategyInterface<ItemPO, ResultResponse>> checkResultDispatcherItemComX = new ConcurrentHashMap<>();
/**
* 初始化 业务逻辑分派Map 其中value 存放的是 lambda表达式
*/
@PostConstruct
private void checkResultDispatcherComXInit() {
//商品业务
checkResultDispatcherItemComX.put(ServiceFlagEnum.ITEM_MARK001, item-> testItemBizService.bizOne(item));
checkResultDispatcherItemComX.put(ServiceFlagEnum.ITEM_MARK002, item-> testItemBizService.bizTwo(item));
checkResultDispatcherItemComX.put(ServiceFlagEnum.ITEM_MARK003, item-> testItemBizService.bizThree(item));
}
/**
* 获取策略逻辑
* @param order
* @param mark
* @return
*/
private ResultResponse getCheckResultItemComX(ItemPO order, String mark) {
ServiceStrategyInterface<ItemPO, ResultResponse> result = checkResultDispatcherItemComX.get(ServiceFlagEnum.getFlagStrategy(mark));
if (result != null) {
//执行这段表达式获得的结果
return result.doExecute(order);
}
return new ResultResponse("不在处理的逻辑中返回业务错误");
}
@Override
public ResultResponse doExecute(ItemPO itemEntity) {
return getCheckResultItemComX(itemEntity, itemEntity.getMark());
}
}
3.ServiceContextProxy这个类的作用是代理多个业务上下文(2),因为实际开发过程中可能会出现不同业务的策略上下文模式
/**
*
* @Author:xuLiangLiang
* @Description:业务上下文代理
* @Date 2020/9/9
*/
@AllArgsConstructor
@Component
public class ServiceContextProxy<T>{
private ApplicationContext applicationContext;
/**
* 执行业务域
* @param t
* @param classBiz
* @return
*/
public ResultResponse execute(T t, Class<? extends ContextProxyInterface> classBiz) {
ContextProxyInterface<T, ResultResponse> contextProxyInterface = applicationContext.getBean(classBiz);
return contextProxyInterface.doExecute(t);
}
}
4.TestStrategyBizController 这个类是为了测试,但是也可以是对外开放的Controller或RPC接口
/**
*
* @Author:xuliangliang
* @Description:测试策略模式
* @Date 2020/8/28
*/
@RestController
public class TestStrategyBizController {
@Autowired
private ServiceContextProxy serviceContextProxy;
@PostMapping(value = "/v1/biz/test/order", produces="application/json")
public ResultResponse orderTest(OrderPO orderEntity) {
if(StringUtils.isEmpty(orderEntity.getMark())){
return new ResultResponse("没有Mark标识", false);
}
ResultResponse resultEntity = serviceContextProxy.execute(orderEntity, OrderBizContext.class);
return resultEntity;
}
@PostMapping(value = "/v1/biz/test/item", produces="application/json")
public ResultResponse itemTest(ItemPO itemEntity) {
if(StringUtils.isEmpty(itemEntity.getMark())){
return new ResultResponse("没有Mark标识", false);
}
ResultResponse resultEntity = serviceContextProxy.execute(itemEntity, ItemBizContext.class);
return resultEntity;
}
}
5.以上代码是策略模式主要核心代码,感兴趣的可以拉代码学习,这也是我在开发过程中根据业务需求进行编写的Demo
https://github.com/gitHub-good/strategy_demo.git
上一篇: entitas学习笔记二
下一篇: 方法定义、方法重载