您现在的位置是:首页 > 文章详情

使用桥接模式来显示下单结果

日期:2019-09-03点击:254

用工厂方法模式来下不同订单 中我们看到,我们只简单显示来一个“下单成功”,但实际上我们需要给用户返回到结果可能多种多样。

先增加一个订单结果到接口。

public interface OrderSuccessResult { public OrderSuccessResult getResult(Order order); }

添加一个服务订单结果实现类

@Data @AllArgsConstructor @NoArgsConstructor @Builder public class ServiceOrderSuccessResult implements OrderSuccessResult { private String content;  private String storeName;  private String orderCode;  private String servicContent;  private Date serviceDate;  private String storeAddress;  private BigDecimal serviceAmount;   @Override  public OrderSuccessResult getResult(Order order) { ServiceOrderSuccessResult orderSuccessResult = ServiceOrderSuccessResult.builder() .content("服务商家正在审核,请耐心等待") .orderCode(((ServiceOrder)order).getCode()) .serviceAmount(((ServiceOrder)order).getService().getService().getPrice().getNormalPrice()) .serviceDate(((ServiceOrder)order).getServiceDate()) .servicContent(((ServiceOrder)order).getService().getSimpleContent()) .storeName(((ServiceOrder)order).getStore().getName()) .storeAddress(((ServiceOrder)order).getStore().getAddress().getName()) .build();  return orderSuccessResult;  } }

给Order接口添加一个适配方法

public interface Order { public void makeOrder(Order order);  public OrderSuccessResult getResult(Order order); }

增加一个订单抽象类,完成对订单结果接口的桥接

public abstract class AbstractOrder implements Order { protected OrderSuccessResult orderSuccessResult; }

修改各个Order的实现类,继承于该抽象类

@Data @AllArgsConstructor @NoArgsConstructor @ServiceOrderVersion(value = 1) @RequiredArgsConstructor public class ServiceOrder extends AbstractOrder { private Long id;  @NonNull  private String code;  @NonNull  private Store store;  @NonNull  private ProviderService service;  @NonNull  private Car car;  @NonNull  private Date serviceDate;  @NonNull  private String contact;  @NonNull  private String contactTel;  private AppUser user;  @NonNull  private String content;  private int status;  private Date createDate;   @Override  public void makeOrder(Order order) { ServiceOrderDao serviceOrderDao = SpringBootUtil.getBean(ServiceOrderDao.class);  IdService idService = SpringBootUtil.getBean(IdService.class);  ((ServiceOrder)order).setId(idService.genId());  ((ServiceOrder)order).setCode(getCode(idService));  AppUser loginAppUser = AppUserUtil.getLoginAppUser();  AppUser user = new AppUser();  user.setId(loginAppUser.getId());  user.setUsername(loginAppUser.getUsername());  ((ServiceOrder)order).setUser(user);  ((ServiceOrder)order).setStatus(1);  ((ServiceOrder)order).setCreateDate(new Date());  serviceOrderDao.save((ServiceOrder) order);  } @Override  public OrderSuccessResult getResult(Order order) { this.orderSuccessResult = new ServiceOrderSuccessResult();  return this.orderSuccessResult.getResult(order);  } private String getCode(IdService idService) { String flow = String.valueOf(idService.genId());  flow = flow.substring(14,flow.length());  String pre = DateUtils.format(new Date(), DateUtils.pattern9);  return pre + flow;  } }

我们可以看到this.orderSuccessResult = new ServiceOrderSuccessResult();ServiceOrderSuccessResult进行了耦合,此处需要修改。

Controller修改如下

