设计模式2 - 抽象工厂模式

作者:billy
版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处

抽象工厂模式

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

使用场景

  • 用户无需关心对象的创建过程,将对象的创建和使用解耦。
  • 产品等级结构稳定,在设计完成之后不会向系统中增加新的产品等级结构或者删除已有的产品等级结构。
  • 系统中有多于一个的产品族,而每次只使用其中某一产品族。可以通过配置文件等方式来使用户能够动态改变产品族,也可以很方便的增加新的产品族。

优缺点

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

  • 缺点:
    产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 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
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值