C++设计模式:抽象工厂模式

抽象工厂模式

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

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

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

使用场景

用户无需关心对象的创建过程,将对象的创建和使用解耦。

产品等级结构稳定,在设计完成之后不会向系统中增加新的产品等级结构或者删除已有的产品等级结构。

系统中有多于一个的产品族,而每次只使用其中某一产品族。可以通过配置文件等方式来使用户能够动态改变产品族,也可以很方便的增加新的产品族。

优缺点

优点:

当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。

缺点:

产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。

注意事项

产品族难扩展,产品等级易扩展。

UML结构图

 

代码实现

interface.h
创建抽象类 - 图形; 创建具体类 - 圆形、正方形、矩形
创建抽象类 - 颜色; 创建具体类 - 红色、绿色、蓝色

#include <iostream>
using namespace std;

class Shape //创建基类-图形
{
public:
    Shape() { cout << "create Shape" << endl; }
    virtual ~Shape() {}

    virtual void draw() = 0;
};

class Circle: public Shape  //子类-圆形
{
public:
    Circle() { cout << "create Circle" << endl; }

    void draw() { cout << "draw Circle" << endl; }
};

class Square: public Shape  //子类-正方形
{
public:
    Square() { cout << "create Square" << endl; }

    void draw() { cout << "draw Square" << endl; }
};

class Rectangle: public Shape   //子类-矩形
{
public:
    Rectangle() { cout << "create Rectangle" << endl; }

    void draw() { cout << "draw Rectangle" << endl; }
};



class Color //创建基类-颜色
{
public:
    Color() { cout << "create Color" << endl; }
    virtual ~Color() {}

    virtual void fill() = 0;
};

class Red: public Color //子类-红色
{
public:
    Red() { cout << "create Red" << endl; }

    void fill() { cout << "fill Red" << endl; }
};

class Green: public Color   //子类-绿色
{
public:
    Green() { cout << "create Green" << endl; }

    void fill() { cout << "fill Green" << endl; }
};

class Blue: public Color    //子类-蓝色
{
public:
    Blue() { cout << "create Blue" << endl; }

    void fill() { cout << "fill Blue" << endl; }
};

abstractfactory.h
创建抽象类 - 抽象工厂;创建具体类 - 图形工厂、颜色工厂

#include "interface.h"
#include <string>

class AbstractFactory   //基类-抽象工厂
{
public:
    AbstractFactory() { cout << "create AbstractFactory" << endl; }
    virtual ~AbstractFactory() {}

    virtual Shape * getShape(string type) = 0;
    virtual Color * getColor(string type) = 0;
};

class ShapeFactory: public AbstractFactory  //子类-图形工厂
{
public:
    ShapeFactory() { cout << "create ShapeFactory" << endl; }

    Shape * getShape(string type)
    {
        if (type.compare("Circle") == 0)
        {
            return new Circle();
        }
        else if (type.compare("Square") == 0)
        {
            return new Square();
        }
        else if (type.compare("Rectangle") == 0)
        {
            return new Rectangle();
        }
        else
        {
            return nullptr;
        }
    }

    Color * getColor(string type) { return nullptr; }
};

class ColorFactory: public AbstractFactory  //子类-颜色工厂
{
public:
    ColorFactory() { cout << "create ColorFactory" << endl; }

    Color * getColor(string type)
    {
        if (type.compare("Red") == 0)
        {
            return new Red();
        }
        else if (type.compare("Green") == 0)
        {
            return new Green();
        }
        else if (type.compare("Blue") == 0)
        {
            return new Blue();
        }
        else
        {
            return nullptr;
        }
    }

    Shape * getShape(string type) { return nullptr; }
};

factoryproducer.h
创建工厂提供类 - 可根据选择的类型创建相应的工厂

#include "abstractfactory.h"

class FactoryProducer
{
public:
    FactoryProducer() {}

    AbstractFactory * getFactory(string type)
    {
        if (type.compare("ShapeFactory") == 0)
        {
            return new ShapeFactory();
        }
        else if (type.compare("ColorFactory") == 0)
        {
            return new ColorFactory();
        }
        else
        {
            return nullptr;
        }
    }
};

main.cpp
实例应用 - 根据输入的类型创建相应的工厂,
再根据输入的类型创建相应的产品,隐藏了工厂和产品的创建方法

#include "factoryproducer.h"
#include <iostream>
using namespace std;

