• 缩减项目代码中的大面积if策略


    参考设计模式 - 策略模式我们可以优化if-else代码段,而在Spring(Boot)中,借助ApplicationContext扫描,可以使代码更加干净。

    话不多说,亮代码:

    首先按照策略模式的写法,创建一个Handle接口,用于区分处理策略。

    public interface ITypeHandle {
        /**
         * 不同的业务操作
         *
         * @return 对应的操作结果
         */
        String working();
    
        /**
         * 声明用于处理的业务(方便查看而已)
         *
         * @return 业务名称
         */
        String handleType();
    }
    

    然后创建对应的唤醒处理机和睡觉处理机

    // 起床操作
    public class WakeTypeHandle implements ITypeHandle {
        @Override
        public String working() {
            try {
                // 模拟业务操作
                Thread.sleep(100);
            } catch (Exception ex) {
                return "Wake Interrupt";
            }
            return "Wake up Wake up Wake up";
        }
    
        @Override
        public String handleType() {
            return "wake";
        }
    }
    
    // 睡觉操作
    public class SleepTypeHandle implements ITypeHandle {
        @Override
        public String working() {
            try {
                // 模拟业务操作
                Thread.sleep(500);
            } catch (Exception ex) {
                return "Sleep Interrupt";
            }
            return "Sleep Sleep Sleep Zzzzzzzzzzzzz";
        }
    
        @Override
        public String handleType() {
            return "sleep";
        }
    }
    

    简单来说,策略模式的操作方式是通过上下文切换不同的处理机来处理不同的业务。在Spring中,可以借助ApplicationContext和ComponentScan来完成。

    建立策略工厂

    public class TypeHandleFactory {
        private Map<String , ITypeHandle> map;
    
        @Autowired
        private ApplicationContext applicationContext;
    
        @PostConstruct
        private void init(){
            Map<String ,ITypeHandle> beans=applicationContext.getBeansOfType(ITypeHandle.class);
            map= new HashMap<>(beans.size());
            for (ITypeHandle handle : beans.values()) {
                map.put(handle.handleType(),handle);
            }
        }
        public ITypeHandle getInstance(String type){
            return map.get(type);
        }
    }
    

    这里的初始化应当在ApplicationContext注入完成后进行,因此添加@PostConstruct注解。在Spring中,类执行主要注解的顺序为构造方法=>@Autowired=>@PostConstruct=>@PreDestroy=>销毁。另外需要在策略工厂和两个处理机上添加@Component注解或由@ComponentScan扫描。

    添加一个Service类来模拟业务层:

    @Service
    public class MainServiceImpl {
        @Autowired
        private TypeHandleFactory factory ;
        @Override
        public String work(String type){
            ITypeHandle handler = factory.getInstance(type);
            return handler.working();
        }
    }
    

    至此,我们已经完成了策略模式在Spring中的构建。可以编写测试类测试:

    @SpringBootTest
    class MainServiceImplTest {
        @Autowired
        private MainServiceImpl service;
        @Test
        void work(){
            String typeA="wake";
            String typeB="sleep";
            System.out.println(service.work(typeA));
            System.out.println(service.work(typeB));
        }
    }
    

    日志打印为:

    Wake up Wake up Wake up
    Sleep Sleep Sleep Zzzzzzzzzzzzz
    

    策略模式在Spring中的优化完成。

  • 相关阅读:
    问世即屠榜的bert
    写给日后面试的小朋友们~
    SQL笔记续补
    《姜子牙》视频笔记
    知识图谱之小米的落地与应用探索
    Pyspark ml
    一个小时学会用 Go 编写命令行工具
    C#设计模式-组合模式(Composite Pattern)
    C#设计模式-桥接模式(Bridge Pattern)
    C#设计模式-装饰器模式(Decorator Pattern)
  • 原文地址:https://www.cnblogs.com/ZoraZora59/p/12422967.html
Copyright © 2020-2023  润新知