用工厂方法模式来下不同订单

原创
2019/09/02 19:29
阅读数 1K

现在假设我们有两种类型的订单,汽车服务订单和商城配件订单

我们的抽象订单接口为

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

抽象订单工厂接口为

public interface OrderFactory {
    public Order createOrder();
    public Order getOrder();
}

现有具体的汽车服务类型订单

@Data
@AllArgsConstructor
@NoArgsConstructor
@ServiceOrderVersion(value = 1)
@RequiredArgsConstructor
public class ServiceOrder implements Order {
    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());
        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);
    }
}

@ServiceOrderVersion版本号如下

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ServiceOrderVersion {
    int value();
}

商城配件订单略

具体汽车服务工厂,我们将其注入Spring容器,不需要每次下单的时候都去扫描包,过滤规则

@Component
public class ServiceOrderFactory implements OrderFactory {
    private Set<Class<?>> classes = ClassUtil.getClassSet("com.cloud.ownercar.domain");
    private Order createdOrder;

    @PostConstruct
    private void initOrder() {
        this.createdOrder = createOrder();
    }

    @Override
    public Order createOrder() {
        Object instance = null;
        try {
            //过滤有@OrderVersion标签的类
            instance = classes.stream().filter(clazz -> clazz.isAnnotationPresent(ServiceOrderVersion.class))
                    //过滤实现了Order接口的类
                    .filter(clazz -> Order.class.isAssignableFrom(clazz))
                    //找出版本号大的类,并实例化为对象
                    .max(Comparator.comparingInt(clazz -> clazz.getAnnotation(ServiceOrderVersion.class).value()))
                    .get().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return (Order) instance;
    }

    @Override
    public Order getOrder() {
        return createdOrder;
    }
}

具体配件工厂略

另外为了满足开闭原则,我们需要加载所有类型的订单和订单工厂,以后在新增订单类型的时候,只需要在指定包下追加新的类型就好。

@Component
public class OrderBean {
    @Getter
    private Map<String,Class<?>> orderFactoryMap = new HashMap<>();
    @Getter
    private Map<String,Class<?>> orderMap = new HashMap<>();

    @PostConstruct
    private void init() {
        Set<Class<?>> classes = ClassUtil.getClassSet("com.cloud.ownercar.domain");
        classes.stream().filter(clazz -> OrderFactory.class.isAssignableFrom(clazz))
                .forEach(clazz -> orderFactoryMap.put(clazz.getSimpleName(),clazz));
        classes.stream().filter(clazz -> Order.class.isAssignableFrom(clazz))
                .forEach(clazz -> orderMap.put(clazz.getSimpleName(),clazz));
    }
}

Controller如下,用传递的内容来判断是哪种类型的订单,并给抽象订单工厂来获取具体的订单工厂,通过具体的订单工厂来生成订单服务,完成下单功能。考虑到线程安全问题,所以要加ThreadLocal进行保护。这个type是在下单的时候需要提供的订单类型,比如"ServiceOrder"或者"ProductOrder"或者以后新增的订单类型。

@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<String> makeOrder(@RequestBody String orderStr, @RequestParam("type") String type) {
        log.info(orderStr);
        try {
            Order order = setOrderFactory(orderStr,type);
            orderService.get().makeOrder(order);
            return Result.success("下单成功");
        }
        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;
    }
}

dao,mapper略

展开阅读全文
加载中

作者的其它热门文章

打赏
1
1 收藏
分享
打赏
0 评论
1 收藏
1
分享
返回顶部
顶部