zoukankan      html  css  js  c++  java
  • 设计模式(三)--抽象工厂模式

    设计模式(三)--抽象工厂模式


    抽象工厂模式的作用。能够处理具有同样等级结构的多个产品族中产品对象的创建问题。

    首先解释一下产品族和同样等级结构的概念


    同样等级结构:  抽象产品A和抽象产品B处于同一个继承等级(父类),因此成为具有同样等级结构。这里关键是要理解A和B都是相互独                         立的抽象产品。在JAVA中用接口定义。

    产品族:详细产品A1和详细产品A2都是由抽象产品继承下来的产品,这两个子类成为父类的产品族




    抽象工厂的类图例如以下



    抽象工厂的源码

    public interface Creator  
    {  
        /** 
         * A的抽象工厂方法
         */  
        public ProductA factoryA();  
        /** 
         * B的抽象工厂方法
         */  
        public ProductB factoryB();  
    }  

    详细工厂1的源代码

    public class ConcreteCreator1 implements Creator  
    {  
        /** 
         * A的详细工厂 
         */  
        public ProductA factoryA()  
        {  
            return new ProductA1();  
        }  
        /** 
         * B的详细工厂 
         */  
        public ProductB factoryB()  
        {  
            return new ProductB1();  
        }  
    } 

    详细工厂2的源代码

    public class ConcreteCreator2 implements Creator  
    {  
        /** 
         * A的详细工厂 
         */  
        public ProductA factoryA()  
        {  
            return new ProductA2();  
        }  
        /** 
         * B的详细工厂 
         */  
        public ProductB factoryB()  
        {  
            return new ProductB2();  
        }  
    } 

    抽象产品A源代码

    public interface ProductA  
    {  
    }  

    抽象产品B源代码

    public interface ProductB  
    {  
    }  

    详细产品A1源代码

    public class ProductA1 implements ProductA  
    {  
        public ProductA1()  
        {  
            //do something  
        }  
    }  

    详细产品A2源代码

    public class ProductA2 implements ProductA  
    {  
        public ProductA2()  
        {  
            //do something  
        }  
    }  

    详细产品B1源代码

    public class ProductB1 implements ProductB  
    {  
        public ProductB1()  
        {  
            //do something  
        }  
    }  

    详细产品B2源代码

    public class ProductB2 implements ProductB  
    {  
        public ProductB2()  
        {  
            //do something  
        }  
    }  



  • 相关阅读:
    图片处理
    define 常量的定义和读取
    curl
    stream_get_contents 和file_get_content的区别
    php flock 文件锁
    字符串函数
    php 常量
    debug_backtrace()
    pathlib模块替代os.path
    Python中对 文件 的各种骚操作
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/7050276.html
Copyright © 2011-2022 走看看