设计模式——工厂模式

本文深入解析了工厂模式的三种类型:简单工厂模式、工厂方法模式和抽象工厂模式,详细阐述了每种模式的特点、应用场景和优缺点,通过示例代码帮助读者理解工厂模式的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过一个共同的接口来执行新创建的对象。工厂模式作为一种创建模式,一般在用在创建复杂对象,在创建简单对象时,还是建议直接使用new完成一个实例对象的创建。

1.简单工厂模式

特点:需要在工厂类中做判断,从而创造出相应的产品,当增加新产品时,需要修改工厂类。使用简单工厂模式,我们只需要知道具体的产品型号就可以创建一个产品。

缺点:工厂类中集中了所有产品类的创建逻辑,如果产品量较大,会使工厂类变得非常臃肿。

#include<iostream>
using namespace std;
 typedef enum
 {
	 Product_Type_A,
	 Product_Type_B,
	 Product_Type_Num
 }Product_Type;
 class Product
 {
 public:
	 virtual const string & type()=0;
 };
 class ProductA:public Product
 {
 public:
	 ProductA():Product() ,strType("ProductA")
	 {
	 }
	 const string & type() 
	 {
		 cout<<strType.data()<<endl;
		return strType;
	 }
 private:
	 string strType;
 };
 class ProductB:public Product
 {
 public:
	 ProductB():Product(),strType("ProductB")
	 {
	 }
	 const string & type() 
	 {
		 cout<<strType.data()<<endl;
		 return strType;
	 }
 private:
	 string strType;
 };
class Factory
{
public:
	Product* createProduct(Product_Type type)
	{
		switch(type)
		{
		case Product_Type_A:
			return new ProductA();
		case Product_Type_B:
			return new ProductB();
		default:
			return nullptr;
		}
	}
};

int main()
{
	Factory *factory=new Factory();
	Product* ProductA=factory->createProduct(Product_Type_A);
	ProductA->type();
	Product* ProductB=factory->createProduct(Product_Type_B);
	ProductB->type();

	delete ProductB;
	ProductB=nullptr;
	delete ProductA;
	ProductA=nullptr;
	factory=nullptr;

	return 0;
}

2.工厂方法模式

功能:定义创建对象的接口,封装了对象的创建;使得具体化的工作延迟到子类中。

该类在这里类似于一个真正意义上的工厂(生产对象)。我们需要提供一个对象创建对象的接口,并在子类中提供具体实现,因为只有在子类中可以决定到底实例化哪一个类。

主要解决的问题:接口选择的问题

什么情况下使用:我们明确的计划不同条件下创建不同实例时

怎么解决:让子类实现工厂接口,返回的是一个抽象的产品

缺点:没增加一个产品,就要增加一个对象工厂,产品数据较多时,需要实现大量的工厂类,增加了代码量。

#include<iostream>
using namespace std;

 class Product
 {
 public:
	 virtual const string & type()=0;
 };
 class ProductA:public Product
 {
 public:
	 ProductA():Product() ,strType("ProductA")
	 {
	 }
	 const string & type() 
	 {
		 cout<<strType.data()<<endl;
		return strType;
	 }
 private:
	 string strType;
 };
 class ProductB:public Product
 {
 public:
	 ProductB():Product(),strType("ProductB")
	 {
	 }
	 const string & type() 
	 {
		 cout<<strType.data()<<endl;
		 return strType;
	 }
 private:
	 string strType;
 };
class Factory //抽象工厂类,提供创建接口
{
public:
	virtual Product* createProduct()=0;
	//提供创建产品实例的接口,返回抽象产品类
};