int main()
{
    FactoryProducer factoryProducer;

    AbstractFactory * shapeFactory = factoryProducer.getFactory("ShapeFactory");
    AbstractFactory * colorFactory = factoryProducer.getFactory("ColorFactory");
    cout << endl;

    Shape * circle = shapeFactory->getShape("Circle");
    circle->draw();
    cout << endl;

    Shape * square = shapeFactory->getShape("Square");
    square->draw();
    cout << endl;

    Shape * rectangle = shapeFactory->getShape("Rectangle");
    rectangle->draw();
    cout << endl;

    Color * red = colorFactory->getColor("Red");
    red->fill();
    cout << endl;

    Color * green = colorFactory->getColor("Green");
    green->fill();
    cout << endl;

    Color * blue = colorFactory->getColor("Blue");
    blue->fill();

    return 0;
}

运行结果:
create AbstractFactory
create ShapeFactory
create AbstractFactory
create ColorFactory

create Shape
create Circle
draw Circle

create Shape
create Square
draw Square

create Shape
create Rectangle
draw Rectangle

create Color
create Red
fill Red

create Color
create Green
fill Green

create Color
create Blue
fill Blue

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单工厂模式: 简单工厂模式是一种创建型设计模式,它提供了一种创建对象的最佳方式。在简单工厂模式中,我们创建对象而无需向客户端暴露创建逻辑。这种类型的设计模式属于创建型模式,提供了一种创建对象的最佳方式。 C++代码实现: ```c++ #include <iostream> using namespace std; // 抽象产品 class Product { public: virtual void Show() = 0; }; // 具体产品 A class ConcreteProductA : public Product { public: void Show() { cout << "ConcreteProductA Show" << endl; } }; // 具体产品 B class ConcreteProductB : public Product { public: void Show() { cout << "ConcreteProductB Show" << endl; } }; // 工厂类 class Factory { public: Product* CreateProduct(int type) { switch (type) { case 1: return new ConcreteProductA(); case 2: return new ConcreteProductB(); default: return nullptr; } } }; int main() { Factory factory; Product* productA = factory.CreateProduct(1); if (productA != nullptr) { productA->Show(); } Product* productB = factory.CreateProduct(2); if (productB != nullptr) { productB->Show(); } delete productA; delete productB; return 0; } ``` 抽象工厂模式抽象工厂模式是一种创建型设计模式,它提供了一种创建一系列相关或依赖对象的最佳方式。抽象工厂模式的基本思想是提供一个接口,用于创建与产品簇相关的一族对象,而不需要明确指定它们的类。 C++代码实现: ```c++ #include <iostream> using namespace std; // 抽象产品 A class AbstractProductA { public: virtual void Show() = 0; }; // 具体产品 A1 class ProductA1 : public AbstractProductA { public: void Show() { cout << "ProductA1 Show" << endl; } }; // 具体产品 A2 class ProductA2 : public AbstractProductA { public: void Show() { cout << "ProductA2 Show" << endl; } }; // 抽象产品 B class AbstractProductB { public: virtual void Show() = 0; }; // 具体产品 B1 class ProductB1 : public AbstractProductB { public: void Show() { cout << "ProductB1 Show" << endl; } }; // 具体产品 B2 class ProductB2 : public AbstractProductB { public: void Show() { cout << "ProductB2 Show" << endl; } }; // 抽象工厂 class AbstractFactory { public: virtual AbstractProductA* CreateProductA() = 0; virtual AbstractProductB* CreateProductB() = 0; }; // 具体工厂 1 class ConcreteFactory1 : public AbstractFactory { public: AbstractProductA* CreateProductA() { return new ProductA1(); } AbstractProductB* CreateProductB() { return new ProductB1(); } }; // 具体工厂 2 class ConcreteFactory2 : public AbstractFactory { public: AbstractProductA* CreateProductA() { return new ProductA2(); } AbstractProductB* CreateProductB() { return new ProductB2(); } }; int main() { AbstractFactory* factory = new ConcreteFactory1(); AbstractProductA* productA = factory->CreateProductA(); AbstractProductB* productB = factory->CreateProductB(); productA->Show(); productB->Show(); delete productA; delete productB; factory = new ConcreteFactory2(); productA = factory->CreateProductA(); productB = factory->CreateProductB(); productA->Show(); productB->Show(); delete productA; delete productB; delete factory; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值