抽象工厂模式C++实现

参考:http://www.runoob.com/design-pattern/abstract-factory-pattern.html

C++代码实现:(为了简单演示,这些类的定义都在一个头文件中,具体使用的时候,应该分开文件存放)

#include <iostream>
using namespace std;

class Shape{
public:
    virtual void draw() = 0;
};

class Rectangle : public Shape{
public:
    void draw(){
        cout << "Inside Rectangle::draw() method." << endl;
    }
};

class Square : public Shape{
public:
    void draw(){
        cout << "Inside Square::draw() method." << endl;
    }
};

class Circle : public Shape{
public:
    void draw(){
        cout << "Inside Circle::draw() method." << endl;
    }
};

class Color{
public:
    virtual void fill() = 0;
};

class Red : public Color{
public:
    void fill(){
        cout << "Inside Red:fill() methon." << endl;
    }
};

class Green : public Color{
public:
    void fill(){
        cout << "Inside Green:fill() methon." << endl;
    }
};

class Blue : public Color{
public:
    void fill(){
        cout << "Inside Blue:fill() methon." << endl;
    }
};

class AbstractFactory{
public:
    virtual Color* getColor(string color) = 0;
    virtual Shape* getShape(string shape) = 0;
};

class ShapeFactory : public AbstractFactory{
public:
    Shape* getShape(string shapeType){
        if (shapeType.empty())
        {
            return NULL;
        }

        if (shapeType == "CIRCLE")
        {
            return new Circle();
        }
        else if (shapeType == "RECTANGLE")
        {
            return new Rectangle();
        }
        else if (shapeType == "SQUARE")
        {
            return new Square();
        }
        return NULL;
    }

    Color* getColor(string color){
        return NULL;
    }
};

class ColorFactory : public AbstractFactory{
public:
    Shape* getShape(string shapeType){
        return NULL;
    }

    Color* getColor(string color){
        if (color.empty())
        {
            return NULL;
        }
        else if (color == "RED")
        {
            return new Red();
        }
        else if (color == "Green")
        {
            return new Green();
        }
        else if (color == "BLUE")
        {
            return new Blue();
        }
        return NULL;
    }
};

class FactoryProducer
{
public:
    static AbstractFactory* getFactory(string choice){
        if (choice == "SHAPE")
        {
            return new ShapeFactory();
        }
        else if (choice == "COLOR")
        {
            return new ColorFactory();
        }
        return NULL;
    }
};

测试:

#include "AbstractFactory.h"

int main()
{
    //获取形状工厂
    AbstractFactory* shapeFactory = FactoryProducer::getFactory("SHAPE");

    //获取形状为Circle的对象
    Shape* shape1 = shapeFactory->getShape("CIRCLE");

    //调用Circle的draw方法
    shape1->draw();

    //获取形状为Rectangle的对象
    Shape *shape2 = shapeFactory->getShape("RECTANGLE");

    //调用Rectangle的draw方法
    shape2->draw();

    //获取形状为Square的对象
    Shape *shape3 = shapeFactory->getShape("SQUARE");

    //调用Square的draw方法
    shape3->draw();

    //获取颜色工厂
    AbstractFactory* colorFactory = FactoryProducer::getFactory("COLOR");

    //获取颜色为Red的对象
    Color *color1 = colorFactory->getColor("RED");

    //调用Green的fill方法
    color1->fill();

    //获取颜色为Green的对象
    Color *color2 = colorFactory->getColor("Green");

    //调用Red的fill方法
    color2->fill();

    //获取颜色为Blue的对象
    Color *color3 = colorFactory->getColor("BLUE");

    //调用Blue的fill方法
    color3->fill();

    getchar();
    return 0;
}

运行结果:

 

posted @ 2018-02-02 12:02  懒猫的新窝  阅读(1078)  评论(0编辑  收藏  举报