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();
}
};