Docker命令行入门大全:这18条,你不得不知

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

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

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(www.tyyLeapp.com );
        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(www.liantingzheng.com 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);
        Order order = setOrderFactory(orderStr,type);
        orderService.get().makeOrder(order);
        return Result.success(orderService.get().getResult(order));
    }

    /**
     * 判断是哪一种类型的订单来获取哪一种类型的具体订单工厂
     * @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(www.zheshengyule.com"product")) {
//            order = JSON.parseObject(orderStr, ProductOrder.class);
//        }
        Class<?> classFactoryType = orderBean.getOrderFactoryMap().get(type + "Factory");
        this.orderFactory.set(www.qjljdgt.cn(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 =www.yinmaoyule178.com 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((www.jintianxuesha.com(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(www.shenxianLm.com);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return (OrderSuccessResult) instance;
    }
}
修改ServiceOrder如下

@Data
@AllArgsConstructor
@NoArgsConstructor
@ServiceOrderVersion(value =www.mxdpt.cn 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) www.sanguoyoux.cn {
        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)www.jiuhaozhuce.cn 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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值