工厂模式的定义我也说不清,请同学们自己百度一下吧。我在这里用c++描述一下工厂模式的实现。
一个工厂通常包含一些接口类,而工厂就是“生产”这些接口类的具体对象的。工厂本身通常是一个单例模式的类。说的好像不明不白的。看例子吧。
先不建立工厂,这会儿先写一个接口类:
class IAnimal
{
public:
virtual void speak() = 0;
virtual void getLegs() = 0;
// it is important to add virtual destructor in interface class
virtual ~IAnimal(){}
};
接口类很简单,一会儿会用这个接口类生成具体的类。工厂中包含的就是这个接口类。
还是先不忙写工厂,这时再写几个具体的类,继承这个接口:
class Cat : public IAnimal
{
public:
virtual void speak()
{
std::cout << "mow" << std::endl;
}
virtual void getLegs()
{
std::cout << "four legs" << std::endl;
}
static Cat* createSelf()
{
return new Cat;
}
~Cat()
{
// free memory if this class allocate memory from heap
}
};
class Dog : public IAnimal
{
public:
virtual void speak()
{
std::cout << "wang" << std::endl;
}
virtual void getLegs()
{
std::cout << "four legs" << std::endl;
}
static Dog* createSelf()
{
return new Dog;
}
~Dog()
{
// free memory if this class allocate memory from heap
}
};
cat类和dog类实现IAnimal接口。
现在来写工厂类。这是一个单例(singleton类),意味着只有一个工厂实例。
class AnimalFactory
{
public:
// this singleton is thread safe in c++11.
static AnimalFactory instance()
{
static AnimalFactory instance;
return instance;
}
IAnimal* create(string animal)
{
map<string, IAnimal*>::iterator it = factory_map.find(animal);
if(it != factory_map.end())
{
return it->second;
}
return NULL;
}
private:
std::map<string, IAnimal*> factory_map;
void registe(string animal, IAnimal* ia)
{
factory_map[animal] = ia;
}
AnimalFactory()
{
registe("Cat", Cat::createSelf());
registe("dog", Dog::createSelf());
registe("jiji", Jiji::createSelf());
registe("monkey", Monkey::createSelf());
}
};
工厂类中,使用静态方法instance()实现单例,工厂类的构造函数是私有的(只有这样才能单例)。接下来使用create()方法取得接口类的指针,用来指向各式的子类对象,这样工厂就能“创造出”各种动物了(多态的体现啊)。
好了,就这样,已经完成了。在main中试试效果:
int main()
{
IAnimal* an_animal = NULL;
string animal_name;
while(1)
{
cout << "input an animal:" << endl;
cin >> animal_name;
if(animal_name == "Q" || animal_name == "q") break;
an_animal = AnimalFactory::instance().create(animal_name);
if(!an_animal)
{
cout << "the factory can not create" + animal_name + ", you should registe it";
}
else
{
an_animal->speak();
an_animal->getLegs();
}
}
return 0;
}
下面是完整的程序,如果想亲手试试可以直接copy。本人使用mingw4.8编译通过。
#include <map>
#include <iostream>
using namespace std;
class IAnimal
{
public:
virtual void speak() = 0;
virtual void getLegs() = 0;
// it is important to add this virtual destructor
virtual ~IAnimal(){}
};
class Cat : public IAnimal
{
public:
virtual void speak()
{
std::cout << "mow" << std::endl;
}
virtual void getLegs()
{
std::cout << "four legs" << std::endl;
}
static Cat* createSelf()
{
return new Cat;
}
~Cat()
{
// free memory if this class allocate memory from heap
}
};
class Dog : public IAnimal
{
public:
virtual void speak()
{
std::cout << "wang" << std::endl;
}
virtual void getLegs()
{
std::cout << "four legs" << std::endl;
}
static Dog* createSelf()
{
return new Dog;
}
~Dog()
{
// free memory if this class allocate memory from heap
}
};
class Jiji : public IAnimal
{
public:
virtual void speak()
{
std::cout << "gege" << std::endl;
}
virtual void getLegs()
{
std::cout << "two legd" << std::endl;
}
static Jiji* createSelf()
{
return new Jiji;
}
~Jiji()
{
// free memory if this class allocate memory from heap
}
};
class Monkey : public IAnimal
{
public:
virtual void speak()
{
std::cout << "owow" << std::endl;
}
virtual void getLegs()
{
std::cout << "two legs" << std::endl;
}
static Monkey* createSelf()
{
return new Monkey;
}
~Monkey()
{
// free memory if this class allocate memory from heap
}
};
class AnimalFactory
{
public:
// this singleton is thread safe in c++11.
static AnimalFactory instance()
{
static AnimalFactory instance;
return instance;
}
IAnimal* create(string animal)
{
map<string, IAnimal*>::iterator it = factory_map.find(animal);
if(it != factory_map.end())
{
return it->second;
}
return NULL;
}
private:
std::map<string, IAnimal*> factory_map;
void registe(string animal, IAnimal* ia)
{
factory_map[animal] = ia;
}
AnimalFactory()
{
registe("Cat", Cat::createSelf());
registe("dog", Dog::createSelf());
registe("jiji", Jiji::createSelf());
registe("monkey", Monkey::createSelf());
}
};
int main()
{
IAnimal* an_animal = NULL;
string animal_name;
while(1)
{
cout << "input an animal:" << endl;
cin >> animal_name;
if(animal_name == "Q" || animal_name == "q") break;
an_animal = AnimalFactory::instance().create(animal_name);
if(!an_animal)
{
cout << "the factory can not create" + animal_name + ", you should registe it";
}
else
{
an_animal->speak();
an_animal->getLegs();
}
}
return 0;
}