接口定义:
public interface ProcessParserStrategy {
List<ProcessInfo> parser(String osType, String processInfo);
String getApp();
}
public interface ConfigParserStrategy {
List<ConfigInfo> parser(String configInfo);
String getConfigType();
}
接口实现:
@Service
public class xxxProcessParser implements ProcessParserStrategy {
@Override
public List<ProcessInfo> parser(String osType, String processInfo) {
return null;
}
@Override
public String getApp() {
return "xxx";
}
}
@Service
public class xxxDispatcherConfigParser implements ConfigParserStrategy {
@Override
public List<ConfigInfo> parser(String configInfo) {
return null;
}
@Override
public String getConfigType() {
return "xxx_dispatcher";
}
}
service定义:
@Service
public class ProcessParserService {
private Map<String, ProcessParserStrategy> processParserStrategyMap;
@Autowired
public ProcessParserService(List<ProcessParserStrategy> processParserStrategyList) {
processParserStrategyMap = processParserStrategyList.stream()
.collect(Collectors.toMap(ProcessParserStrategy::getApp, Function.identity()));
}
public List<ProcessInfo> parseProcess(String app, String osType, String processInfo) {
final ProcessParserStrategy processParserStrategy = processParserStrategyMap.get(app);
final List<ProcessInfo> processInfos = processParserStrategy.parser(osType, processInfo);
return processInfos;
}
}
@Service
public class ConfigParserService {
private Map<String, ConfigParserStrategy> configParserStrategyMap;
@Autowired
public ConfigParserService(List<ConfigParserStrategy> configParserStrategieList) {
configParserStrategyMap = configParserStrategieList.stream()
.collect(Collectors.toMap(ConfigParserStrategy::getConfigType, Function.identity()));
}
public List<ConfigInfo> parseProcess(String app, String configFileType, String configInfo) {
String configType = app + "_" + configFileType;
final ConfigParserStrategy configParserStrategy = configParserStrategyMap.get(configType.toLowerCase());
final List<ConfigInfo> configInfos = configParserStrategy.parser(configInfo);
return configInfos;
}
}
client 调用:
@RestController public class PaymentController { @Autowired private xxxService xxxService; @GetMapping("/xxx") public void xxx(@RequestParam String type, @RequestParam int amount) { xxxService.xxx(type, amount); } }
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » 策略模式编程
发表评论 取消回复