策略模式定义了一组算法,将每个算法都封装起来,并且使它们之间可以互换
主要角色如下:
//抽象策略角色
public interface Strategy {
//策略模式的运算法则
void doSomething();
}
//具体策略角色
public class ConcreteStrategy1 implements Strategy {
@Override
public void doSomething() {
System.out.println("具体策略1的运算法则...");
}
}
//具体策略角色
public class ConcreteStrategy2 implements Strategy {
@Override
public void doSomething() {
System.out.println("具体策略2的运算法则...");
}
}
public class Client {
public static void main(String[] args) {
//声明一个具体的策略
Strategy strategy = new ConcreteStrategy1();
//声明上下文对象
Context context = new Context(strategy);
//执行封装后的方法
context.doAnything();
}
}
执行结果如下:
具体策略1的运算法则...
public interface Strategy {
//策略模式的运算法则
void doSomething();
}
@Component
public class ConcreteStrategy1 implements Strategy {
@Override
public void doSomething() {
System.out.println("具体策略1的运算法则...");
}
@Override
public String toString() {
return "具体策略1";
}
}
@Component
public class ConcreteStrategy2 implements Strategy {
@Override
public void doSomething() {
System.out.println("具体策略2的运算法则...");
}
@Override
public String toString() {
return "具体策略2";
}
}
@Component
public class DefaultStrategy implements Strategy {
@Override
public void doSomething() {
System.out.println("默认策略的运算法则...");
}
@Override
public String toString() {
return "默认策略";
}
}
@Component
public class StrategyFactory {
//Spring会自动将Strategy接口的实现类注入到这个Map中,key为bean id,value值则为对应的策略实现类
@Autowired
private Map<String, Strategy> strategyMap;
public Strategy getBy(String strategyName) {
return strategyMap.get(strategyName);
}
}
Spring会自动将Strategy接口的实现类注入到这个Map中(前提是实现类得是交给Spring 容器管理的),这个Map的key为bean id,可以用@Component(value = "xxx")
的方式设置,如果直接用默认的方式的话,就是首字母小写。value值则为对应的策略实现类
测试类:
@SpringBootTest
class SpringbootDemoApplicationTests {
@Autowired
private ApplicationContext context;
@Test
public void test() {
context.getBean(StrategyFactory.class).getBy("concreteStrategy1").doSomething();
context.getBean(StrategyFactory.class).getBy("concreteStrategy2").doSomething();
}
}
执行结果如下:
具体策略1的运算法则...
具体策略2的运算法则...
上面测试类调用的使用使用的bean id,实际业务中应该是将传入的code转义成对应的策略类的bean id
@Component
@PropertySource("classpath:application.properties")
@ConfigurationProperties(prefix = "strategy")
public class StrategyAliasConfig {
private HashMap<String, String> aliasMap;
public static final String DEFAULT_STATEGY_NAME = "defaultStrategy";
public HashMap<String, String> getAliasMap() {
return aliasMap;
}
public void setAliasMap(HashMap<String, String> aliasMap) {
this.aliasMap = aliasMap;
}
public String of(String entNum) {
return aliasMap.get(entNum);
}
}
配置文件application.properties
strategy.aliasMap.strategy1=concreteStrategy1
strategy.aliasMap.strategy2=concreteStrategy2
@Component
public class StrategyFactory {
@Autowired
private StrategyAliasConfig strategyAliasConfig;
//Spring会自动将Strategy接口的实现类注入到这个Map中,key为bean id,value值则为对应的策略实现类
@Autowired
private Map<String, Strategy> strategyMap;
//找不到对应的策略类,使用默认的
public Strategy getBy(String strategyName) {
String name = strategyAliasConfig.of(strategyName);
if (name == null) {
return strategyMap.get(StrategyAliasConfig.DEFAULT_STATEGY_NAME);
}
Strategy strategy = strategyMap.get(name);
if (strategy == null) {
return strategyMap.get(StrategyAliasConfig.DEFAULT_STATEGY_NAME);
}
return strategy;
}
}
测试类:
@SpringBootTest
class SpringbootDemoApplicationTests {
@Autowired
private ApplicationContext context;
@Test
public void test() {
context.getBean(StrategyFactory.class).getBy("strategy1").doSomething();
context.getBean(StrategyFactory.class).getBy("strategy2").doSomething();
}
}
执行结果如下:
具体策略1的运算法则...
具体策略2的运算法则...
网站声明:如果转载,请联系本站管理员。否则一切后果自行承担。
添加我为好友,拉您入交流群!
请使用微信扫一扫!