接口定义:
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); } }