C++工厂模式

目录

一.简单工厂模式

二.工厂方法模式

三.抽象工厂模式


一.简单工厂模式

假设现在有一家工厂生产苹果,梨两种水果,其中苹果的编号为1梨的编号为2,若某个商家与该工厂合作,生产水果时,不需要说明水果的名字,只需要水果的编号即可,即若代号为1则生产苹果,代号为2则生产梨。


#include<iostream>
#include<string>

class Fruit
{
public:
	Fruit(std::string name):mname(name){}
	virtual void option() = 0;

protected:
	std::string mname;
};
class Apple :public Fruit
{
public:
	Apple(std::string name):Fruit(name){}
	void option()
	{
		std::cout << "this is an " << mname << std::endl;
	}
}; 
class Pear :public Fruit
{
public:
	Pear(std::string name):Fruit(name){}
	void option()
	{
		std::cout << "this is a " << mname << std::endl;
	}
};
class Factory
{
public:
	Factory(){}
	Fruit* createProduct(int flag)
	{
		switch (flag)
		{
		case 1:
			return new Apple("apple");
			break;
		case 2:
			return new Pear("pear");
			break;
		default:
			std::cout << "no this product!" << std::endl;
			break;
		}
	}
};

int main()
{
	Factory f;
	Fruit* fp = f.createProduct(1);
	fp->option();
	return 0;
}

我们看到,只需输入相应的编号,就可以从工厂中生产出相应的对象。但如果现在要再添加一种水果,比如香蕉(代号为3),那么此时我们就要在工厂这个类中新添加关于香蕉的代码,而这违反了开发封闭原则,其中开发原则意思就是旧版本的代码可以供新版本的代码沿用,而封闭原则的意思是新的版本可以用旧的版本的代码,但是不能就该旧版本的代码。

二.工厂方法模式

相对于简单工厂模式的一个工厂生产多种商品而言,工厂方法模式是一个工厂只生产一种商品由工厂编号取代了产品编号,即1号工厂只生产苹果,2号工厂只生产梨,此时就算要新生产一种水果,例如香蕉,也不会影响其他工厂生产其对应的商品。

#if 0
#include<iostream>
#include<string>

class Fruit
{
public:
	Fruit(std::string name) :mname(name) {}
	virtual void option() = 0;

protected:
	std::string mname;
};
class Apple :public Fruit
{
public:
	Apple(std::string name) :Fruit(name) {}
	void option()
	{
		std::cout << "this is an " << mname << std::endl;
	}
};
class Pear :public Fruit
{
public:
	Pear(std::string name) :Fruit(name) {}
	void option()
	{
		std::cout << "this is a " << mname << std::endl;
	}
};
class Factory
{
public:
	Factory(std::string name):mname(name) {}
	virtual Fruit* createProduct() = 0;

protected:
	std::string mname;
};
class F1 :public Factory {
public:
	F1(std::string name):Factory(name){}
	Fruit* createProduct()
	{
		return new Apple("apple");
	}
};
class F2 :public Factory {
public:
	F2(std::string name) :Factory(name) {}
	Fruit* createProduct()
	{
		return new Pear("pear");
	}
};

int main()
{
	Factory* pf = new F1("f1");
	Fruit* pff = pf->createProduct();
	pff->option();

	
	return 0;
}

三.抽象工厂模式

假设现在有工厂A和工厂B,工厂A生产A类型的产品,工厂B生产B类型的产品。而现在又分别有商品1和商品2,商品1分别由A工厂生产的A1产品和B工厂生产的B1产品组成,商品2由工厂A生产的A2产品和工厂B生产的B2产品组成。

#include<iostream>
#include<string>

class A//工厂A
{
public:
	A(std::string name):mname(name){}
	virtual void option() = 0;
protected:
	std::string mname;
};

class B//工厂B
{
public:
	B(std::string name) :mname(name) {}
	virtual void option() = 0;
protected:
	std::string mname;
};

class A1 :public A//工厂A生产的A1
{
public:
	A1(std::string name):A(name){}
	void option()
	{
		std::cout << "A1" << std::endl;
	}
};

class A2 :public A//工厂A生产的A2
{
public:
	A2(std::string name) :A(name) {}
	void option()
	{
		std::cout << "A2" << std::endl;
	}
};

class B1 :public B//工厂B生产的B1
{
public:
	B1(std::string name) :B(name) {}
	void option()
	{
		std::cout << "B1" << std::endl;
	}
};

class B2 :public B//工厂B生产的B2
{
public:
	B2(std::string name) :B(name) {}
	void option()
	{
		std::cout << "B2" << std::endl;
	}
};

class AbstractFactory
{
public:
	AbstractFactory(std::string name) :mname(name){}
	virtual A* createProductA() = 0;
	virtual B* createProductB() = 0;
	virtual void Show() = 0;
protected:
	std::string mname;
};

class ABF1 :public AbstractFactory//商品1
{
public:
	ABF1(std::string name):AbstractFactory(name){}
	A* createProductA()
	{
		return new A1("A1");
	}
	B* createProductB()
	{
		return new B1("B1");
	}
	void Show()
	{
		std::cout << "商品1" << std::endl;
	}
};

class ABF2 :public AbstractFactory//商品2
{
public:
	ABF2(std::string name) :AbstractFactory(name) {}
	A* createProductA()
	{
		return new A2("A2");
	}
	B* createProductB()
	{
		return new B2("B2");
	}
	void Show()
	{
		std::cout << "商品2" << std::endl;
	}
};

int main()
{
	AbstractFactory* paf = new ABF1("ABF1");//AbstractFactory* paf = new ABF2("ABF2");
	A* pa = paf->createProductA();
	B* pb = paf->createProductB();
	paf->Show();
	pa->option();
	pb->option();
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值