工厂模式
本文主要时学习时参考和自己理解,便于以后的复习和使用,按照自己的习惯进行重新编写,原文介绍的更加详细;
所谓工厂便是用于生产,在工厂模式中便是用于一个或者多个判断条件用于产生对象的方法,采用工厂模式可以创建多个基于一个父类的子类对象,返回父类对象用于使用;
工厂模式的实现方式可分别简单工厂模式、工厂方法模式、抽象工厂模式;
简单工厂模式实现:
简单工厂模式组成:
工厂类:固定接口,根据条件产生子类指针返回父类指针
产品的公用父类:所有产品需要集成该父类
产品的具体类(具体类需要集成自公用父类):产品的具体实现
产品父类:
头文件:
class ProductFather
{
public:
ProductFather();
~ProductFather();
virtual void ShowProductThings()=0;
};
CPP文件:
ProductFather::ProductFather()
{
}
ProductFather::~ProductFather()
{
}
产品类1:
头文件:
class ProductChild361 :
public ProductFather
{
public:
ProductChild361();
virtual ~ProductChild361();
virtual void ShowProductThings();
};
Cpp文件:
ProductChild361::ProductChild361()
{
}
ProductChild361::~ProductChild361()
{
}
void ProductChild361::ShowProductThings()
{
std::cout << "361" << std::endl;
}
产品类2:
class ProductChildNB :
public ProductFather
{
public:
ProductChildNB();
~ProductChildNB();
virtual void ShowProductThings();
};
ProductChildNB::ProductChildNB()
{
}
ProductChildNB::~ProductChildNB()
{
}
void ProductChildNB::ShowProductThings()
{
std::cout << "NB" << std::endl;
}
工厂类:
class FactorClass
{
public:
FactorClass();
~FactorClass();
static ProductFather * CreateObjectPtr(int ObjectType);
};
FactorClass::FactorClass()
{
}
FactorClass::~FactorClass()
{
}
ProductFather * FactorClass::CreateObjectPtr(int ObjectType)
{
if (1 == ObjectType)
{
return new ProductChild361();
}
else if (2 == ObjectType)
{
return new ProductChildNB();
}
}
简单工厂模式可以很清楚的看得出:通过createobjec产生对象然后使用,但缺点也很明显没增加一个产品,便需要修改工厂的createobject接口;
抽象工厂模式实现
工厂模式或者抽象工厂模式与简单工厂有些区别:工厂模式需要将工厂区分的更加详细,每一个品牌需要对应一个专门的工厂进行生产,在对应的工厂中进行创建该品牌下的所有产品;感觉抽象工厂与工厂模式在理解上面没有太大的不同
工厂的组成:
工厂基类:工厂的所有基类,定义工厂的所有虚接口;
工厂类:完善该工厂所需的接口和产生对应的产品对象
产品的公用父类:所有产品需要集成该父类
产品的具体类(具体类需要集成自公用父类):产品的具体实现
工厂抽象类:
#include "ProductFather.h"
class FactorClassFather
{
public:
FactorClassFather();
~FactorClassFather();
virtual ProductFather* CreateObjectPtr(int ObjectType) = 0;
};
工厂具体实现类:
class Factor361 :
public FactorClassFather
{
public:
Factor361();
virtual ~Factor361();
ProductFather * CreateObjectPtr(int ObjectType);
};
ProductFather * Factor361::CreateObjectPtr(int ObjectType)
{
if (1 == ObjectType)
{
return new ProductChild361Shoes();
}
else if (2 == ObjectType)
{
return new ProductChild361YF();
}
}
class FactorNB :
public FactorClassFather
{
public:
FactorNB();
~FactorNB();
ProductFather* CreateObjectPtr(int ObjectType);
};
ProductFather* FactorNB::CreateObjectPtr(int ObjectType)
{
if (1 == ObjectType)
{
return new ProductChildNBShoes();
}
else if (2 == ObjectType)
{
return new ProductChildNBYF();
}
}
产品基类:
class ProductFather
{
public:
ProductFather();
~ProductFather();
virtual void ShowProductThings()=0;
};
class ProductChild361 :
public ProductFather
{
public:
ProductChild361();
virtual ~ProductChild361();
virtual void ShowProductThings();
};
Cpp文件:
ProductChild361::ProductChild361()
{
}
ProductChild361::~ProductChild361()
{
}
void ProductChild361::ShowProductThings()
{
std::cout << "361" << std::endl;
}
class ProductChildNB :
public ProductFather
{
public:
ProductChildNB();
~ProductChildNB();
virtual void ShowProductThings();
};
ProductChildNB::ProductChildNB()
{
}
ProductChildNB::~ProductChildNB()
{
}
void ProductChildNB::ShowProductThings()
{
std::cout << "NB" << std::endl;
}
产品类实现:
class ProductChild361YF :
public ProductChild361
{
public:
ProductChild361YF();
~ProductChild361YF();
void ShowProductThings()
{
std::cout << "361衣服" << std::endl;
}
};
class ProductChild361Shoes :
public ProductChild361
{
public:
ProductChild361Shoes();
virtual ~ProductChild361Shoes();
void ShowProductThings()
{
std::cout << "361鞋子" << std::endl;
}
};
class ProductChildNBYF :
public ProductChildNB
{
public:
ProductChildNBYF();
virtual ~ProductChildNBYF();
void ShowProductThings()
{
std::cout << "NB衣服" << std::endl;
}
};
class ProductChildNBShoes :
public ProductChildNB
{
public:
ProductChildNBShoes();
virtual ~ProductChildNBShoes();
void ShowProductThings()
{
std::cout << "NB鞋子" << std::endl;
}
};
模板工厂类
上述的工厂代码可以看出,每当产生一个新的产品类型,都需要重新编写相应的工厂类,这样会造成持续修改,在看了原文之后也看到了为了减少代码而写的模板工厂方法按照个人习惯做了某些修改:
工厂类:
template <class T_FatherClass>
class FactorClassTemplate
{
public:
FactorClassTemplate(){};
~FactorClassTemplate(){};
template <class T_ClildClass>
static T_FatherClass * CreateObjectPtr(int ObjectType)
{
return new T_ClildClass();
}
};
typedef FactorClassTemplate<ProductFather> ProductFatherFactor;
调用:
ProductFather * Object1 = ProductFatherFactor::CreateObjectPtr<ProductChild361>(1);
Object1->ShowProductThings();
delete Object1;
ProductFather * Object2 = ProductFatherFactor::CreateObjectPtr<ProductChildNB>(1);
Object2->ShowProductThings();
delete Object2;
在最开始使用typedef重命名一下,然后在使用时可以减少一些创建的定义,当然也可以按照自己的要求进行整改;
注册式工厂:
工厂类父类:
#include <iostream>
#include "ProductFather.h"
class FactorFather
{
public:
FactorFather();
virtual ~FactorFather();
virtual ProductFather * CreateObjectPtr() = 0;
};
工厂类:
template <class T_FatherClass, class T_ChildenClass>
class FactorClass :
public FactorFather
{
public:
FactorClass(){};
~FactorClass(){};
//T_FatherClass* CreateObjectPtr()
//{
// return new T_ChildenClass();
//}
ProductFather * CreateObjectPtr()
{
return new T_ChildenClass();
};
};
数据注册类:
class RegisterInstance
{
public:
RegisterInstance();
~RegisterInstance();
static RegisterInstance * Instance()
{
static RegisterInstance Ins;
return &Ins;
}
template <class T_ClassFather, class T_ChildClass>
void SetMapInstance(std::string TypeName)
{
FactorFather * father = new FactorClass<T_ClassFather, T_ChildClass>();
_MapInstance[TypeName]= father;
}
FactorFather* GetMapInstance(std::string _MessageType)
{
auto it = _MapInstance.find(_MessageType);
if (it != _MapInstance.end())
{
return it->second;
}
else
{
return NULL;
}
}
std::map<std::string, FactorFather *> _MapInstance;
};
产品类实现:
class ProductFather
{
public:
ProductFather();
~ProductFather();
virtual void ShowProductThings()=0;
};
class ProductChild361 :
public ProductFather
{
public:
ProductChild361();
virtual ~ProductChild361();
virtual void ShowProductThings()
{
std::cout << "361" << std::endl;
};
};
class ProductChild361YF :
public ProductChild361
{
public:
ProductChild361YF();
~ProductChild361YF();
void ShowProductThings()
{
std::cout << "361衣服" << std::endl;
}
};
调用方式
RegisterInstance::Instance()->SetMapInstance<ProductFather, ProductChild361YF>("361");
FactorFather * _FactorFather = static_cast<FactorFather *>(RegisterInstance::Instance()->GetMapInstance("361"));
if (_FactorFather != NULL)
{
ProductFather *n = _FactorFather->CreateObjectPtr();
if (n == NULL)
{
int i = 0;
}
else
{
ProductChild361 * Pro = static_cast<ProductChild361 *>(n);
Pro->ShowProductThings();
}
}
这里采用了单例和模板工厂结合的方式,每一个注册的结构体都会生成一个对应的工厂,而注册表则是单例类型,保证注册都会存到一个结构体里面,通过Createobjec获取工厂生产的对象指针,在使用时注意析构便可;