抽象工厂(Abstract Factory)
抽象工厂是为了提供一系列相关或相互依赖对象的接口。对象创建型模式的一种。
客户Client
抽象工厂接口AbstractFactory
抽象工厂的实现类ConcreteFactory
抽象产品接口AbstractProduct
产品实现类ConcreteProduct
需要生产的几种产品分别是:ConcreteProductA1/ConcreteProductA2/ConcreteProductB1/ConcreteProductB2。
各个系列产品的启动和退出方式相同,但是运行方式不同。这里分别用一个具体工厂ConcreteFactory1和ConcreteFactory2的对象来生产多种产品。
首先是Abstact以及Concrete Product:
下面的代码应该注意的是,ConcreteProductA1与ConcreteProductB1实际上是属于同一产品族的。所谓的统一产品族指的是类似的产品,例如BenzSportCar和BmwSportCar和AudiSportCar属于SportCar产品族.
class AbstractProductA{
public:
AbstractProductA(){}
virtual ~AbstractProductA(){}
void start(){
cout << "Product A start like this !" << endl;
}
virtual void execute() = ;
void exit(){
cout << "Product A exit like this !" << endl;
}
}; class ConcreteProductA1 : public AbstractProductA{
public:
ConcreteProductA1(){}
~ConcreteProductA1(){}
void execute(){
cout << "ConcreteProductA1 execute like this!" << endl;
}
}; class ConcreteProductA2 : public AbstractProductA{
public:
ConcreteProductA2(){}
~ConcreteProductA2(){}
void execute(){
cout << "ConcreteProductA2 execute like this!" << endl;
}
}; class AbstractProductB{
public:
AbstractProductB(){}
virtual ~AbstractProductB(){}
void start(){
cout << "Product B start like this!" << endl;
}
virtual void execute() = ;
void exit(){
cout << "ProductB exit like this!" << endl;
}
}; class ConcreteProductB1 : public AbstractProductB{
public:
ConcreteProductB1(){}
~ConcreteProductB1(){}
void execute(){
cout << "concreteProductB1 execute like this!" << endl;
}
}; class ConcreteProductB2 : public AbstractProductB{
public:
ConcreteProductB2(){}
~ConcreteProductB2(){}
void execute(){
cout << "concreteProductB2 execute like this!" << endl;
}
};
之后就是Abstract以及Concrete Factory:
class AbstractFactory{
public:
AbstractFactory(){}
virtual ~AbstractFactory(){}
virtual AbstractProductA * createProductA() = ;
virtual AbstractProductB * createProductB() = ;
}; class ConcreteFactory1 : public AbstractFactory{
public:
ConcreteFactory1(){}
~ConcreteFactory1(){}
AbstractProductA * createProductA();
AbstractProductB * createProductB();
}; AbstractProductA * ConcreteFactory1::createProductA()
{
return new ConcreteProductA1();
} AbstractProductB * ConcreteFactory1::createProductB()
{
return new ConcreteProductB1();
} class ConcreteFactory2 : public AbstractFactory{
public:
ConcreteFactory2(){}
~ConcreteFactory2(){};
AbstractProductA * createProductA();
AbstractProductB * createProductB();
}; AbstractProductA * ConcreteFactory2::createProductA()
{
return new ConcreteProductA2();
} AbstractProductB * ConcreteFactory2::createProductB()
{
return new ConcreteProductB2();
}
测试方法如下所示:
int main()
{
AbstractFactory * abf = new ConcreteFactory1();
AbstractProductA * aba = abf->createProductA();
aba->start();
aba->execute();
aba->exit();
AbstractProductB * abb = abf->createProductB();
abb->start();
abb->execute();
abb->exit(); abf = new ConcreteFactory2();
aba = abf->createProductA();
aba->start();
aba->execute();
aba->exit();
abb = abf->createProductB();
abb->start();
abb->execute();
abb->exit();
system("pause");
}
参考:
http://www.cnblogs.com/zhangchenliang/p/3700820.html 这个讲的真的很好,推荐
http://www.cnblogs.com/cbf4life/archive/2009/12/23/1630612.html