UML:
class:
public interface IDeliveryService {/*** 创建第三方出货单* @param obj 入参*/Object createThirdOrder(Object obj);
}
@Service
@RequiredArgsConstructor
public class DeliveryServiceImpl implements IDeliveryService {private final DeliveryStrategyFactory deliveryStrategyFactory;@Overridepublic Object createThirdOrder(Object obj){// 根据type确定出库策略IDeliveryStrategy deliveryStrategy = deliveryStrategyFactory.getDeliveryStrategy(DeliveryPlatformEnum.MO_FANG);// 创建return deliveryStrategy.create((MoFangCreateOrderDTO )deliveryOrderDTO);}
}
@Component
public class DeliveryStrategyFactory implements ApplicationContextAware {private Map<DeliveryPlatformEnum,IDeliveryStrategy> deliveryStrategyMap = new HashMap<>();@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {Map<String, IDeliveryStrategy> beansOfType = applicationContext.getBeansOfType(IDeliveryStrategy.class);beansOfType.values().forEach(e-> deliveryStrategyMap.put(e.getDeliveryPlatformEnum(),e));}public IDeliveryStrategy getDeliveryStrategy(DeliveryPlatformEnum deliveryPlatformEnum){return deliveryStrategyMap.get(deliveryPlatformEnum);}
}
@Getter
public enum DeliveryPlatformEnum {/*** 魔方平台*/MO_FANG(1,"魔方");private final Integer code;private final String description;DeliveryPlatformEnum(Integer code,String description) {this.code = code;this.description = description;}
}
public interface IDeliveryStrategy {/*** 策略所属平台枚举* @return 。*/DeliveryPlatformEnum getDeliveryPlatformEnum();/*** 创建出货单* @param* @return*/Object create(Object obj);
}
@Service
@RequiredArgsConstructor
public class MoFangDeliveryStrategy implements IDeliveryStrategy {private final MoFangClient moFangClient;@Overridepublic DeliveryPlatformEnum getDeliveryPlatformEnum() {return DeliveryPlatformEnum.MO_FANG;}@Overridepublic Object create(Object obj){return JsonUtils.toObject(moFangClient.createOrder((MoFangCreateOrderDTO) obj), MoFangCreateOrderResponse.class);}}