设计模式之C++实现 - 工厂模式
1 工厂
现实中的工厂是用来生产实际物体的,类似的,面向对象程序开发中的工厂是产生对象的。在开发中使用工厂时,只要写出要求工厂产生一个对象实例的代码即可,而无需直接构造对象实例。例如,在一个室内装饰软件中或许有个FurnitureFactory对象,当代码需要产生一些桌子时,可以通过调用FurnitureFactory中的createTable()方法来产生一些桌子。
乍眼一看,工厂好像只会带来开发的复杂性而没有明显的好处,但它可以在大批量的产生对象时有用。(--规模化生产?)另外一个好处是在构建对象时无需关心对象对应的确切的类。下面是一个例子。
在真实世界中,当说到开车时,不会具体的指是哪种车,可以是宝马或者吉利,只要能开就行。现在假设你要辆车且经济上可以负担,通常是工厂有什么车生产出来,你就可以买到哪种车。在程序开发中也类似,首先需要一个可以开的车,一个多义性的车。你可以写一个抽象类Car,里面有drive()方法。那么,宝马或者吉利就是它的子类。当然,如果需要直到最后要到的车是宝马还是吉利需要在创建car对象的时候就明确。
如果是采用car 工厂的办法,,CarFactory的超类会定义个buildCar()虚拟的方法。BMWFactory和GeliFactory的子类会重载buildCar方法来创建宝马和吉利。
采用工厂的方法可以抽象对象产生的过程,你可以很容易的替换不同的工厂。就像利用多义性创建对象一样,同样可以利用多义性创建工厂。这样做可以很容易的扩展程序--只需更换工厂实例。
工厂的实现
首先需要实现车的类
/**
* Car.h
*
*/
#include <iostream>
class Car
{
public:
virtual void info() = 0;
};
class Geli : public Car
{
public:
virtual void info() { std::cout << "Geli" << std::endl; }
};
class BMW: public Car
{
public:
virtual void info() { std::cout << "BMW" << std::endl; }
};
接下来实现CarFactory的类,其中子类重载了createCar()以返回特定的车
/**
* CarFactory.h
*/
#include "Car.h"
class CarFactory
{
public:
CarFactory();
Car* requestCar();
int getNumCarsInProduction() const;
protected:
virtual Car* createCar() = 0;
private:
int mNumCarsInProduction;
};
class BMWFactory : public CarFactory
{
protected:
virtual Car* createCar();
};
class GeliFactory : public CarFactory
{
protected:
virtual Car* createCar();
};
最后实现CarFactory
/**
* CarFactory.cpp
*/
#include "CarFactory.h"
// Initialize the count to zero when the factory is created.
CarFactory::CarFactory() : mNumCarsInProduction(0) {}
// Increment the number of cars in production and return the
// new car.
Car* CarFactory::requestCar()
{
mNumCarsInProduction++;
return createCar();
}
int CarFactory::getNumCarsInProduction() const
{
return mNumCarsInProduction;
}
Car* BMWFactory::createCar()
{
return new BMW();
}
Car* GeliFactory::createCar()
{
return new Geli();
}
使用CarFactory
最简单的办法就是实例化并调用正确的方法,例如
GeliFactory myFactory;
Car* myCar = myFactory.requestCar();
一个有意思的例子是使用虚拟构造器(vitual Constructor)来建立一个产量最小的工厂。这需要一个函数来检查各个工厂并且选择最闲的一个。例如:
CarFactory* getLeastBusyFactory(const vector<CarFactory*>& inFactories)
{
if (inFactories.size() == 0) return NULL;
CarFactory* bestSoFar = inFactories[0];
for (size_t i = 1; i < inFactories.size(); i++)
{
if (inFactories->getNumCarsInProduction() <
bestSoFar->getNumCarsInProduction()) {
bestSoFar = inFactories;
}
}
return bestSoFar;
}
以下代码将会利用上面的函数在最闲的工厂生产10辆车。
int main(int argc, char** argv)
{
vector<CarFactory*> factories;
// Create 3 Ford factories and 1 Toyota factory.
BMWFactory* factory1 = new BMWFactory();
BMWFactory* factory2 = new BMWFactory();
BMWFactory* factory3 = new BMWFactory();
GeliFactory* factory4 = new GeliFactory();
// To get more interesting results, pre-preorder some cars.
factory1->requestCar();
factory1->requestCar();
factory2->requestCar();
factory4->requestCar();
// Add the factories to a vector.
factories.push_back(factory1);
factories.push_back(factory2);
factories.push_back(factory3);
factories.push_back(factory4);
// Build 10 cars from the least busy factory.
for (int i = 0; i < 10; i++) {
CarFactory* currentFactory = getLeastBusyFactory(factories);
Car* theCar = currentFactory->requestCar();
theCar->info();
}
}