案例:假如以快餐类为需求。模拟现实业务
方法一:简单工厂+策略模式(采用afterPropertiesSet方法)

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14
            //从这里可以看出使用了大量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块钱");
      }
   }

/**

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
 * @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);
   }


}

策略工厂

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
@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);
   }

}

方法二:

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 * @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";
   }




}

策略工厂

text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
@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