1、策略接口
java
/**
* 策略接口
*/
public interface DemoStrategy {
Result execute();
}
2、策略工厂
java
/**
* 策略工厂
*/
@Component
public class DemoFactory {
@Resource
private final Map<String, DemoStrategy> demoStrategy= new ConcurrentHashMap<>();
public DemoStrategy doStrategy(String data){
return this.demoStrategy.get(data);
}
}
3、策略实现类
java
/**
* 策略实现类
*/
@Component("dataName")
public class DataNameImpl implements DemoStrategy {
@Override
public Result execute() {
//do something
return null;
}
}
4、调用Demo
java
// class
@Resource
private DemoFactory demoFactory ;
//method
String dataName= "dataName";
return demoFactory.doStrategy(dataName).execute();