@Slf4j @RestController public class OrderController { private ThreadLocal<OrderFactory> orderFactory = new ThreadLocal<>();  private ThreadLocal<Order> orderService = new ThreadLocal<>();  @Autowired  private OrderBean orderBean;   @Transactional  @SuppressWarnings("unchecked") @PostMapping("/makeeorder") public Result<OrderSuccessResult> makeOrder(@RequestBody String orderStr, @RequestParam("type") String type) { log.info(orderStr);         try {     Order order = setOrderFactory(orderStr,type);      orderService.get().makeOrder(order);     return Result.success(orderService.get().getResult(order));         }         finally {             orderFactory.remove();             orderService.remove();         }  } /**  * 判断是哪一种类型的订单来获取哪一种类型的具体订单工厂  * @param orderStr  * @return  */  private Order setOrderFactory(String orderStr,String type) { Class<?> classType = orderBean.getOrderMap().get(type);  Object order = JSONObject.parseObject(orderStr, classType); // if (orderStr.contains("service")) { // order = JSON.parseObject(orderStr, ServiceOrder.class); // }else if (orderStr.contains("product")) { // order = JSON.parseObject(orderStr, ProductOrder.class); // }  Class<?> classFactoryType = orderBean.getOrderFactoryMap().get(type + "Factory");  this.orderFactory.set((OrderFactory) SpringBootUtil.getBean(classFactoryType)); // if (order instanceof ServiceOrder) { // this.orderFactory.set(SpringBootUtil.getBean(ServiceOrderFactory.class)); // }else if (order instanceof ProductOrder) { // this.orderFactory.set(SpringBootUtil.getBean(ProductOrderFactory.class)); // }  orderService.set(orderFactory.get().getOrder());  return (Order) order;  } }

现在运行是没有问题的。返回结果如下

{
"code" : 200 ,
"data" : {
"content" : "服务商家正在审核,请耐心等待" ,
"orderCode" : "2019090353318" ,
"servicContent" : "精细洗车" ,
"serviceAmount" : 100 ,
"serviceDate" : "2019-10-03T00:00:00.000+0800" ,
"storeAddress" : "北京市海淀区西北旺" ,
"storeName" : "三润汽修厂"
},
"msg" : "操作成功"
}
现在我们用简单工厂进行解耦合。添加版本标签
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface ServiceResultVersion { int value(); }

ServiceOrderSuccessResult打上版本标签

@Data @AllArgsConstructor @NoArgsConstructor @Builder @ServiceResultVersion(value = 1) public class ServiceOrderSuccessResult implements OrderSuccessResult { private String content;  private String storeName;  private String orderCode;  private String servicContent;  private Date serviceDate;  private String storeAddress;  private BigDecimal serviceAmount;   @Override  public OrderSuccessResult getResult(Order order) { ServiceOrderSuccessResult orderSuccessResult = ServiceOrderSuccessResult.builder() .content("服务商家正在审核,请耐心等待") .orderCode(((ServiceOrder)order).getCode()) .serviceAmount(((ServiceOrder)order).getService().getService().getPrice().getNormalPrice()) .serviceDate(((ServiceOrder)order).getServiceDate()) .servicContent(((ServiceOrder)order).getService().getSimpleContent()) .storeName(((ServiceOrder)order).getStore().getName()) .storeAddress(((ServiceOrder)order).getStore().getAddress().getName()) .build();  return orderSuccessResult;  } }

添加简单工厂类(其实这里也可以使用工厂方法模式,但怕过于复杂,故不使用工厂方法模式)

@Component public class ServiceOrderSuccessResultFactory { @Getter  private OrderSuccessResult orderSuccessResult;   @PostConstruct  private void init() { this.orderSuccessResult = createResult();  } private OrderSuccessResult createResult() { Set<Class<?>> classes = ClassUtil.getClassSet("com.cloud.ownercar.domain");  Object instance = null;  try { //过滤有@ServiceResultVersion标签的类  instance = classes.stream().filter(clazz -> clazz.isAnnotationPresent(ServiceResultVersion.class)) //过滤实现了OrderSuccessResult接口的类  .filter(clazz -> OrderSuccessResult.class.isAssignableFrom(clazz)) //找出版本号大的类,并实例化为对象  .max(Comparator.comparingInt(clazz -> clazz.getAnnotation(ServiceResultVersion.class).value())) .get().newInstance();  } catch (InstantiationException e) { e.printStackTrace();  } catch (IllegalAccessException e) { e.printStackTrace();  } return (OrderSuccessResult) instance;  } }

修改ServiceOrder如下

@Data @AllArgsConstructor @NoArgsConstructor @ServiceOrderVersion(value = 1) @RequiredArgsConstructor public class ServiceOrder extends AbstractOrder { private Long id;  @NonNull  private String code;  @NonNull  private Store store;  @NonNull  private ProviderService service;  @NonNull  private Car car;  @NonNull  private Date serviceDate;  @NonNull  private String contact;  @NonNull  private String contactTel;  private AppUser user;  @NonNull  private String content;  private int status;  private Date createDate;   @Override  public void makeOrder(Order order) { ServiceOrderDao serviceOrderDao = SpringBootUtil.getBean(ServiceOrderDao.class);  IdService idService = SpringBootUtil.getBean(IdService.class);  ((ServiceOrder)order).setId(idService.genId());  ((ServiceOrder)order).setCode(getCode(idService));  AppUser loginAppUser = AppUserUtil.getLoginAppUser();  AppUser user = new AppUser();  user.setId(loginAppUser.getId());  user.setUsername(loginAppUser.getUsername());  ((ServiceOrder)order).setUser(user);  ((ServiceOrder)order).setStatus(1);  ((ServiceOrder)order).setCreateDate(new Date());  serviceOrderDao.save((ServiceOrder) order);  } @Override  public OrderSuccessResult getResult(Order order) { ServiceOrderSuccessResultFactory orderSuccessResultFactory = SpringBootUtil.getBean(ServiceOrderSuccessResultFactory.class);  this.orderSuccessResult = orderSuccessResultFactory.getOrderSuccessResult();  return this.orderSuccessResult.getResult(order);  } private String getCode(IdService idService) { String flow = String.valueOf(idService.genId());  flow = flow.substring(14,flow.length());  String pre = DateUtils.format(new Date(), DateUtils.pattern9);  return pre + flow;  } }
原文链接:https://my.oschina.net/u/3768341/blog/3101448
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章