三种工厂模式

工厂模式分为三种类型,分别是简单工厂,工厂方法和抽象工厂。这三种类型的实现都和虚函数分不开。

一.简单工厂

class Product
{
public:
    virtual int fun(int a, int b) = 0;
};

class ProductAdd : public Product
{
public:
    int fun(int a, int b) {
        return a + b;
    }
};

class ProductMul : public Product
{
public:
    int fun(int a, int b) {
        return a * b;
    }
};

class Factory
{
public:
    Product* create(int i) {
        switch (i) {
            case 1: return new ProductAdd; break;
            case 2: return new ProductMul; break;
            default:; break;
        }
        //return new ProductAdd;
    }
};

int main()
{
    Factory *fac = new Factory;
    cout << fac->create(1)->fun(2, 3) << endl;
    cout << fac->create(2)->fun(2, 3) << endl;
    return 0;
}

假如添加一个减法,需要改之前实现的部分,不方便维护,这有违软件开发的开放-封闭原则。

二.工厂方法

class Product
{
public:
    virtual int fun(int a, int b) = 0;
};

class ProductAdd : public Product
{
public:
    int fun(int a, int b) {
        return a + b;
    }
};

class ProductMul : public Product
{
public:
    int fun(int a, int b) {
        return a * b;
    }
};

class Factory
{
public:
    virtual Product* create() = 0;
};

class FactoryAdd : public Factory
{
public:
    Product* create() {
        return new ProductAdd;
    }
};

class FactoryMul : public Factory
{
public:
    Product* create() {
        return new ProductMul;
    }
};

int main()
{
    Factory *facadd = new FactoryAdd;
    Factory *facmul = new FactoryMul;

    cout << facadd->create()->fun(2, 3) << endl;
    cout << facmul->create()->fun(2, 3) << endl;
    return 0;
}

工厂方法这种方式扩展新的产品时避免了修改之前的代码,但是每种工厂只能生产一种产品。

三.抽象工厂

class Product
{
public:
    virtual int fun(int a, int b) = 0;
};

class ProductAdd : public Product
{
public:
    int fun(int a, int b) {
        return a + b;
    }
};

class ProductMul : public Product
{
public:
    int fun(int a, int b) {
        return a * b;
    }
};

class ProductN
{
public:
    virtual int fun(int a, int b) = 0;
};

class ProductNAdd : public ProductN
{
public:
    int fun(int a, int b) {
        return (-a) + (-b);
    }
};

class ProductNMul : public ProductN
{
public:
    int fun(int a, int b) {
        return (-a) * (-b);
    }
};

class Factory
{
public:
    virtual Product* create() = 0;
    virtual ProductN* create_n() = 0;
};

class FactoryAdd : public Factory
{
public:
    Product* create() {
        return new ProductAdd;
    }
    ProductN* create_n() {
        return new ProductNAdd;
    }
};

class FactoryMul : public Factory
{
public:
    Product* create() {
        return new ProductMul;
    }
    ProductNMul* create_n() {
        return new ProductNMul;
    }
};

int main()
{
    Factory *facadd = new FactoryAdd;
    Factory *facmul = new FactoryMul;

    cout << facadd->create()->fun(2, 3) << endl;
    cout << facadd->create_n()->fun(2, 3) << endl;
    cout << facmul->create()->fun(2, 3) << endl;
    cout << facmul->create_n()->fun(2, 3) << endl;

    return 0;
}

抽象工厂其实是前面两种方法的一个结合,相对于平行扩展了一个product,既没有违背开闭原则,又可以生产多种产品。

参考资料:

https://www.cnblogs.com/xiaolincoding/p/11524376.html

C++工厂模式_c++ 工厂模式-CSDN博客

C++11工厂模式_c++11 工厂模式-CSDN博客

https://www.cnblogs.com/xiaolincoding/p/11524401.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值