1 概述
工厂模式专门负责将大量有共同接口的类实例化。工厂模式可以动态决定将哪一个类实例化,不必事先知道每次要实例化哪一个类。工厂模式有以下几种形态:
a) 简单工厂(Simple Factory)模式
专门定义一个类来负责创建其它类的实例,被创建的实例通常都具有共同的父类。
注意:简单工厂模式并不包含在23种模式之内。
b) 工厂方法(Factory Method)模式,又称多形性工厂(Polymorphic Factory)模式
将对象的创建交由父类中定义的一个标准方法来完成,而不是其构造函数,究竟应该创建何种对象由具体的子类负责决定。
通过继承来实现的。
c) 抽象工厂(Abstract Factory)模式,又称工具箱(Kit或Toolkit)模式
提供一个共同的接口来创建相互关联的多个对象。
通过组合来实现的,但是抽象工厂中创建产品的方法通常是以“工厂方法”来实现的
GOF在《设计模式》一书中将工厂模式分为两类:工厂方法模式(Factory Method)与抽象工厂模式(Abstract Factory)。将简单工厂模式(Simple Factory)看为工厂方法模式的一种特例,两者归为一类。
2 说明
工厂方法用来处理对象的创建,并将这样的行为封装在子类中。这样,客户程序中关于超类的代码就和子类对象创建代码解耦了。
工厂模式定义:实例化对象,用工厂方法代替new操作。
简单工厂代码示例
class Light
{
public:
virtual void TurnOn() = 0;
virtual void TurnOff() = 0;
};
class BulbLight : public Light
{
public:
virtual void TurnOn()
{
cout << "Bulb Light is Turned on\n";
}
virtual void TurnOff()
{
cout << "Bulb Light is Turned off\n";
}
};
class TubeLight : public Light
{
public:
virtual void TurnOn()
{
cout << "Tube Light is Turned on\n";
}
virtual void TurnOff()
{
cout << "Tube Light is Turned off\n";
}
};
typedef enum EM_LightType
{
LightType_Bulb,
LightType_Tube,
}LightType;
class LightSimpleFactory
{
public:
Light* Create(LightType emLightType)
{
if(LightType_Bulb == emLightType)
{
return new BulbLight();
}
else if(LightType_Tube == emLightType)
{
return new TubeLight();
}
else
{
return NULL;
}
}
};
class LightSimpleFactoryTest
{
public:
void run()
{
LightSimpleFactory* pFactory = new LightSimpleFactory();
Light* pLight = pFactory->Create(LightType_Bulb);
pLight->TurnOn();
pLight->TurnOff();
delete pLight;
cout <<"-----------------\n";
pLight = pFactory->Create(LightType_Tube);
pLight->TurnOn();
pLight->TurnOff();
delete pLight;
delete pFactory;
}
};
工厂方法示例
#ifndef FACTORY_H
#define FACTORY_H
#include <iostream>
#include <string>
#include <list>
#include <cassert>
using namespace std;
//工厂模式
namespace Factory
{
//const definitions
//////////////////////////////////////////////////////////////////////////
typedef list<string> ToppingList;
//////////////////////////////////////////////////////////////////////////
class Pizza
{
public:
const char* getName()
{
return m_Name.c_str();
}
void prepare()
{
// class PizzaIngredientFactory
// {
// public:
// Dough* creatDough() { return new Dough(); }
// Sauce* creatSauce() { return new Sauce(); }
// };
// PizzaIngredientFactory ingredientFactory;
//
// m_Dough = ingredientFactory.creatDough();
// m_Sauce = ingredientFactory.creatSauce();
cout << "Preparing " << m_Name.c_str() << endl;
cout << "Tossing dough..." << endl;
cout << "Adding sauce..." << endl;
cout << "Adding topping: " << endl;
ToppingList::const_iterator iter = m_Toppings.begin();
ToppingList::const_iterator end = m_Toppings.end();
for ( ; iter != end; ++iter)
{
cout << " " << (*iter).c_str();
}
cout << endl;
}
void bake()
{
cout << "Bake for 25 minutes at 350" << endl;
}
void cut()
{
cout << "Cutting the pizza into diagonal slices" << endl;
}
void box()
{
cout << "Place pizza in official PizzaStore box" << endl;
}
protected:
string m_Name;
string m_Dough;
string m_Sauce;
ToppingList m_Toppings;
};
//////////////////////////////////////////////////////////////////////////
class NYStyleCheesePizza : public Pizza
{
public:
NYStyleCheesePizza()
{
m_Name = "NY Style Sauce and cheese Pizza";
m_Dough = "Thin Crust Dough";
m_Sauce = "Marinara Sauce";
m_Toppings.push_back(string("Grated Reggiano Cheese"));
}
};
//////////////////////////////////////////////////////////////////////////
class ChicagoStyleCheesePizza : public Pizza
{
public:
ChicagoStyleCheesePizza()
{
m_Name = "Chicago Style Deep Dish cheese Pizza";
m_Dough = "Extra Thick Crust Dough";
m_Sauce = "Plum Tomato Sauce";
m_Toppings.push_back(string("Shredded Mozzarella Cheese"));
}
void cut()
{
cout << "Cutting the pizza into square slices" << endl;
}
};
//////////////////////////////////////////////////////////////////////////
class PizzaStore
{
public:
Pizza* orderPizza( string type)
{
if (m_Pizza != NULL)
{
delete m_Pizza;
m_Pizza = NULL;
}
m_Pizza = createPizza(type);
if ( m_Pizza != NULL)
{
m_Pizza->prepare();
m_Pizza->bake();
m_Pizza->cut();
m_Pizza->box();
}
return m_Pizza;
}
PizzaStore()
: m_Pizza(NULL)
{}
virtual ~PizzaStore()
{
if (m_Pizza!=NULL)
{
delete m_Pizza;
m_Pizza = NULL;
}
}
protected:
virtual Pizza* createPizza(string type) = 0;
protected:
Pizza* m_Pizza;
};
//the factory
//////////////////////////////////////////////////////////////////////////
class NYStylePizzaStore : public PizzaStore
{
public:
virtual Pizza* createPizza(string type)
{
if (type == "cheese")
{
m_Pizza = new NYStyleCheesePizza();
}
else if (type == "pepperoni")
{
//m_Pizza = new NYStylePepperoniPizza();
}
else if (type=="clam")
{
//m_Pizza = new NYStyleClamPizza();
}
else if (type=="veggie")
{
//m_Pizza = new NYStyleVeggiePizza();
}
else
{
m_Pizza = NULL;
}
return m_Pizza;
}
};
//////////////////////////////////////////////////////////////////////////
class ChicgoStytlePizzaStore : public PizzaStore
{
public:
virtual Pizza* createPizza(string type)
{
if (type == "cheese")
{
m_Pizza = new ChicagoStyleCheesePizza();
}
else if (type == "pepperoni")
{
//m_Pizza = new ChicagoStylePepperoniPizza();
}
else if (type=="clam")
{
//m_Pizza = new ChicagoStyleClamPizza();
}
else if (type=="veggie")
{
//m_Pizza = new ChicagoStyleVeggiePizza();
}
else
{
m_Pizza = NULL;
}
return m_Pizza;
}
};
//////////////////////////////////////////////////////////////////////////
class PizzaTestDrive
{
public:
void run()
{
PizzaStore* nyStore = new NYStylePizzaStore();
PizzaStore* chicagoStore = new ChicgoStytlePizzaStore();
Pizza* pizza = nyStore->orderPizza("cheese");
assert(pizza != NULL);
cout << "Ethan ordered a " << pizza->getName() << endl;
pizza = chicagoStore->orderPizza("cheese");
assert(pizza != NULL);
cout << "Joel ordered a " << pizza->getName() << endl;
delete nyStore;
delete chicagoStore;
}
};
}//namespace Factory
#endif
抽象工厂示例
#include <iostream>
using namespace std;
class AbstractProductA
{
public:
virtual ~AbstractProductA(){}
protected:
AbstractProductA(){}
private:
};
class AbstractProductB
{
public:
virtual ~AbstractProductB(){}
protected:
AbstractProductB(){}
};
class ProductA1 : public AbstractProductA
{
public:
ProductA1()
{
cout<<"ProductA1..."<<endl;
}
~ProductA1(){}
};
class ProductA2 : public AbstractProductA
{
public:
ProductA2()
{
cout<<"ProductA2..."<<endl;
}
~ProductA2(){}
};
class ProductB1 : public AbstractProductB
{
public:
ProductB1()
{
cout<<"ProductB1..." << endl;
}
~ProductB1(){}
};
class ProductB2 : public AbstractProductB
{
public:
ProductB2()
{
cout<<"ProductB2..." << endl;
}
~ProductB2(){}
};
//////////////////////////////////////////////////////////////////////////
class AbstractFactory
{
public:
virtual ~AbstractFactory(){}
virtual AbstractProductA *CreateProductA() = 0;
virtual AbstractProductB *CreateProductB() = 0;
protected:
AbstractFactory(){}
};
class ConcreteFactory1 : public AbstractFactory
{
public:
ConcreteFactory1()
{
}
~ConcreteFactory1()
{
}
AbstractProductA* CreateProductA()
{
return new ProductA1();
}
AbstractProductB* CreateProductB()
{
return new ProductB1();
}
};
class ConcreteFactory2 : public AbstractFactory
{
public:
ConcreteFactory2(){}
~ConcreteFactory2(){}
AbstractProductA *CreateProductA()
{
return new ProductA2();
}
AbstractProductB *CreateProductB()
{
return new ProductB2();
}
};
//////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
AbstractFactory *cf1 = new ConcreteFactory1();
cf1->CreateProductA();
cf1->CreateProductB();
AbstractFactory *cf2 = new ConcreteFactory2();
cf2->CreateProductA();
cf2->CreateProductB();
return 0;
}