Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

工厂模式(Factory Pattern)笔记 #86

Open
Wang-Shengyuan opened this issue Jun 15, 2022 · 1 comment
Open

工厂模式(Factory Pattern)笔记 #86

Wang-Shengyuan opened this issue Jun 15, 2022 · 1 comment

Comments

@Wang-Shengyuan
Copy link

Wang-Shengyuan commented Jun 15, 2022

除了课上所讲的结构型模式和行为型模式外,OOP的设计模式还有创建型模式、并发型模式和线程池模式等。工厂模式属于创建型模式,分为三类:简单工厂模式、工厂方法模式、抽象工厂模式

!(https://img-blog.csdn.net/20140304233530984)

简单工厂
具体情形:有一个肥皂厂,生产各种肥皂,有舒肤佳,夏士莲,娜爱斯等。

#include <iostream>
using namespace std;
enum PRODUCTTYPE {SFJ,XSL,NAS};
class soapBase
{
	public:
	virtual ~soapBase(){};
	virtual void show() = 0;
};
 
class SFJSoap:public soapBase
{
	public:
	void show() {cout<<"SFJ Soap!"<<endl;}
};
 
class XSLSoap:public soapBase
{
	public:
	void show() {cout<<"XSL Soap!"<<endl;}
};
 
class NASSoap:public soapBase
{
	public:
	void show() {cout<<"NAS Soap!"<<endl;}
};
 
class Factory
{
	public:
	soapBase * creatSoap(PRODUCTTYPE type)
	{
		switch(type)
		{
			case SFJ: 
				return new SFJSoap();
				break;
			case XSL:
				return new XSLSoap();
				break;
			case NAS:
				return new NASSoap();
				break;
			default:break;
		}
		
	}
};
 
int main()
{
	Factory factory;
	soapBase* pSoap1 = factory.creatSoap(SFJ);
	pSoap1->show();
	soapBase* pSoap2 = factory.creatSoap(XSL);
	pSoap2->show();
	soapBase* pSoap3 = factory.creatSoap(NAS);
	pSoap3->show();
	delete pSoap1;
	delete pSoap2;
	delete pSoap3;
	return 0;
}

2

工厂方法模式
具体情形:独立每个生产线,每个生产线只生产一种肥皂。

#include <iostream>
using namespace std;
enum SOAPTYPE {SFJ,XSL,NAS};
 
class soapBase
{
	public:
	virtual ~soapBase(){};
	virtual void show() = 0;
};
 
class SFJSoap:public soapBase
{
	public:
	void show() {cout<<"SFJ Soap!"<<endl;}
};
 
class XSLSoap:public soapBase
{
	public:
	void show() {cout<<"XSL Soap!"<<endl;}
};
 
class NASSoap:public soapBase
{
	public:
	void show() {cout<<"NAS Soap!"<<endl;}
};
 
class FactoryBase
{
	public:
	virtual soapBase * creatSoap() = 0;
};
 
class SFJFactory:public FactoryBase
{
	public:
	soapBase * creatSoap()
	{
		return new SFJSoap();
	}
};
 
class XSLFactory:public FactoryBase
{
	public:
	soapBase * creatSoap()
	{
		return new XSLSoap();
	}
};
 
class NASFactory:public FactoryBase
{
	public:
	soapBase * creatSoap()
	{
		return new NASSoap();
	}
};
 
 
 
int main()
{
	SFJFactory factory1;
	soapBase* pSoap1 = factory1.creatSoap();
	pSoap1->show();
	XSLFactory factory2;
	soapBase* pSoap2 = factory2.creatSoap();
	pSoap2->show();
	NASFactory factory3;
	soapBase* pSoap3 = factory3.creatSoap();
	pSoap3->show();
	delete pSoap1;
	delete pSoap2;
	delete pSoap3;
	return 0;
}

抽象工厂模式
具体情形:这个肥皂厂发现搞牙膏也很赚钱,决定做牙膏。牙膏有高档低档,肥皂也是。现在搞两个厂房,一个生产低档的牙膏和肥皂,一个生产高档的牙膏和肥皂。比如,厂房一生产中华牙膏、娜爱斯肥皂,厂房二生产黑人牙膏和舒肤佳牙膏

3

#include <iostream>
using namespace std;
enum SOAPTYPE {SFJ,XSL,NAS};
enum TOOTHTYPE {HR,ZH};
 
class SoapBase
{
	public:
	virtual ~SoapBase(){};
	virtual void show() = 0;
};
 
class SFJSoap:public SoapBase
{
	public:
	void show() {cout<<"SFJ Soap!"<<endl;}
};
 
class NASSoap:public SoapBase
{
	public:
	void show() {cout<<"NAS Soap!"<<endl;}
};
 
class ToothBase
{
	public:
	virtual ~ToothBase(){};
	virtual void show() = 0;
};
 
class HRTooth:public ToothBase
{
	public:
	void show() {cout<<"Hei ren Toothpaste!"<<endl;}
};
 
class ChinaTooth:public ToothBase
{
	public:
	void show() {cout<<"China Toothpaste!"<<endl;}
};
 
class FactoryBase
{
	public:
	virtual SoapBase * creatSoap() = 0;
	virtual ToothBase * creatToothpaste() = 0;
};
 
class FactoryA :public FactoryBase
{
	public:
	SoapBase * creatSoap()
	{
		return new SFJSoap();
	}
	
	ToothBase * creatToothpaste()
	{
		return new HRTooth();
	}
};
 
class FactoryB :public FactoryBase
{
	public:
	SoapBase * creatSoap()
	{
		return new NASSoap();
	}
	
	ToothBase * creatToothpaste()
	{
		return new ChinaTooth();
	}
};
 
 
int main()
{
	FactoryA factory1;
	FactoryB factory2;
	SoapBase *pSoap1 = NULL;
	ToothBase *pToothpaste1 = NULL;
	pSoap1 = factory1.creatSoap();
	pToothpaste1 = factory1.creatToothpaste();
	pSoap1->show();
	pToothpaste1->show();
	
	SoapBase *pSoap2 = NULL;
	ToothBase *pToothpaste2 = NULL;
	pSoap2 = factory2.creatSoap();
	pToothpaste2 = factory2.creatToothpaste();
	pSoap2->show();
	pToothpaste2->show();
	
	delete pSoap1;
	delete pSoap2;
	delete pToothpaste1;
	delete pToothpaste2;
	
	return 0;
}

工厂模式的组成
1)抽象工厂角色:这是工厂方法模式的核心,它与应用程序无关。是具体工厂角色必须实现的接口或者必须继承的父类。
2)具体工厂角色:它含有和具体业务逻辑有关的代码。由应用程序调用以创建对应的具体产品的对象。
3)抽象产品角色:它是具体产品继承的父类或者是实现的接口。
4)具体产品角色:具体工厂角色所创建的对象就是此角色的实例。

适用场合

在程序中,需要创建的对象很多,导致对象的new操作多且杂时,需要使用简单工厂模式;
由于对象的创建过程是我们不需要去关心的,而我们注重的是对象的实际操作,所以,我们需要分离对象的创建和操作两部分,如此,方便后期的程序扩展和维护。

@Wang-Shengyuan
Copy link
Author

第一幅UML图上传疑有问题,可点击链接查看

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant