zoukankan      html  css  js  c++  java
  • 创建型模式之 抽象工厂模式

    介绍参见菜鸟教程

    下面给出C++的一个例子

    #include<iostream>
    #include<memory>
    using namespace std;
    
    //shap接口
    class shap {
    public:
        virtual void draw() {};
    };
    
    //shap接口的实现类
    class Rect : public shap 
    {
     public:
         void draw() { std::cout << "生成一个矩形" << std::endl; }
    };
    class Squ : public shap
    {
    public:
        void draw() { std::cout << "生成一个正方形" << std::endl; }
    };
    class Circ : public shap
    {
    public:
        void draw() { std::cout << "生成一个圆" << std::endl; }
    };
    
    //color接口
    class color 
    {
    public:
        virtual void fill() {};
    };
    
    //color 实现类
    class Red : public color 
    {
    public:
        void fill() { std::cout << "填充红色" << std::endl; }
    };
    class Green : public color
    {
    public:
        void fill() { std::cout << "填充绿色" << std::endl; }
    };
    class Blue : public color
    {
    public:
        void fill() { std::cout << "填充蓝色" << std::endl; }
    };
    
    //为 Color 和 Shape 对象创建抽象类来获取工厂
    class AbstractFactory 
    {
    public:
        virtual color* GetColor(string strColor) { return 0; };
        virtual shap*  GetShap(string strShap) {  return 0; };
    };
    
    //创建shap工厂
    class ShapFactory : public AbstractFactory
    {
    public:
        ShapFactory() { cShap = NULL;  }
        ~ShapFactory() { ReleaseFactouy(); }
    
        shap* GetShap(string strShap)
        {
            if (strShap.compare("rectangle") == 0)
            {
                cShap = new Rect;
            }        
            if (strShap.compare("square") == 0)
            {
                cShap = new Squ;
            }
            if (strShap.compare("circle") == 0)
            {
                cShap = new Circ;
            }
            return cShap;
        }
        void ReleaseFactouy() 
        {
            if (cShap != NULL)
            {
                delete cShap;
            }
        }
    private:
        shap * cShap;
    };
    
    
    //创建色彩工厂
    class ColorFactory : public AbstractFactory
    {
    public:
        ColorFactory() { cColor = NULL; }
        ~ColorFactory() { ReleaseFactouy(); }
    
        color* GetColor(string strColor)
        {
            if (strColor.compare("red") == 0)
            {
                cColor = new Red;
            }
            if (strColor.compare("green") == 0)
            {
                cColor = new Green;
            }
            if (strColor.compare("blue") == 0)
            {
                cColor = new Blue;
            }
            return cColor;
        }
        void ReleaseFactouy()
        {
            if (cColor != NULL)
            {
                delete cColor;
            }
        }
    private:
        color * cColor;
    };
    
    //创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂
    class FactroyProduce 
    {
    private:
        FactroyProduce() {};
        ~FactroyProduce() {};
        int i;
    public:
        static AbstractFactory* GetFactory(string strFact)
        {
            if (strFact.compare("shap") == 0 )
            {
                cout << "新的mew1" << endl;
                return new ShapFactory;
            }
            else if (strFact.compare("color") == 0)
            {
                cout << "新的mew2" << endl;
                return new ColorFactory;
            }
            else
            {
                return NULL;
            }
                
        }
    };
    int main()
    {
        shared_ptr<AbstractFactory> AbsFact(FactroyProduce::GetFactory("shap"));
        shap* shap1 = AbsFact->GetShap("rectangle");
        shap1->draw();        //生成一个矩形
        //...
        shared_ptr<AbstractFactory> AbsFact2(FactroyProduce::GetFactory("color"));
        color* color1 = AbsFact2->GetColor("red");
        color1->fill();        //生成红色
        //...
        return 0;
    }

     

  • 相关阅读:
    MySQLSource-Flume
    Flume
    Hive| ETL清洗& 查询练习
    Hive-04 压缩| 存储
    Hive-03 查询
    Hive-02 DDL| DML
    shell基本用法
    Hive-01 配置| 架构原理
    HDFS-HA高可用 | Yarn-HA
    react-路由和Ant design
  • 原文地址:https://www.cnblogs.com/gardenofhu/p/8488118.html
Copyright © 2011-2022 走看看