java设计模式之策略模式+工厂模式(优化if-else)
java设计模式之策略模式+工厂模式
·
案例:假如以快餐类为需求。模拟现实业务
方法一:简单工厂+策略模式(采用afterPropertiesSet方法)
//从这里可以看出使用了大量il/else,如果是真实业务
//可能每个if/else中又有大量的业务逻辑这样就会显得代码很臃肿
String type="";
if (type.equals("炒饭")) {
System.out.println("10块钱");
} else if (type.equals("炒米粉")) {
System.out.println("12块钱");
} else if (type.equals("炒火腿")) {
System.out.println("14块钱");
} else if (type.equals("炒粉")) {
System.out.println("16块钱");
}
}
/**
* @Author cgz
* @Date 2022/11/18 9:30
*/
//注意需要继承这个InitializingBean或者实现类实现这个InitializingBean才会自动注入Bean
//策略模式定义策略
public interface Strategy extends InitializingBean{
void getDecs();
}
//这里要让这个bean被扫描到才会自动注册到工厂里
//如果需要afterPropertiesSet这个方法启动时久把该类存到内存中还需要加注解,不然spring默认是实例化该方法才会调用到
@Service
@Lazy(value=false)
public class StrategyA implements Strategy{
@Override
public String price(String type) {
return "10";
}
@Override
public void afterPropertiesSet() throws Exception {
StrategyFactory.register("炒饭",this);
}
}
@Service
@Lazy(value=false)
public class StrategyB implements Strategy{
@Override
public String price(String type) {
return "12";
}
@Override
public void afterPropertiesSet() throws Exception {
StrategyFactory.register("炒米粉",this);
}
}
@Service
@Lazy(value=false)
public class StrategyC implements Strategy{
@Override
public String price(String type) {
return "14";
}
@Override
public void afterPropertiesSet() throws Exception {
StrategyFactory.register("炒火腿",this);
}
}
@Service
@Lazy(value=false)
public class StrategyD implements Strategy{
@Override
public String price(String type) {
return "16";
}
@Override
public void afterPropertiesSet() throws Exception {
StrategyFactory.register("炒粉",this);
}
}
策略工厂
@Component
public class StrategyFactory {
@Autowired
private static Map<String, Strategy> strategyMap = new HashMap<>();
public static Strategy getInvokeStrategy(String username) {
return strategyMap.get(username);
}
public static void register(String name, Strategy handler) {
if (ObjectUtils.isEmpty(name) && handler == null) {
return;
}
strategyMap.put(name, handler);
}
}
方法二:
* @Author cgz
* @Date 2022/11/18 9:30
*/
public interface Strategy {
String price();
}
@Service
public class StrategyA implements Strategy{
@Override
public String price(String type) {
return "10";
}
}
@Service
public class StrategyB implements Strategy{
@Override
public String price(String type) {
return "12";
}
}
@Service
public class StrategyC implements Strategy{
@Override
public String price(String type) {
return "14";
}
}
@Service
public class StrategyD implements Strategy{
@Override
public String price(String type) {
return "16";
}
}
策略工厂
@Component
public class StrategyFactory {
@Autowired
private static Map<String, Strategy> strategyMap = new HashMap<>();
public static Strategy getInvokeStrategy(String username) {
return strategyMap.get(username);
}
//调用工厂得时候放入map
static {
strategyMap.put("炒饭", new StrategyA());
strategyMap.put("炒米粉",new StrategyB());
strategyMap.put("炒火腿",new StrategyC());
strategyMap.put("炒粉",new StrategyD());
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)