一.策略上下文
@Component("classifyStrategyContext")
public class ClassifyStrategyContext{
@Autowired
private Map<String, AbstractDeviceStrategy> strategyMap;
public AbstractDeviceStrategy getClassifyStrategy(String modelLabel) {
String strategyBeanName = "device_"+ modelLabel;
return strategyMap.get(strategyBeanName);
}
}
二. 策略抽象类
@Data
public abstract class AbstractDeviceStrategy {
@Autowired
protected ITestService testService;
/**
* 抽象类
* @param deviceInDcBase--公共Bean
* @return
*/
public abstract BaseTreeNodeBO classify(DevicesInPlaceBO deviceInDcBase);
/**
* 策略公共方法
* @param modelLabel
* @param ids
* @return
*/
public List<BaseTreeNodeBO> handleMeterTypes(String modelLabel, Collection<Long> ids) {
//todo
return null;
}
}
三. 策略实现类
@Component("device_ats")
public class AtsDeviceStrategy extends AbstractDeviceStrategy {
@Override
public BaseTreeNodeBO classify(DevicesInPlaceBO deviceInDcBase) {
//do something .......
//调用公共方法
List<BaseTreeNodeBO> baseTreeNodes = handleMeterTypes("ats", ids);
//do something .......
}
}
四.Demo用例
AbstractDeviceStrategy classifyStrategy = classifyStrategContext.getClassifyStrategy("ats");
if (null != classifyStrategy) {
BaseTreeNodeBO baseTreeNodeBO = classifyStrategy.classify(devicesInPlaceBO);
//do something
}