一、接口类
案例:
登录后复制
/**
* 接口策略
*/
public interface DataServeStrategy {
Map<String,Object> getData(Api api, String testNo, HttpServletRequest request, HttpServletResponse response);
}1.2.3.4.5.6.
二、接口实现类
案例1
登录后复制
@Component("ORCHESTRATION")
public class GetOrchestrationData implements DataServeStrategy {
@Override
public Map<String, Object> getData(Api api, String testNo, HttpServletRequest request, HttpServletResponse response) {
Map<String,Object> result = new HashMap<>(Collections.emptyMap());
//业务逻辑
return result;
}
}1.2.3.4.5.6.7.8.9.
案例2
登录后复制
@Component("VIDEO")
public class GetVideoData implements DataServeStrategy {
@Resource
private VideoDataService videoDataService;
@Override
public Map<String, Object> getData(Api api, String testNo, HttpServletRequest request, HttpServletResponse response) {
Map<String,Object> result = new HashMap<>(Collections.emptyMap());
//处理过程
return result;
}
}1.2.3.4.5.6.7.8.9.10.11.12.
三、上下文控制(重点)
登录后复制
@Service
public class DataServeContext {
private final Map<String, DataServeStrategy> orderStrategyMap = new ConcurrentHashMap<>();
public DataServeContext(Map<String, DataServeStrategy> strategyMap) {
this.orderStrategyMap.putAll(strategyMap);
}
public DataServeStrategy getResource(String type){
return orderStrategyMap.get(type);
}
}1.2.3.4.5.6.7.8.9.10.11.12.13.
四、业务调用
登录后复制
/**
* api.getServiceType() 对应@Component("ORCHESTRATION") 中的ORCHESTRATION
* getData 为对应的实际方法
* stringObjectMap 返回结果
*/
DataServeStrategy dataServeStrategy = dataServeContext.getResource(api.getServiceType());
Map<String, Object> stringObjectMap = dataServeStrategy.getData(api, testNo, request, response);1.2.3.4.5.6.7.
未经允许不得转载:AiShang - 爱尚IT分享博客 » 设计模式:策略模式java实践