2、C++设计模式——抽象工厂模式(菜鸟教程例子)

原例网址:抽象工厂模式(菜鸟教程)

抽象工厂模式

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

 

#include "iostream"
#include "string"
using namespace std;


//步骤1 为形状创建一个接口
class Shape
{
public:
    virtual void draw() = 0;
};


//步骤2 创建实现形状接口的实体类
class Circle : public Shape
{
public:
    void draw()
    {
        cout << "Inside Circle::draw() method." << endl;
    }
};

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

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


//步骤3 为颜色创建一个接口
class Color
{
public:
    virtual void fill() = 0;
};


//步骤4 创建实现颜色接口的实体类
class Red : public Color
{
public:
    void fill()
    {
        cout << "Inside Red::fill() method." << endl;
    }
};

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

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


//步骤5 为 Color 和 Shape 对象创建抽象类来获取工厂
class AbstractFactory
{
public:
    virtual Shape* getShape(string shapeType) = 0;
    virtual Color* getColor(string color) = 0;
};


//步骤6 创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象
class ShapeFactory : public AbstractFactory
{
public:
    Shape* getShape(string shapeType)
    {
        if ( shapeType == "CIRCLE" )
            return new Circle;
        else if ( shapeType == "SQUARE" )
            return new Square;
        else if ( shapeType == "RECTANGLE" )
            return new Rectangle;
        else
        {
            cout << "未知的shapeType" << endl;
            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 == "RED" )
            return new Red;
        else if ( color == "GREEN" )
            return new Green;
        else if ( color == "BLUE" )
            return new Blue;
        else
        {
            cout << "未知的color" << endl;
            return NULL;
        }
    }
};


//步骤7 创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂
class FactoryProducer
{
public:
    AbstractFactory* getFactory(string choice)
    {
        if ( choice == "SHAPE" )
            return new ShapeFactory;
        else if ( choice == "COLOR" )
            return new ColorFactory;
        else
        {
            cout << "未知的choice" << endl;
            return NULL;
        }
    }
};


//步骤8 使用 FactoryProducer 来获取 AbstractFactory,通过传递类型信息来获取实体类的对象
void main()
{
    FactoryProducer *factoryProducer = NULL;
    AbstractFactory *factory = NULL;
    Shape *shape =  NULL;
    Color *color = NULL;


    factoryProducer = new FactoryProducer;

    //获取形状工厂
    factory = factoryProducer->getFactory("SHAPE");

    //获取形状为 Circle 的对象
    shape = factory->getShape("CIRCLE");
    //调用 Circle 的 draw 方法
    shape->draw();
    //删除 Circle 对象
    delete shape;

    //获取形状为 Square 的对象
    shape = factory->getShape("SQUARE");
    //调用 Square 的 draw 方法
    shape->draw();
    //删除 Square 对象
    delete shape;

    //获取形状为 Rectangle 的对象
    shape = factory->getShape("RECTANGLE");
    //调用 Rectangle 的 draw 方法
    shape->draw();
    //删除 Rectangle 对象
    delete shape;


    //删除形状工厂
    delete factory;

    //获取颜色工厂
    factory = factoryProducer->getFactory("COLOR");
    //获取颜色为 Red 的对象
    color = factory->getColor("RED");
    //调用 Red 的 fill 方法
    color->fill();
    //删除 Red 对象
    delete color;

    //获取颜色为 Green 的对象
    color = factory->getColor("GREEN");
    //调用 Green 的 fill 方法
    color->fill();
    //删除 Green 对象
    delete color;

    //获取颜色为 Blue 的对象
    color = factory->getColor("BLUE");
    //调用 Blue 的 fill 方法
    color->fill();
    //删除 Blue 对象
    delete color;

    delete factoryProducer;
    delete factory;
    //delete shape;
    //delete color;
    
    system("pause");
}

 

运行结果:

posted @ 2022-03-31 15:44  张三Nefe  阅读(325)  评论(0)    收藏  举报