• 【C++】设计模式之工厂模式



    C++工厂模式
    工厂模式C++实现(三种工厂模式附详细注释)

    什么是工厂模式

    工厂模式是一种创建型模式,
    适用场景:安全创建对象
    顾名思义,工厂是用来生产东西的,而在C++里面就是用来生产对象的。

    使用工厂模式的好处

    解耦
    通过工厂模式可以把对象创建使用过程分割开来。比如说 Class A 想调用 Class B的方法,那么我们无需关心B是如何创建的,直接去工厂获取就行。

    减少代码量,易于维护
    如果我们直接new一个对象时,如果需要的对象构造方法比较复杂,那么可能需要一连串的代码去创建对象,如果在别的类中又需要创建该对象,那么代码的重复度肯定不小。通过工厂模式的话,我们把对象创建的具体逻辑给隐藏起来了,交给工厂统一管理,这样不仅减少了代码量,以后如果想改代码的话,只需要改一处即可,也方便我们日常的维护。

    原文链接:https://blog.csdn.net/weixin_42780235/article/details/106503298

    类别

    简单工厂模式
    工厂方法模式
    抽象工厂模式

    特点

    简单工厂模式:一个工厂N个产品
    工厂模式:N个工厂N个产品
    抽象工厂模式:N个工厂M个产品

    缺点(相对)

    简单工厂模式:违背了开放封闭原则(软件实现应该对扩展开放,对修改关闭。改动源码),产品多了会比较杂
    工厂模式:产品多了会很杂,代码量稍大。当我们新增产品时,还需要提供对应的工厂类,系统中类的个数将会成倍增加,相当于增加了系统的复杂性。
    抽象工厂模式: 扩展新种类产品时困难。需要增加一个对应的产品的具体工厂类,这就会增大了代码的编写量。

    优点(相对)

    简单工厂模式:代码量小,方便添加新的产品。结构简单,管理方式简单。
    工厂模式:符合开放封闭原则(不改动源码),方便添加新的产品。符合单一职责原则,每个工厂只负责一种产品,而不是由一个工厂去生成所有商品。
    抽象工厂模式:模式灵活,调用方便

    简单工厂模式

    一个产品抽象基类,三个具体产品子类,一个工厂类。

    //简单工厂模式
    typedef enum ProductTypeTag
    {
    TypeA,
    TypeB,
    TypeC
    }PRODUCTTYPE;
    class Product//产品抽象基类
    {
    public:
    virtual void Show() = 0;
    };
    class ProductA : public Product
    {
    public:
    void Show()
    {
    cout<<"I'm ProductA"<<endl;
    }
    };
    class ProductB : public Product
    {
    public:
    void Show()
    {
    cout<<"I'm ProductB"<<endl;
    }
    };
    class ProductC : public Product
    {
    public:
    void Show()
    {
    cout<<"I'm ProductC"<<endl;
    }
    };
    class Factory//工厂类
    {
    public:
    Product* CreateProduct(PRODUCTTYPE type)
    {
    switch (type)
    {
    case TypeA:
    return new ProductA();
    case TypeB:
    return new ProductB();
    case TypeC:
    return new ProductC();
    default:
    return NULL;
    }
    }
    };
    int main()
    {
    Factory productCreator;
    Product *productA=productCreator.CreateProduct(TypeA);
    Product *productB=productCreator.CreateProduct(TypeB);
    Product *productC=productCreator.CreateProduct(TypeC);
    productA->Show();
    productB->Show();
    productC->Show();
    if(productA){
    delete productA;
    productA=NULL;
    }
    if(productB){
    delete productB;
    productB=NULL;
    }
    if(productC){
    delete productC;
    productC=NULL;
    }
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    工厂模式

    一个产品抽象基类,三个具体产品子类;一个工厂抽象基类,三个具体工厂子类。

    //工厂方法模式
    typedef enum ProductTypeTag
    {
    TypeA,
    TypeB,
    TypeC
    }PRODUCTTYPE;
    class Product//产品抽象基类
    {
    public:
    virtual void Show() = 0;
    };
    class ProductA : public Product
    {
    public:
    void Show()
    {
    cout<<"I'm ProductA"<<endl;
    }
    };
    class ProductB : public Product
    {
    public:
    void Show()
    {
    cout<<"I'm ProductB"<<endl;
    }
    };
    class Factory//工厂类
    {
    public:
    virtual Product *createProduct()=0;
    };
    class FactoryA:public Factory{
    public:
    Product *createProduct(){
    return new ProductA();
    }
    };
    class FactoryB:public Factory{
    public:
    Product *createProduct(){
    return new ProductB();
    }
    };
    class FactoryC:public Factory{
    public:
    Product *createProduct(){
    return new ProductC();
    }
    };
    int main()
    {
    Factory *factoryA=new FactoryA();
    Product *productA = factoryA->createProduct();
    productA->Show();
    Factory *factoryB=new FactoryB();
    Product *productB = factoryB->createProduct();
    productB->Show();
    if (factoryA)
    {
    delete factoryA;
    factoryA = NULL;
    }
    if (factoryB)
    {
    delete factoryB;
    factoryB = NULL;
    }
    if (productA)
    {
    delete productA;
    productA = NULL;
    }
    if (productB)
    {
    delete productB;
    productB = NULL;
    }
    return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    抽象工厂模式

    抽象工厂模式更适合实际情况,受生产线所限,让低端工厂生产不同种类的低端产品,高端工厂生产不同种类的高端产品。

    例如:一个品牌基类,一个工厂基类。两个品牌子类A、B。各有两种产品Ac、Ad、Bc、Bd。两个工厂子类Fc、Fd。Fc生产两个品牌的c类产品(Ac、Bc),Fd生产两个品牌的d类产品(Ad、Bd)

    //抽象工厂模式
    class ProductA
    {
    public:
    virtual void Show() = 0;
    };
    class ProductA1 : public ProductA//A类低端产品
    {
    public:
    void Show()
    {
    cout<<"I'm ProductA1"<<endl;
    }
    };
    class ProductA2 : public ProductA//A类高端产品
    {
    public:
    void Show()
    {
    cout<<"I'm ProductA2"<<endl;
    }
    };
    class ProductB
    {
    public:
    virtual void Show() = 0;
    };
    class ProductB1 : public ProductB//B类低端产品
    {
    public:
    void Show()
    {
    cout<<"I'm ProductB1"<<endl;
    }
    };
    class ProductB2 : public ProductB//B类高端产品
    {
    public:
    void Show()
    {
    cout<<"I'm ProductB2"<<endl;
    }
    };
    class Factory
    {
    public:
    virtual ProductA *CreateProductA() = 0;
    virtual ProductB *CreateProductB() = 0;
    };
    class Factory1 : public Factory//1号工厂用于生产低端产品
    {
    public:
    ProductA *CreateProductA()
    {
    return new ProductA1();
    }
    ProductB *CreateProductB()
    {
    return new ProductB1();
    }
    };
    class Factory2 : public Factory//2号工厂用于生产高端产品
    {
    ProductA *CreateProductA()
    {
    return new ProductA2();
    }
    ProductB *CreateProductB()
    {
    return new ProductB2();
    }
    };
    int main()
    {
    Factory *factory1 = new Factory1();
    ProductA *productA1 = factory1->CreateProductA();
    ProductB *productB1 = factory1->CreateProductB();
    productA1->Show();
    productB1->Show();
    Factory *factory2 = new Factory2();
    ProductA *productA2 = factory2->CreateProductA();
    ProductB *productB2 = factory2->CreateProductB();
    productA2->Show();
    productB2->Show();
    if (factory1)
    {
    delete factory1;
    factory1 = NULL;
    }
    if (productA1)
    {
    delete productA1;
    productA1= NULL;
    }
    if (productB1)
    {
    delete productB1;
    productB1 = NULL;
    }
    if (factory2)
    {
    delete factory2;
    factory2 = NULL;
    }
    if (productA2)
    {
    delete productA2;
    productA2 = NULL;
    }
    if (productB2)
    {
    delete productB2;
    productB2 = NULL;
    }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
  • 相关阅读:
    Element - el-tree 树形结构拖拽以及增删改查
    java基于微信小程序的劳务知识分享系统 uniapp 小程序
    docker基础用法
    iOS 蓝牙连接 connectionSupervisionTimeout 是720ms 还是 2~6s
    图书管理系统的测试
    老年人入住 ICU 的疾病严重程度和死亡率的趋势
    如何使用Matplotlib模块的text()函数给柱形图添加美丽的标签数据?
    【python数据建模】Sympy库
    爱剪辑如何将视频旋转90度,详细操作流程
    Oracle到PostgreSQL的不停机数据库迁移
  • 原文地址:https://blog.csdn.net/kin_16/article/details/126542899