设计模式之工厂模式

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

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

#include<iostream>

using namespace std;

typedef enum
{
	Car_Type_Tesla,
	Car_Type_Benz,
	Car_Type_Num
}Car_Type;

void testModel();

//抽象类
class Car
{
public:
	virtual const string& type() = 0;
};
//具体产品类
class Tesla : public Car
{
public:
	Tesla() :Car(), m_strType("Tesla")
	{

	}
	const string& type() override
	{
		cout << m_strType.data() << endl;
		return m_strType;
	}
private:
	string m_strType;
};
//具体产品类
class Benz :public Car
{
public:
	Benz() :Car(), m_strType("Benz")
	{

	}
	const string& type() override
	{
		cout << m_strType.data() << endl;
		return m_strType;
	}
private:
	string m_strType;
};

//简单工厂模式
//创建过程在工厂类中完成
class CarFactory
{
public:
	//根据产品信息创建具体的产品类型的实例,返回一个抽象产品类
	Car* createCar(Car_Type type)
	{
		switch (type)
		{
		case Car_Type_Tesla:
			return new Tesla();
		case Car_Type_Benz:
			return new Benz();
		default:
			return nullptr;
		}
	}
};
void main()
{
	CarFactory* factory = new CarFactory();
	Car* benz = factory->createCar(Car_Type_Benz);
	Car* tesla = factory->createCar(Car_Type_Tesla);

	benz->type();
	tesla->type();

	delete benz;
	benz = nullptr;
	delete tesla;
	tesla = nullptr;
	delete factory;
	factory = nullptr;

}

1.2工厂方法模式
定义一个创建对象的接口,其子类去具体实现这个接口以完成具体的创建工作。如果需要增加新的产品类,只需要扩展一个相应的工厂类即可。

//抽象类
#include<iostream>

using namespace std;

class Car
{
public:
	virtual const string& type() = 0;
};
//具体产品类
class Tesla : public Car
{
public:
	Tesla() :Car(), m_strType("Tesla")
	{

	}
	const string& type() override
	{
		cout << m_strType.data() << endl;
		return m_strType;
	}
private:
	string m_strType;
};
//具体产品类
class Benz :public Car
{
public:
	Benz() :Car(), m_strType("Benz")
	{

	}
	const string& type() override
	{
		cout << m_strType.data() << endl;
		return m_strType;
	}
private:
	string m_strType;
};
class CarFactory
{
public:
	virtual Car* createCar() = 0;
};

//具体的创建工厂类 使用抽象工厂类提供的接口 去创建具体的产品实例
class TeslaFactory : public CarFactory
{
public:
	Car* createCar() override
	{
		return new Tesla();
	}
};

class BenzFactory : public CarFactory
{
public:
	Car* createCar() override
	{
		return new Benz();
	}
};
void main()
{
	BenzFactory* factoryBenz = new BenzFactory();
	Car* benz = factoryBenz->createCar();
	benz->type();
	
	delete benz;
	benz = nullptr;

	delete factoryBenz;
	factoryBenz = nullptr;
}

1.3 抽象工厂模式
抽象工厂模式提供创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
当存在多个产品系列,而客户端只是用一个系列产品时,可以考虑是用抽象工厂模式。
缺点:当增加一个新系列时,不仅需要实现具体的产品类,还需要增加一个新的创建接口,扩展相对困难。

#include<iostream>

using namespace std;

class Coat
{
public:
	virtual const string& color() = 0;
};

class BlackCoat :public Coat
{
public:
	BlackCoat() :Coat(), m_strColor("Black Coat")
	{

	}

	const string& color() override
	{
		cout << m_strColor.data() << endl;
		return m_strColor;
	}
private:
	string m_strColor;
};

class WhiteCoat :public Coat
{
public:
	WhiteCoat() :Coat(), m_strColor("White Coat")
	{

	}

	const string& color() override
	{
		cout << m_strColor.data() << endl;
		return m_strColor;
	}
private:
	string m_strColor;
};


//抽象裤子类
class Pants
{
public:
	virtual const string& color() = 0;
};

class BlackPants :public Pants
{
public:
	BlackPants() :Pants(), m_strColor("Black Pants")
	{

	}
	const string& color() override
	{
		cout << m_strColor.data() << endl;
		return m_strColor;
	}

private:
	string m_strColor;
};
class WhitePants :public Pants
{
public:
	WhitePants() :Pants(), m_strColor("White Pants")
	{

	}
	const string& color() override
	{
		cout << m_strColor.data() << endl;
		return m_strColor;
	}

private:
	string m_strColor;
};

class Factory
{
public:
	//上衣创建接口
	virtual Coat* createCoat() = 0;
	//裤子创建接口
	virtual Pants* createPants() = 0;
};

//创建生产白色系列产品的工厂类
class WhiteFactory :public Factory
{
	Coat* createCoat() override
	{
		return new WhiteCoat();
	}

	Pants* createPants() override
	{
		return new WhitePants();
	}
};
//创建生产黑色系列产品的工厂类
class BlackFactory :public Factory
{
	Coat* createCoat() override
	{
		return new BlackCoat();
	}

	Pants* createPants() override
	{
		return new BlackPants();
	}
};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weiwin_Murphy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值