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;
    }

     

  • 相关阅读:
    用C#一步步创建Window Service (转) 沧海一粟
    IOS 开发,调用打电话,发短信,打开网址 沧海一粟
    IOS UIScrollView (转) 沧海一粟
    苹果IOS开发者账号总结 沧海一粟
    ios公司开发者账号申请分享攻略(转自yiwind0101) 沧海一粟
    iOS开发:自定义UITableViewCell(转) 沧海一粟
    可任意自定义的UITableViewCell(转) 沧海一粟
    iphone开发获取当前app的名称和版本号 沧海一粟
    SMTP协议在cmd下利用命令行发送邮件(转) 沧海一粟
    iPhone提供的4种基本的表格视图单元格 沧海一粟
  • 原文地址:https://www.cnblogs.com/gardenofhu/p/8488118.html
Copyright © 2011-2022 走看看