//创建具体的工厂类,使用抽象工厂类提供的接口,去创建具体的产品实例
class ProductAFactory:public Factory
{
public:
	Product* createProduct() 
	{
		return new ProductA();
	}
};
class ProductBFactory:public Factory
{
public:
	Product* createProduct() 
	{
		return new ProductB();
	}
};
int main()
{
	Factory *factoryA=new ProductAFactory();
	Product* ProductA=factoryA->createProduct();
	ProductA->type();

	Factory *factoryB=new ProductBFactory();
	Product* ProductB=factoryB->createProduct();
	ProductB->type();

	delete ProductB;
	ProductB=nullptr;
	delete factoryB;
	factoryB=nullptr;

	delete ProductA;
	ProductA=nullptr;
	delete factoryA;
	factoryA=nullptr;

	return 0;
}

3.抽象工厂模式

功能:创建一组相关或者相互依赖的对象,抽象工厂模式的关键是将这一组对象的创建封装到一个用于创建对象的类中,维护这样一个创建类总比维护n多相关对象的创建过程要容易的多。

主要解决的问题:接口选择的问题

什么情况下使用:系统的产品有多于一个的产品族,而系统只消费其中某一族的产品。

怎么解决:在一个产品族里,定义多个产品

缺点:产品族扩展起来非常困难,增加一个系列的某一产品,既要在抽象的create中加代码,又要在具体的里面加代码

#include<iostream>
using namespace std;
class Cap  //创建一个抽象帽子类
{
public:
	virtual const string & color()=0;
};
class BlackCap:public Cap 
{
public:
	BlackCap():Cap(),strcolor("Black cap")
	{
	}
	const string& color() 
	{
		cout<<strcolor.data()<<endl;
		return strcolor;
	}
private:
	string strcolor;
};
class WhiteCap:public Cap
{
public:
	WhiteCap():Cap(),strcolor("White cap")
	{
	}
	const string& color() 
	{
		cout<<strcolor.data()<<endl;
		return strcolor;
	}
private:
	string strcolor;
};
class Coat  //创建一个抽象大衣类
{
public:
	virtual const string & color()=0;
};
class BlackCoat:public Coat
{
public:
	BlackCoat():Coat(),strcolor("Black coat")
	{
	}
	const string& color() 
	{
		cout<<strcolor.data()<<endl;
		return strcolor;
	}
private:
	string strcolor;
};
class WhiteCoat:public Coat
{
public:
	WhiteCoat():Coat(),strcolor("White coat")
	{
	}
	const string& color() 
	{
		cout<<strcolor.data()<<endl;
		return strcolor;
	}
private:
	string strcolor;
};
class Factory  //创建抽象工厂类,提供产品创建接口
{
public:
	virtual Cap* createCap()=0;  //帽子创建接口,返回抽象帽子类
	virtual Coat* createCoat()=0;  //上衣创建接口,返回抽象上衣类
};
class WhiteFactory:public Factory  //创建白色产品的工厂类,具体创建白色帽子和上衣的接口
{
public:
	Cap* createCap() 
	{
		return new WhiteCap(); 
	}
	Coat* createCoat() 
	{
		return new WhiteCoat();
	}
};
class BlackFactory:public Factory  //创建黑色产品的工厂类,具体创建黑色帽子和上衣的接口
{
public:
	Cap* createCap() 
	{
		return new BlackCap();
	}
	Coat* createCoat()
	{
		return new WhiteCoat();
	}
};
int main()
{
	Factory *factoryWhite=new WhiteFactory();
	 Cap*  WhiteCap=factoryWhite->createCap();
	WhiteCap->color();
	Coat* WhiteCoat=factoryWhite->createCoat();
	WhiteCoat->color();
	Factory *factoryBlack=new BlackFactory();
	Cap* BlackCap=factoryBlack->createCap();
	BlackCap->color();
	Coat* BlackCoat=factoryBlack->createCoat();
	BlackCoat->color();

	delete factoryWhite;
	factoryWhite=nullptr;
	delete WhiteCap;
	WhiteCap=nullptr;
	delete WhiteCoat;
	WhiteCoat=nullptr;

	delete factoryBlack;
	factoryBlack=nullptr;
	delete BlackCap;
	BlackCap=nullptr;
	delete BlackCoat;
	BlackCoat=nullptr;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值