参考连接:
http://www.cnblogs.com/cxjchen/p/3143633.html 详细的对比
http://blog.csdn.net/wuzhekai1985/article/details/6660462 生动的例子
http://www.cnblogs.com/sld666666/archive/2011/05/27/2060234.html 智能指针shared_ptr管理内存
工厂模式:分为简单工厂模式,工厂模式,抽象工厂模式。
三种模式结构复杂度递增,适应场景各有不同,各有优缺点,可根据实际情况选用。
什么时候使用工厂模式?
只有当无法预料所创建的实例时,才使用工厂模式。即:我们明确的计划不同条件下创建不同实例时,使用它。
经典场景1:
日志记录器:记录可能记录到本地硬盘、系统事件、远程服务器等,用户可以选择记录日志到什么地方。
经典场景2:
数据库访问类:访问可能是SQLSERVER、ORACLE等,用记可以选择创建访问不同的数据库
1.简单工厂模式
UML类图:
优点:如果ProductA,ProductB...等产品,所用的method都相同,那么此模式很好,只要知道type就能活动相应的Product。
缺点:如果所有Product的方法不同,此时客户端Client调用各个Product对应的方法时,也必须知道是哪个产品才能成功使用。(下面介绍的工厂模式能够解决此问题,但是有个前提:如果Product中根据拥有不同method,还能对Product分类)
2.工厂模式
UML类图:
优点:更加灵活
3.抽象工厂模式
UML类图:
代码:
1.简单工厂模式
product.h代码:
<span style="font-size:18px;">#ifndef _PRODUCT_H_
#define _PRODUCT_H_
#include <iostream>
using namespace std;
class AbstractProduct
{
public:
AbstractProduct(){ cout << "AbstractProduct Constructor..." << endl; }
virtual ~AbstractProduct(){ cout << "~AbstractProduct Destructor..." << endl; }
virtual void Operation() = 0;
private:
};
class ProductA :public AbstractProduct
{
public:
ProductA(){ cout << "ProductA Constructor..." << endl; }
virtual ~ProductA(){ cout << "~ProductA Destructor..." << endl; }
virtual void Operation() { cout << "ProductA Operation..." << endl; };
private:
};
class ProductB :public AbstractProduct
{
public:
ProductB(){ cout << "ProductB Constructor..." << endl; }
virtual ~ProductB(){ cout << "~ProductB Destructor..." << endl; }
virtual void Operation() { cout << "ProductB Operation..." << endl; };
private:
};
#endif</span>
factory.h代码:
<span style="font-size:18px;">#ifndef _FACTORY_H_
#define _FACTORY_H_
#include <iostream>
using namespace std;
#include "Product.h"
class AbstractFactory
{
public:
AbstractFactory(){ cout << "AbstractFactory Constructor..." << endl; }
virtual ~AbstractFactory(){ cout << "~AbstractFactory Destructor..." << endl; }
virtual AbstractProduct* CreateProduct(int nType) = 0;
private:
};
class SimpleFactory : public AbstractFactory
{
public:
SimpleFactory(){ cout << "Factory Constructor..." << endl; }
~SimpleFactory(){ cout << "~Factory Destructor..." << endl; }
virtual AbstractProduct* CreateProduct(int nType);
private:
};
AbstractProduct* SimpleFactory::CreateProduct(int nType)
{
AbstractProduct* temp = NULL;
switch (nType)
{
case 1:
temp = new ProductA();
break;
case 2:
temp = new ProductB();
break;
default:
break;
}
return temp;
}
#endif</span>
client代码:
<span style="font-size:18px;">#include "Factory.h"
#include "Product.h"
int _tmain(int argc, _TCHAR* argv[])
{
SimpleFactory pfactory;
AbstractProduct *pProductA = pfactory.CreateProduct(1);
pProductA->Operation();
if (pProductA)
{
delete pProductA;
pProductA = nullptr;
}
return 0;
}</span>
product.h代码:同简单工厂模式
Factory.h代码:
#ifndef _FACTORY_H_
#define _FACTORY_H_
#include <iostream>
using namespace std;
#include "Product.h"
class AbstractFactory
{
public:
AbstractFactory(){ cout << "AbstractFactory Constructor..." << endl; }
virtual ~AbstractFactory(){ cout << "~AbstractFactory Destructor..." << endl; }
virtual AbstractProduct* CreateProduct() = 0;
private:
};
class FactoryA : public AbstractFactory
{
public:
FactoryA(){ cout << "FactoryA Constructor..." << endl; }
~FactoryA(){ cout << "~FactoryA Destructor..." << endl; }
virtual AbstractProduct* CreateProduct();
private:
};
AbstractProduct* FactoryA::CreateProduct()
{
return new ProductA();
}
class FactoryB : public AbstractFactory
{
public:
FactoryB(){ cout << "FactoryB Constructor..." << endl; }
~FactoryB(){ cout << "~FactoryB Destructor..." << endl; }
virtual AbstractProduct* CreateProduct();
private:
};
AbstractProduct* FactoryB::CreateProduct()
{
return new ProductB();
}
#endif
客户端代码:
int _tmain(int argc, _TCHAR* argv[])
{
FactoryB factoryB;
AbstractProduct *pProductB = factoryB.CreateProduct();
pProductB->Operation();
if (pProductB)
{
delete pProductB;
pProductB = nullptr;
}
return 0;
}
3.抽象工厂模式
product.h代码:
#ifndef _PRODUCT_H_
#define _PRODUCT_H_
#include <iostream>
using namespace std;
class AbstractProductA
{
public:
AbstractProductA(){ cout << "AbstractProductA Constructor..." << endl; }
virtual ~AbstractProductA(){ cout << "~AbstractProductA Destructor..." << endl; }
virtual void Operation() = 0;
private:
};
class ProductA1 :public AbstractProductA
{
public:
ProductA1(){ cout << "ProductA1 Constructor..." << endl; }
virtual ~ProductA1(){ cout << "~ProductA1 Destructor..." << endl; }
virtual void Operation() { cout << "ProductA1 Operation..." << endl; };
private:
};
class ProductA2 :public AbstractProductA
{
public:
ProductA2(){ cout << "ProductA2 Constructor..." << endl; }
virtual ~ProductA2(){ cout << "~ProductA2 Destructor..." << endl; }
virtual void Operation() { cout << "ProductA2 Operation..." << endl; };
private:
};
class AbstractProductB
{
public:
AbstractProductB(){ cout << "AbstractProductB Constructor..." << endl; }
virtual ~AbstractProductB(){ cout << "~AbstractProductB Destructor..." << endl; }
virtual void Operation() = 0;
private:
};
class ProductB1 :public AbstractProductB
{
public:
ProductB1(){ cout << "ProductB1 Constructor..." << endl; }
virtual ~ProductB1(){ cout << "~ProductB1 Destructor..." << endl; }
virtual void Operation() { cout << "ProductB1 Operation..." << endl; };
private:
};
class ProductB2 :public AbstractProductB
{
public:
ProductB2(){ cout << "ProductB2 Constructor..." << endl; }
virtual ~ProductB2(){ cout << "~ProductB2 Destructor..." << endl; }
virtual void Operation() { cout << "ProductB2 Operation..." << endl; };
private:
};
#endif
Factory.h代码:
#ifndef _FACTORY_H_
#define _FACTORY_H_
#include <iostream>
using namespace std;
#include "Product.h"
class AbstractFactory
{
public:
AbstractFactory(){ cout << "AbstractFactory Constructor..." << endl; }
virtual ~AbstractFactory(){ cout << "~AbstractFactory Destructor..." << endl; }
virtual AbstractProductA* CreateProductA() = 0;
virtual AbstractProductB* CreateProductB() = 0;
private:
};
class FactoryA : public AbstractFactory
{
public:
FactoryA(){ cout << "FactoryA Constructor..." << endl; }
~FactoryA(){ cout << "~FactoryA Destructor..." << endl; }
virtual AbstractProductA* CreateProductA();
virtual AbstractProductB* CreateProductB();
private:
};
AbstractProductA* FactoryA::CreateProductA()
{
return new ProductA1();
}
AbstractProductB* FactoryA::CreateProductB()
{
return new ProductB1();
}
class FactoryB : public AbstractFactory
{
public:
FactoryB(){ cout << "FactoryB Constructor..." << endl; }
~FactoryB(){ cout << "~FactoryB Destructor..." << endl; }
virtual AbstractProductA* CreateProductA();
virtual AbstractProductB* CreateProductB();
private:
};
AbstractProductA* FactoryB::CreateProductA()
{
return new ProductA2();
}
AbstractProductB* FactoryB::CreateProductB()
{
return new ProductB2();
}
#endif
客户端代码:
int _tmain(int argc, _TCHAR* argv[])
{
FactoryA factoryA;//FactoryA创建ProductA1,ProductB1
AbstractProductA *pProductA1 = factoryA.CreateProductA();
pProductA1->Operation();
if (pProductA1)
{
delete pProductA1;
pProductA1 = nullptr;
}
FactoryB factoryB;//FactoryB创建ProductA2,ProductB2
AbstractProductA *pProductA2 = factoryB.CreateProductA();
pProductA2->Operation();
if (pProductA2)
{
delete pProductA2;
pProductA2 = nullptr;
}
return 0;
}