设计模式(c++) 工厂模式

(1)简单工厂模式
#include <iostream>
using namespace std;
//基类
class Product
{
public :
    virtual void Do() = 0; //纯虚函数
};
/*纯虚函数在基类中,调函数去子类中去调*/

//子类A
class ProductA : public Product
{
public :
    void Do()
    {
        cout << "生产A" << endl;
    }
};
//子类B
class ProductB : public Product
{
public :
    void Do()
    {
        cout << "生产B" << endl;
    }
};
//子类C
class ProductC : public Product
{
public :
    void Do()
    {
        cout << "生产C" << endl;
    }
};
//工厂类
class Factory
{
private:
    Product *product;
public :
    Factory(int n)
    {
        switch(n)
        {
        case 1:
            product = new ProductA();
            break;
        case 2:
            product = new ProductB();
            break;
        case 3:
            product = new ProductC();
        }
    }
    void send()
    {
        product->Do();
    }
};
//客户端
int main()
{
    Factory *factory = new Factory(2);//构造
    factory->send();

    return 0;
}


(2)工厂模式
#include <iostream>
using namespace std;
//基类
class Product
{
public :
    virtual void Do() = 0; //纯虚函数
};
/*纯虚函数在基类中,调函数去子类中去调*/

//子类A
class ProductA : public Product
{
public :
    void Do()
    {
        cout << "生产A" << endl;
    }
};
//子类B
class ProductB : public Product
{
public :
    void Do()
    {
        cout << "生产B" << endl;
    }
};
//子类C
class ProductC : public Product
{
public :
    void Do()
    {
        cout << "生产C" << endl;
    }
};
//工厂类
class Factory
{
private:
    Product *product;
public :
    Factory(Product *t)
    {
        product = t;
    }
    void send()
    {
        product->Do();
    }
};
//客户端
//让客户端来new,把工厂的构造函数进行了改动
int main()
{
    Factory *factory = new Factory(new ProductB());//构造
    factory->send();

    return 0;
}


(3)抽象工厂模式
#include <iostream>
using namespace std;
//基类
class Product
{
public :
    virtual void Do() = 0; //纯虚函数
};
/*纯虚函数在基类中,调函数去子类中去调*/

//子类A
class ProductA : public Product
{
public :
    void Do()
    {
        cout << "生产A" << endl;
    }
};
//子类B
class ProductB : public Product
{
public :
    void Do()
    {
        cout << "生产B" << endl;
    }
};
//子类C
class ProductC : public Product
{
public :
    void Do()
    {
        cout << "生产C" << endl;
    }
};
//工厂类
class Factory
{
protected:
    Product *product;
public :
    virtual void send() = 0;
};
//子类工厂A
class FactoryA : public Factory
{
public :
    FactoryA(Product *t)
    {
        product = t;
    }
    void send()
    {
        product->Do();
    }
};
//子类工厂B
class FactoryB : public Factory
{
public :
    FactoryB(Product *t)
    {
        product = t;
    }
    void send()
    {
        product->Do();
    }
};
//子类工厂C
class FactoryC : public Factory
{
public :
    FactoryC(Product *t)
    {
        product = t;
    }
    void send()
    {
        product->Do();
    }
};
//客户端
int main()
{
    Factory *factory = new FactoryA(new ProductA());
    factory->send();

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值