链式编程是将多个操作通过点号"."链接在一起成为一个整体,从而更加的简洁方便。链式编程的原理就是每个操作完成后都会返回一个this对象,也就是返回对象本身!在生产实际环境的应用如下所示:
public void filter(OrderRequest request, Param param, OrderDto orderDto, Map<String, Object> map) { new OrderContext(request, extParam, orderDto, map) .check() .createOrderDto() .synOrder(); }
构建一个内部类OrderContext,在内部类里面设置一个构造函数,传入参数,然后改内部类里面定义业务函数,然后返回orderContext对象,这样完成链式编程的逻辑思想。
protected class OrderContext {
OrderSubmitRequest request;
Param param;
OrderDto orderDto;
Map<String, Object> map;
public OrderContext(OrderRequest request, Param param, OrderDto orderDto, Map<String, Object> map){
this.request = request;
this.param = param;
this.orderDto = orderDto;
this.map = map;
}
}
public OrderContext check(){ // 各种逻辑校验,实现业务逻辑 return this; }
public OrderContext createOrderDto(){ // 可以使用上面定义的参数,实现业务逻辑 return this; }
总结:链式编程链式编程可以使得代码可读性高,链式编程的原理就是返回一个this对象,就是返回本身,达到链式效果。参考博客如下所示:
https://blog.csdn.net/weixin_38218338/article/details/105199787
标签:总结,map,OrderContext,编程,request,链式,orderDto From: https://www.cnblogs.com/jelly12345/p/16626849.html