• 设计模式:策略模式、工厂模式、模板模式混合使用



    这次我们利用模板模式固定下策略模式的骨架,工厂模式提供注册策略,获取策略的方法,提供一个三个设计模式的例子。

    
    abstract class Template{
            // 模板方法,定义了算法的骨架
        public void templateMethod() {
            System.out.println("执行模板方法的前置操作");
            Product product = createProduct();
            System.out.Printlin(product.toString);
            System.out.println("执行模板方法的后置操作");
        }
    
        // 工厂方法,由子类实现具体的产品创建
        protected abstract Product createProduct();
    }
    
    
    // 具体策略类1/子模板1
    class StrategyImpl1 extends Template {
    
        @Override
        protected Product createProduct() {
            return "产品1";
        }
        
    	@PostConstruct
    	public void registryFactory(){
    		Factory.CHOOSER_MAP.registry("1",this);
    	}
    }
    
    // 具体策略类2/子模板2
    class StrategyImpl2 extends Template {
    
        @Override
        protected Product createProduct() {
            return "产品2";
        }
    
    	@PostConstruct
    	public void registryFactory(){
    		Factory.CHOOSER_MAP.registry("2",this);
    	}
    }
    
    // 工厂接口
    public class Factory {
    
        private final static Map<String, Strategy > CHOOSER_MAP = new ConcurrentHashMap<>();
    
        public static void registry(String code, Strategy strategy ) {
            CHOOSER_MAP.put(code, strategy );
        }
    
        public static Strategy chose(String code) {
    		CHOOSER_MAP.get(code);
        }
    }
    
    // 测试类
    public class Main {
        public static void main(String[] args) {
            StrategyImpl1  StrategyImpl1  = PlatformChooserFactory.chose(1);
            StrategyImpl1.templateMethod();
    
    		StrategyImp2  StrategyImpl2  = PlatformChooserFactory.chose(2);
    		StrategyImpl2.templateMethod();
        }
    }
    
    • 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

    优缺点

    1. 灵活性:策略模式允许在运行时选择不同的策略,工厂模式可以根据需求创建相应的对象,模板模式定义了算法的骨架。这种组合可以使系统更加灵活,能够根据不同的需求选择合适的策略、对象和算法。
    2. 可扩展性:通过工厂模式,可以轻松添加新的具体产品,通过策略模式,可以添加新的策略,通过模板模式,可以添加新的算法实现。这使得系统更容易扩展,可以根据需要动态添加新的产品、策略和算法。
    3. 代码复用:策略模式、工厂模式和模板模式都鼓励代码的重用。策略模式中的策略、工厂模式中的产品和模板模式中的模板方法可以在不同的上下文中被重复使用,避免了重复编写相似的代码。
    4. 松耦合:策略模式、工厂模式和模板模式的结合可以实现松耦合的设计。策略模式通过接口与具体策略解耦,工厂模式通过抽象工厂与具体产品解耦,模板模式通过模板方法与具体算法解耦。这种松耦合设计使得系统更加灵活、可维护和可测试。

    缺点:

    1. 增加复杂性:使用策略模式、工厂模式和模板模式的混合会增加代码的复杂性,需要定义多个接口、类和实现。这可能会增加开发和维护的成本。
    2. 增加类的数量:使用策略模式、工厂模式和模板模式的混合可能导致类的数量增加,特别是在有多个具体策略、产品和算法时。这可能会增加系统的复杂性和内存占用。

    总结

    需要根据具体的应用场景和需求来权衡使用策略模式、工厂模式和模板模式的混合。在某些情况下,这种组合可以提供更灵活、可扩展和可维护的设计,但也需要考虑代码复杂性和类的数量增加的影响。

  • 相关阅读:
    【node】初识node以及fs操作,path操作以及http操作(一)
    Spring Cloud Alibaba【Sentinel控制台环境安装基于Linux、将应用接入Sentinel、流量控制概述、流控模式之直接模式、流控模式之关联模式 】(六)
    ReentrantLock 实现原理
    Curl 命令方式对elasticsearch备份和恢复
    ctf之:《kali-linux-2022-W48-virtualbox-amd64》工具测试netdiscover
    尾递归,还是递推?
    类和函数的泛化、偏特化和全特化
    SIMULIA现实仿真解决方案 SIMULIA仿真模拟应用程序
    ImmunoChemistry艾美捷ELISA洗涤缓冲液说明书
    【华为机试真题 JAVA】单词接龙-100
  • 原文地址:https://blog.csdn.net/qq_27586963/article/details/133045735