策略模式: 1、如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态地让一个对象在许多行为中选择一种行为。 2、一个系统需要动态地在几种算法中选择一种。 3、如果一个对象有很多的行为,如果不用恰当的模式,这些行为就只好使用多重的条件选择语句来实现。
工厂模式:定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行
先讲讲项目需求,在项目中负责连接器模块,在这里需要实现接口的重试功能,但是每个接口传递的参数不同,因此具体的接口请求类也不同,故打算采取策略模式来完成相应的功能开发。
首先,我们可以定义一个接口并添加其基础的两个功能方法,在这里需要实现的是接口重试功能,name()主要是为了从工厂中获取指定的Bean实例,dotryCall()是为了执行我们的接口重试功能,下面看看具体的实现逻辑。
定义接口
public interface ChannelClient<T> {
String name();
void execute(Long id, T requestParam, String url);
}
定义工厂 (***我们需要在项目初始化时加载bean,所以这里需要将方法和工厂Map都定义成static类型)
public class ChannelClientFactory {
private static final Map<String, ChannelClient> CHANNEL_CLIENT_MAP = new HashMap<>();
public static void register (ChannelClient channelClient) {
CHANNEL_CLIENT_MAP.put( channelClient.name(),channelClient);
}
public static ChannelClient getChannelClient(String channelName) {
return Optional.ofNullable(CHANNEL_CLIENT_MAP.get(channelName))
.orElseThrow(() -> new IllegalArgumentException("不支持的渠道类型: " + channelName));
}
}
在对应的功能类上使用init方法(@PostConstruct:实现在项目初始化时自动执行该方法),使用ChannelFactory.register(this)实现bean注册,exceute为具体实现操作的方法。
public class WlyPromotionPlatformOrderChannelClient extends DefaultOrderChannelClient implements ChannelClient<T> {
private static final Logger LOGGER = LoggerFactory.getLogger(WlyPromotionPlatformOrderChannelClient.class);
private final PromotionPlatformClient promotionPlatformClient;
@PostConstruct
public void init() {
ChannelClientFactory.register(this);
}
@Autowired
private ICallOperationLogService iCallOperationLogService;
@Override
public String name() {
return "wlyPromotionPlatformOrderChannelClient";
}
public WlyPromotionPlatformOrderChannelClient(PromotionPlatformClient promotionPlatformClient) {
this.promotionPlatformClient = promotionPlatformClient;
}
@Override
public void execute(Long id, T requestParam, String url) {
CommonRequest<Object> request = new CommonRequest<>(requestParam, url);
CommonChannelBaseRespDto result = new CommonChannelBaseRespDto();
try {
result = promotionPlatformClient.sentRequest(request, new TypeReference<CommonChannelBaseRespDto>() {
});
} catch (Exception e) {
result.setResult("F");
result.setMessage(e.getMessage());
throw new BizException(result.getResult(), result.getMessage()); }
finally {
iCallOperationLogService.updateOperationLog(id, result.getMessage(), result.getResult());
}
}
}
在业务层便可通过ChannelFactory的getChannelClient获取指定的bean并执行exceute方法。
@Override
public void doTryCall(CallOperationLogReqDto callOperationLogReqDto) {
QueryWrapper<CallOperationLogEo> queryWrapper = new QueryWrapper();
queryWrapper.eq("id", callOperationLogReqDto.getId().toString());
CallOperationLogEo callOperationLogEo = callOperationLogEoMapper.selectOne(queryWrapper);
LOGGER.info("查询出的操作信息为:{}", JSONObject.toJSONString(callOperationLogEo));
ChannelClientFactory.getChannelClient(callOperationLogEo.getChannelSource()).
execute(callOperationLogEo.getId(), JSONObject.parse(callOperationLogEo.getBizData()), callOperationLogEo.getBizSource());
}
这样,我们就实现了策略模式在项目中的具体使用