zoukankan      html  css  js  c++  java
  • 设计模式 C++ 抽象工厂模式

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    class Shape
    {
    public:
        virtual void draw()=0;
        virtual ~Shape(){}
    };
    
    
    class Rectangle :public Shape
    {
    public:
        void draw()
        {
            cout << "from rectangle"<<endl;
        }
    };
    
    class Circle :public Shape
    {
    public:
        void draw()
        {
            cout << "from circle"<< endl;
        }
    };
    
    class Color
    {
    public:
        virtual void fill()=0;
        virtual ~Color(){}
    };
    
    class Green:public Color
    {
    public:
        void fill()
        {
            cout << "color green"<<endl;
        }
    };
    
    class Red:public Color
    {
    public:
        void fill()
        {
            cout << "color red"<<endl;
        }
    };
    
    class AbFactory
    {
    public:
        virtual Shape* createShape(string Shape)=0;
        virtual Color* fillColor(string color)=0;
        virtual ~AbFactory(){}
    };
    
    class ShapeFactory:public AbFactory
    {
    public:
        Color* fillColor(string color)
        {
            return NULL;
        }
        Shape* createShape(string shape)
        {
            if(shape=="rectangle"){
                return new Rectangle();
            }
            else if(shape == "circle")
            {
                return new Circle();
            }
            return NULL;
        }
    };
    
    class ColorFactory:public AbFactory
    {
    public:
        Color* fillColor(string color)
        {
            if(color=="green"){
                return new Green();
            }
            else if(color == "red")
            {
                return new Red();
            }
            return NULL;
        }
        Shape* createShape(string shape)
        {
            return NULL;
        }
    };
    
    
    
    
    
    
    int main(int argc, char *argv[])
    {
        string str = "rectangle";
        if(str == "rectangle")
            cout <<"1"<<endl;
        else
            cout <<"0"<<endl;
    
    
        AbFactory* abfactory;
        abfactory = new ShapeFactory();
        Shape* shape = abfactory->createShape("rectangle");
        if(shape == NULL)
        {
            cout << "shape is NULL";
        }
        else
        {
            shape->draw();
        }
    
    
        delete shape;
        delete abfactory;
    
        abfactory = new ColorFactory();
        Color* color = abfactory->fillColor("red");
        if(color == NULL)
        {
            cout << "color is NULL";
        }
        else
        {
            color->fill();
        }
    
    
        delete color;
        delete abfactory;
    
        return 0;
    
    }
    
  • 相关阅读:
    从Malvar的论文与两通道QMF设计原理到Speex 与 ISAC中的QMF使用
    转:薪酬与GDP
    转:A PitchEnergy Quantizer for Codec2
    韩国JoonHyuk Chang DSP Lab 专家
    Audio Codec : MPEG2 AAC 反量化模块
    VOIP Codec 三剑客之 SILK (1) 介绍
    CELT 视频PPT介绍
    HEAAC专利
    关于变换编码算法的(Blocking artifacts)和(Ringing artifacts)(一)
    转:免费国际长途 热门应用Line发布中文版
  • 原文地址:https://www.cnblogs.com/fundou/p/11103697.html
Copyright © 2011-2022 走看看