C++设计模式之工厂模式

工厂模式学习

学习内容来源

链接:c++设计模式之工厂模式

具体代码

#include<iostream>
using namespace std;

class Product
{
public:
	virtual void show() = 0;
};

class Product_A :public Product
{
public:
	void show()
	{
		cout << "Product_A" << endl;
	}
};

class Product_B :public Product
{
public:
	void show()
	{
		cout << "Product_B" << endl;
	}
};

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

class Factory_A : public Factory
{
public:
	Product* createProduct()
	{
		return new Product_A;
	}
};

class Factory_B :public Factory
{
public:
	Product* createProduct()
	{
		return new Product_B;
	}
};

int main()
{
	Factory* fa = new Factory_A();
	Product* pa = fa->createProduct();
	pa->show();
	system("Pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42333471/article/details/88022490