• 工厂+策略设计模式


    有时后经常需要写很多的if判断语句,导致了代码的十分冗余,可读性不高,下面以工厂设计模式+策略设计模式提供一种可替代的写法,简化代码

    工厂类:Factory

    /**
     * 工厂设计模式
     */
    public class Factory {
        private static Map<String, Handler> strategyMap = Maps.newHashMap();
    
        public static Handler getInvokeStrategy(String name) {
            return strategyMap.get(name);
        }
    
        public static void register(String name, Handler handler) {
            if (StringUtils.isEmpty(name) || null == handler) {
                return;
            }
            strategyMap.put(name, handler);
        }
    }

     策略接口:

    import org.springframework.beans.factory.InitializingBean;
    
    /**
     * 策略设计模式
     */
    public interface Handler extends InitializingBean {
    
        public void AAA(String name);
    
    }
    具体实现的一些策略 KangBaHandler
    @Component
    public class KangBaHandler implements Handler {
    
        @Override
        public void AAA(String name) {
            // 业务逻辑A
            System.out.println("亢八完成任务");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            Factory.register("亢八", this);
        }
    }
    具体实现的一些策略 LiSiHandler
    import org.springframework.stereotype.Component;
    
    @Component
    public class LiSiHandler implements Handler {
    
        @Override
        public void AAA(String name) {
            // 业务逻辑A
            System.out.println("李四完成任务");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            Factory.register("李四", this);
        }
    }
    具体实现的一些策略 TianQiHandler
    import org.springframework.stereotype.Component;
    
    @Component
    public class TianQiHandler implements Handler {
    
        @Override
        public void AAA(String name) {
            // 业务逻辑A
            System.out.println("田七完成任务");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            Factory.register("田七", this);
        }
    }
    具体实现的一些策略 WangWuHandler
    import org.springframework.stereotype.Component;
    
    @Component
    public class WangWuHandler implements Handler {
    
        @Override
        public void AAA(String name) {
            // 业务逻辑A
            System.out.println("王五完成任务");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            Factory.register("王五", this);
        }
    }
    具体实现的一些策略 ZhangSanHandler
    import org.springframework.stereotype.Component;
    
    @Component
    public class ZhangSanHandler implements Handler {
    
        @Override
        public void AAA(String name) {
            // 业务逻辑A
            System.out.println("张三完成任务");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            Factory.register("张三", this);
        }
    }


























  • 相关阅读:
    Scala In 5 Years – My Prediction « GridGain – Real Time Big Data
    牛魔王珍满福拉面 北京团购网|京东团购
    Build WebKit On Windows 白果果白的专栏 博客频道 CSDN.NET
    fabulous
    Selenimu做爬虫 oscarxie 博客园
    EA gpl
    数据挖掘与Taco Bell编程
    test cpp could not compiled on ubuntu use g++,i'll tried lateor on win platform
    How to use ActiveRecord without Rails
    svn添加新项目管理并添加到trac 知识是一种越吃越饿的粮食 ITeye技术网站
  • 原文地址:https://www.cnblogs.com/cb1186512739/p/14264616.html
Copyright © 2020-2023  润新知