java设计模式之策略模式+工厂模式(优化if-else)

白色玫瑰 程序猿

时间: 2023-05-22 阅读: 1 字数:4836

{}
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) &amp;&amp; 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());

   }

}

原文地址:https://blog.csdn.net/m0_47944994/article/details/127917732?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168474983616800222844862%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168474983616800222844862&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-14-127917732-null-null.142^v87^control_2,239^v2^insert_chatgpt&utm_term=java%E4%BC%98%E5%8C%96

本文章网址:https://www.sjxi.cn/detil/75cff02ed659420aa76162f95025584f
最新评论
当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号

×

(穷逼博主)在线接单

QQ: 1164453243

邮箱: abcdsjx@126.com

前端项目代做
前后端分离
Python 爬虫脚本
Java 后台开发
各种脚本编写
服务器搭建
个人博客搭建
Web 应用开发
Chrome 插件编写
Bug 修复