java 函数式编程实现策略模式

2020-11-27 15:04:00
admin
原创
225
摘要:java 函数式编程实现策略模式

话不多说,直接怼代码

@RequestMapping(value = "/expressSubscribe", method = RequestMethod.POST)
    public Result expressSubscribe(@RequestParam("orderNo")String  orderNo
            ,@RequestParam("key")String key)  {
        Map<String, Function<String, Order>> checkResultDispatcherComX = new HashMap<>();
        checkResultDispatcherComX.put("key1", a -> bizService.bizOne(orderNo));
        checkResultDispatcherComX.put("key2", a -> bizService.bizTwo(orderNo));
        checkResultDispatcherComX.put("key3", a -> bizService.bizThree(orderNo));
        Order order=getCheckResultComX(checkResultDispatcherComX,key);
        return null;
    }
    public Order getCheckResultComX(Map<String, Function<String, Order>> checkResultDispatcherComX,
                                String key) {
        //写一段生成key的逻辑:
        Function<String, Order> result = checkResultDispatcherComX.get(key);
        if (result != null) {
            //执行这段表达式获得String类型的结果
            return result.apply(key);
        }
        return null;
    }
下面是业务的实现

@Service
public class BizService {
    @Autowired
    private OrderService orderService;
    public Order bizOne(String orderNo) {
        Order order=orderService.queryOneByOrderNo(orderNo);
        order.setRemark("各种花式操作1");
        return order;
    }
    public Order bizTwo(String orderNo) {
        Order order=orderService.queryOneByOrderNo(orderNo);
        order.setRemark( "各种花式操作2");
        return order;
    }
    public Order bizThree(String orderNo) {
        Order order=orderService.queryOneByOrderNo(orderNo);
        order.setRemark( "各种花式操作3");
        return order;
    }
}

发表评论
评论通过审核之后才会显示。
文章分类
联系方式
联系人: 郑州-小万
电话: 13803993919
Email: 1027060531@qq.com
QQ: 1027060531
网址: www.wanhejia.com