• if else 替换方案


    替换前

     public void toName(String code) {
            if ("pig".equals(code)) {
                pig.name();
            } else if ("dog".equals(code)) {
                dog.name();
            } else if ("cat".equals(code)) {
                cat.name();
            } else {
                // default
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    注解方式

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    @Documented
    public @interface AnimalCode {
    
        String value();
    
        String name();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    @Service
    @AnimalCode(value = "cat", name = "猫")
    public class CatImpl1 implements IAnimal {
    
        @Override
        public void name() {
            System.out.println("Cat");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    @Service
    @AnimalCode(value = "dog", name = "狗")
    public class DogImpl1 implements IAnimal {
    
        @Override
        public void name() {
            System.out.println("Dog");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    @Service
    public class AnimalService1 implements ApplicationContextAware {
    
        private static Map<String, IAnimal> animalMap = new HashMap<>();
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(AnimalCode.class);
            beansWithAnnotation.forEach((k, v) -> animalMap.put(v.getClass().getAnnotation(AnimalCode.class).value(), (IAnimal) v));
        }
    
        public void toName(String code) {
            animalMap.get(code).name();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    使用

        @Resource
        private AnimalService1 animalService1;
    
        @Test
        public void toName(){
            animalService1.toName("cat");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    循环方式

    public interface IAnimal {
    
        void name();
    
        boolean isMatch(String code);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    @Service
    public class CatImpl2 implements IAnimal {
    
        @Override
        public void name() {
            System.out.println("Cat");
        }
    
        @Override
        public boolean isMatch(String code) {
            return "cat".equals(code);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    @Service
    public class DogImpl2 implements IAnimal {
    
        @Override
        public void name() {
            System.out.println("Dog");
        }
    
        @Override
        public boolean isMatch(String code) {
            return "dog".equals(code);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    @Service
    public class AnimalService2 {
    
        @Resource
        private List<IAnimal> animals;
    
        public void toName(String code) {
            for (IAnimal animal: animals) {
                if (animal.isMatch(code)) {
                    animal.name();
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    使用

       @Resource
        private AnimalService2 animalService2;
    
        @Test
        public void toName(){
            animalService2.toName("pig");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    策略+工厂

    @Service
    public class DogImpl3 implements IAnimal {
    
        @PostConstruct
        public void init() {
            StrategyFactory.register("dog", this);
        }
    
        @Override
        public void name() {
            System.out.println("Dog");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    @Service
    public class PigImpl3 implements IAnimal {
    
        @PostConstruct
        public void init() {
            StrategyFactory.register("pig", this);
        }
    
        @Override
        public void name() {
            System.out.println("Pig");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    public class StrategyFactory {
    
        private static Map<String, IAnimal> map = new HashMap<>();
    
        public static void register(String code, IAnimal animal){
            map.put(code, animal);
        }
    
        public static IAnimal get(String code){
            return map.get(code);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    @Service
    public class AnimalService3 {
    
        public void toName(String code) {
            StrategyFactory.get(code).name();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    使用

        @Resource
        private AnimalService3 animalService3;
    
        @Test
        public void toName(){
            animalService3.toName("cat");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    责任链

    public abstract class AnimalHandler {
    
        @Getter
        @Setter
        protected AnimalHandler next;
    
        public abstract void name(String code);
    
        public abstract String code();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    @Service
    public class CatHandlerImpl extends AnimalHandler {
    
        @Override
        public void name(String code) {
            if (code().equals(code)) {
                System.out.println("Cat");
            } else {
                next.name(code);
            }
        }
    
        @Override
        public String code() {
            return "cat";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    @Service
    public class DogHandlerImpl extends AnimalHandler {
    
        @Override
        public void name(String code) {
            if (code().equals(code)) {
                System.out.println("Dog");
            } else {
                next.name(code);
            }
        }
    
        @Override
        public String code() {
            return "dog";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    @Service
    public class AnimalHandlerChain implements ApplicationContextAware, InitializingBean {
    
        private ApplicationContext applicationContext;
    
        private AnimalHandler header;
    
        @Override
        public void afterPropertiesSet() throws Exception {
            Map<String, AnimalHandler> beansOfType = applicationContext.getBeansOfType(AnimalHandler.class);
            List<AnimalHandler> handlers = new ArrayList<>(beansOfType.values());
            for (int i = 0; i < handlers.size(); i++) {
                AnimalHandler animalHandler = handlers.get(i);
                if (i != handlers.size() - 1) {
                    animalHandler.setNext(handlers.get(i + 1));
                }
            }
            header = handlers.get(0);
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    
        public void toName(String code) {
            header.name(code);
        }
    }
    
    • 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

    使用

        @Resource
        private AnimalHandlerChain animalHandlerChain;
    
        @Test
        public void toName(){
            animalHandlerChain.toName("pig");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    枚举

    @AllArgsConstructor
    public enum AnimalEnum {
    
        /**
         *
         */
        DOG(1, "dog") {
            @Override
            public void names() {
                System.out.println("Dog");
            }
        }, PIG(2, "pig") {
            @Override
            public void names() {
                System.out.println("Pig");
            }
        }, CAT(3, "cat") {
            @Override
            public void names() {
                System.out.println("Cat");
            }
        };
    
        private int index;
        private String code;
    
        public abstract void names();
    
        public static void toName(String code) {
            Arrays.stream(AnimalEnum.values()).filter(x -> x.code.equals(code)).findFirst().orElseThrow().names();
        }
    }
    
    • 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

    使用

      @Test
        public void toName(){
            AnimalEnum.toName("pig");
        }
    
    • 1
    • 2
    • 3
    • 4

    源码

    https://github.com/googalAmbition/googol/tree/master/ifelse

  • 相关阅读:
    SQL Server - 提高服务器安全性13招
    三维视频融合技术如何为智慧城市建设赋能
    DevOps和SRE还没搞清楚,平台工程又出现了,它会取代DevOps吗?
    NFT 智能合约实战-快速开始(1)NFT发展历史 | NFT合约标准(ERC-721、ERC-1155和ERC-998)介绍
    环境变量与Path环境变量
    【快应用】H5快应用Web组件打开的网页出现跨域问题如何解决?
    JavaScript 基础知识| 数据类型|类型转换
    谛听安全 | 内容审核闯关攻略指南
    李宏毅-机器学习-笔记-P1
    Vue理解01
  • 原文地址:https://blog.csdn.net/qq_23934475/article/details/127870409