在用工廠方法模式來下不一樣訂單 中咱們看到,咱們只簡單顯示來一個「下單成功」,但實際上咱們須要給用戶返回到結果可能多種多樣。app
先增長一個訂單結果到接口。dom
public interface OrderSuccessResult { public OrderSuccessResult getResult(Order order); }
添加一個服務訂單結果實現類ide
@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接口添加一個適配方法ui
public interface Order { public void makeOrder(Order order); public OrderSuccessResult getResult(Order order); }
增長一個訂單抽象類,完成對訂單結果接口的橋接this
public abstract class AbstractOrder implements Order { protected OrderSuccessResult orderSuccessResult; }
修改各個Order的實現類,繼承於該抽象類spa
@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進行了耦合,此處須要修改。.net
Controller修改以下code
@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; } }
如今運行是沒有問題的。返回結果以下orm
@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; } }