private static final StrategyFactory instance = new StrategyFactory();
private StrategyFactory() {
}
public static StrategyFactory getInstance(){
return instance;
}
private static Map<Integer,Strategy> map = new HashMap<>();
//通过类型获取策略
public Strategy getStrategy(Integer type){
if (type == null || !map.containsKey(type)){
throw new RuntimeException(“策略不存在!”);
}
return map.get(type);
}
//静态封装策略到map
static {
map.put(1,new StrategyA());
map.put(2,new StrategyB());
}
}
调用端测试
public class Test {
public static void main(String[] args) {
Strategy strategy = StrategyFactory.getInstance().getStrategy(2);
strategy.algorithm();
}
}
运行结果
采用策略B计算
可以将类型定义成枚举
public enum AlgorithmType {
/**
- A算法
*/
A(1,“A算法”),
/**
- B算法
*/
B(2,“B算法”);
AlgorithmType(int type, String value) {
this.type = type;
this.value = value;
}
private int type;
private String value;
public int getType() {
return type;
}
public String getValue() {
return value;
}
}
6.优化:将策略上下文换成策略选择器
策略接口新增一个算法类型接口
public interface Strategy {
void algorithm();
Integer getAlgorithmType();
}
同时实现类也新增对应的类型方法
@Component
public class StrategyA implements Strategy {
@Override
public void algorithm() {
System.out.println(“采用策略A计算”);
}
@Override
public Integer getAlgorithmType() {
return AlgorithmType.A.getType();
}
}
@Component
public class StrategyB implements Strategy {
@Override
public void algorithm() {
System.out.println(“采用策略B计算”);
}
@Override
public Integer getAlgorithmType() {
return AlgorithmType.B.getType();
}
}
策略选择器定义
@Component
public class StrategySelector {
private static Map<Integer,Strategy> map = new HashMap<>();
//(required=true),表示注入的时候,该bean必须存在,否则就会注入失败。
@Autowired(required = false)
private void init(List strategies){
if (CollectionUtils.isEmpty(strategies)) {
return;
}
//将策略接口注入进map
map = strategies.stream().collect(Collectors.toMap(Strategy::getAlgorithmType, s -> s));
System.out.println(“StrategyMap:”+JSON.toJSONString(map));
}
public void method(Integer type){
map.get(type).algorithm();
}
}
启动服务器控制台输出StrategyMap:{1:{“algorithmType”:1},2:{“algorithmType”:2}},说明策略接口已全部封装在map
调用端
public class Test {
@Resource
private StrategySelector strategySelector;
public void test() {
strategySelector.method(AlgorithmType.A.getType());
}
}
7.优化:将策略上下文换策略选择器,采用注解形式注入
定义算法类型注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Algorithm {
int value();
}
在策略实现接口上加入注解
@Algorithm(1)
@Component
public class StrategyA implements Strategy {
@Override
public void algorithm() {
System.out.println(“采用策略A计算”);
}
}
@Algorithm(2)
@Component
public class StrategyB implements Strategy {
@Override
public void algorithm() {
System.out.println(“采用策略B计算”);
最后
光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的增加文章的篇幅,减少文章的可读性
Java面试宝典2021版
最常见Java面试题解析(2021最新版)
2021企业Java面试题精选
算");
最后
光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的增加文章的篇幅,减少文章的可读性
Java面试宝典2021版
[外链图片转存中…(img-VBJyV9EO-1720130215384)]
[外链图片转存中…(img-2dCxhvBV-1720130215385)]
最常见Java面试题解析(2021最新版)
[外链图片转存中…(img-Wmkb2isR-1720130215385)]
[外链图片转存中…(img-fUDxzEhv-1720130215386)]
2021企业Java面试题精选
[外链图片转存中…(img-BoXf0KXb-1720130215386)]
[外链图片转存中…(img-Zo5b70bJ-1720130215387)]