<!--完整代码下载链接:点击下载完整代码示例-->
CBase* pBase(NULL); switch (type_variable) { case obj1: pBase = new CBaseDerivate1();break; case obj2: pBase = new CBaseDerivate2();break; ... case objN: pBase = new CBaseDerivateN();break; }
CBase* pBase(NULL); if (!strcmp(string_for_type, "Type1")) pBase = new CBaseDerivate1(); else if (!strcmp(string_for_type, "Type2")) pBase = new CBaseDerivate2(); ... else if (!strcmp(string_for_type, "TypeN")) pBase = new CBaseDerivateN();上面两中创建对象的方法使用起来很繁琐,并且当对象的数量增加时,维护起来很麻烦,下面介绍简单工厂模式来解决该问,采用两种方法实现,一种是采用注册函数指针,另外一种采用模板参数。
class IAnimal { public: virtual int GetNumberOfLegs() const = 0; virtual void Speak() = 0; virtual void Free() = 0; };2.定义具体的动物实例
// IAnimal implementations class Cat : public IAnimal { public: int GetNumberOfLegs() const { return 4; } void Speak() { cout << “Meow” << endl; } void Free() { delete this; } static IAnimal * __stdcall Create() { return new Cat(); } }; class Dog : public IAnimal { public: int GetNumberOfLegs() const { return 4; } void Speak() { cout << “Woof” << endl; } void Free() { delete this; } static IAnimal * __stdcall Create() { return new Dog(); } }; class Spider : public IAnimal // Yeah it isn’t really an animal… { public: int GetNumberOfLegs() const { return 8; } void Speak() { cout << endl; } void Free() { delete this; } static IAnimal * __stdcall Create() { return new Spider(); } }; class Horse : public IAnimal { public: int GetNumberOfLegs() const { return 4; } void Speak() { cout << “A horse is a horse, of course, of course.” << endl; } void Free() { delete this; } static IAnimal * __stdcall Create() { return new Horse(); } };
typedef IAnimal* (__stdcall *CreateAnimalFn)(void);
// Factory for creating instances of IAnimal class AnimalFactory { private: AnimalFactory(); AnimalFactory(const AnimalFactory &) { } AnimalFactory &operator=(const AnimalFactory &) { return *this; } typedef map FactoryMap; FactoryMap m_FactoryMap; public: ~AnimalFactory() { m_FactoryMap.clear(); } static AnimalFactory *Get() { static AnimalFactory instance; return &instance; } void Register(const string &animalName, CreateAnimalFn pfnCreate); IAnimal *CreateAnimal(const string &animalName); };构造函数进行具体创建函数的注册
/* Animal factory constructor. Register the types of animals here. */ AnimalFactory::AnimalFactory() { Register(“Horse”, &Horse::Create); Register(“Cat”, &Cat::Create); Register(“Dog”, &Dog::Create); Register(“Spider”, &Spider::Create); }
void AnimalFactory::Register(const string &animalName, CreateAnimalFn pfnCreate) { m_FactoryMap[animalName] = pfnCreate; }
IAnimal *AnimalFactory::CreateAnimal(const string &animalName) { FactoryMap::iterator it = m_FactoryMap.find(animalName); if( it != m_FactoryMap.end() ) return it->second(); return NULL; }
int main( int argc, char **argv ) { IAnimal *pAnimal = NULL; string animalName; while( pAnimal == NULL ) { cout << “Type the name of an animal or ‘q’ to quit: “; cin >> animalName; if( animalName == “q” ) break; IAnimal *pAnimal = AnimalFactory::Get()->CreateAnimal(animalName); if( pAnimal ) { cout << “Your animal has ” << pAnimal->GetNumberOfLegs() << ” legs.” << endl; cout << “Your animal says: “; pAnimal->Speak(); } else { cout << “That animal doesn’t exist in the farm! Choose another!” << endl; } if( pAnimal ) pAnimal->Free(); pAnimal = NULL; animalName.clear(); } return 0; }
template <class BT> class FactoryPlant { public: FactoryPlant() {} virtual ~FactoryPlant() {} virtual BT *createInstance() = 0; }; template <class BT,class ST> class Factory : public FactoryPlant<BT> { public: Factory() {} virtual ~Factory() {} virtual BT *createInstance() {return new ST;} };
FactoryPlant
包含创建实例的纯虚函数createInstance,子类Factory实现该方法。
2.创建具体子类
//抽象动物接口
class IAnimal { public: virtual int GetNumberOfLegs() const = 0; virtual void Speak() = 0; virtual void Free() = 0; typedef FactoryPlant<IAnimal> SimpleBaseClassFactory; };
class Cat : public IAnimal { public: int GetNumberOfLegs() const { return 4; } void Speak() { cout << "Meow" << endl; } void Free() { delete this; } static Factory<IAnimal,Cat> myFactory; }; Factory<IAnimal,Cat> Cat::myFactory; class Dog : public IAnimal { public: int GetNumberOfLegs() const { return 4; } void Speak() { cout << "Woof" << endl; } void Free() { delete this; } static Factory<IAnimal,Dog> myFactory; }; Factory<IAnimal,Dog> Dog::myFactory; class Spider : public IAnimal // { public: int GetNumberOfLegs() const { return 8; } void Speak() { } void Free() { delete this; } static Factory<IAnimal,Spider> myFactory; }; Factory<IAnimal,Spider> Spider::myFactory; class Horse : public IAnimal { public: int GetNumberOfLegs() const { return 4; } void Speak() { cout << "A horse is a horse, of course, of course." << endl; } void Free() { delete this; } static Factory<IAnimal,Horse> myFactory; }; Factory<IAnimal,Horse> Horse::myFactory;
IAnimal *CreateAnimal(std::map<string,IAnimal::SimpleBaseClassFactory *> &factories,const string &animalName) { return factories[animalName]->createInstance(); }4.客户端使用
void main() { IAnimal *pAnimal = NULL; //Register std::map<string,IAnimal::SimpleBaseClassFactory *> factories; factories["Cat"] = &Cat::myFactory; factories["Dog"] = &Dog::myFactory; factories["Spider"] = &Spider::myFactory; factories["Horse"] = &Horse::myFactory; string animalName; while( pAnimal == NULL ) { cout << "Type the name of an animal or 'q' to quit: "; cin >> animalName; if( animalName == "q" ) break; IAnimal *pAnimal = CreateAnimal(factories,animalName); if( pAnimal ) { cout << "Your animal has " << pAnimal->GetNumberOfLegs() << " legs." << endl; cout << "Your animal says: "; pAnimal->Speak(); } else { cout << "That animal doesn't exist in the farm! Choose another!" << endl; } if( pAnimal ) pAnimal->Free(); pAnimal = NULL; animalName.clear(); }
原文地址:http://blog.csdn.net/xiaoding133/article/details/44995145