设计模式-创建型 C++版本
2011-05-15 20:18 Clingingboy 阅读(551) 评论(0) 编辑 收藏 举报
一.Factory Method
http://www.cnblogs.com/Clingingboy/archive/2010/08/26/1809554.html
// 产品抽象类
class CProduct
{
public:
CProduct()
{
theApp.Trace(_T("CProduct 构造"));
};
virtual ~CProduct()
{
theApp.Trace(_T("CProduct 析构"));
};
virtual void Operation() = 0;
};
// 创建者抽象类
class CCreator
{
public:
CCreator()
{
theApp.Trace(_T("CCreator 构造"));
};
virtual ~CCreator()
{
theApp.Trace(_T("CCreator 析构"));
};
virtual CProduct* FactoryMethod() = 0;
void Operation()
{
theApp.Trace(_T("CCreator::Operation()"));
CProduct *pProduct = FactoryMethod();
ASSERT(NULL != pProduct);
pProduct->Operation();
delete pProduct;
pProduct = NULL;
};
};
// 具体产品
class CConcreteProduct : public CProduct
{
public:
CConcreteProduct()
{
theApp.Trace(_T("CConcreteProduct 构造"));
};
virtual ~CConcreteProduct()
{
theApp.Trace(_T("CConcreteProduct 析构"));
};
virtual void Operation()
{
theApp.Trace(_T("CConcreteProduct::Operation()"));
};
};
// 具体创建者
class CConcreteCreator : public CCreator
{
public:
CConcreteCreator()
{
theApp.Trace(_T("CConcreteCreator 构造"));
};
virtual ~CConcreteCreator()
{
theApp.Trace(_T("CConcreteCreator 析构"));
};
virtual CProduct* FactoryMethod()
{
return new CConcreteProduct;
};
};
Test
// 由子类创建对象
CConcreteCreator oCreator;
oCreator.Operation();
二.AbstractFactory
http://www.cnblogs.com/Clingingboy/archive/2010/08/26/1809556.html
// 产品抽象类A
class CAbstractProductA
{
public:
CAbstractProductA()
{
theApp.Trace(_T("CAbstractProductA 构造"));
};
virtual ~CAbstractProductA()
{
theApp.Trace(_T("CAbstractProductA 析构"));
};
virtual void Operation() = 0;
};
// 产品抽象类B
class CAbstractProductB
{
public:
CAbstractProductB()
{
theApp.Trace(_T("CAbstractProductB 构造"));
};
virtual ~CAbstractProductB()
{
theApp.Trace(_T("CAbstractProductB 析构"));
};
virtual void Operation() = 0;
};
// 工厂抽象类
class CAbstractFactory
{
public:
CAbstractFactory()
{
theApp.Trace(_T("CAbstractFactory 构造"));
};
virtual ~CAbstractFactory()
{
theApp.Trace(_T("CAbstractFactory 析构"));
};
virtual CAbstractProductA* CreateProductA() = 0;
virtual CAbstractProductB* CreateProductB() = 0;
};
// 具体产品A1
class CProductA1 : public CAbstractProductA
{
public:
CProductA1()
{
theApp.Trace(_T("CProductA1 构造"));
};
virtual ~CProductA1()
{
theApp.Trace(_T("CProductA1 析构"));
};
virtual void Operation()
{
theApp.Trace(_T("CProductA1::Operation()"));
};
};
// 具体产品A2
class CProductA2 : public CAbstractProductA
{
public:
CProductA2()
{
theApp.Trace(_T("CProductA2 构造"));
};
virtual ~CProductA2()
{
theApp.Trace(_T("CProductA2 析构"));
};
virtual void Operation()
{
theApp.Trace(_T("CProductA2::Operation()"));
};
};
// 具体产品B1
class CProductB1 : public CAbstractProductB
{
public:
CProductB1()
{
theApp.Trace(_T("CProductB1 构造"));
};
virtual ~CProductB1()
{
theApp.Trace(_T("CProductB1 析构"));
};
virtual void Operation()
{
theApp.Trace(_T("CProductB1::Operation()"));
};
};
// 具体产品B2
class CProductB2 : public CAbstractProductB
{
public:
CProductB2()
{
theApp.Trace(_T("CProductB2 构造"));
};
virtual ~CProductB2()
{
theApp.Trace(_T("CProductB2 析构"));
};
virtual void Operation()
{
theApp.Trace(_T("CProductB2::Operation()"));
};
};
// 具体工厂1
class CConcreteFactory1 : public CAbstractFactory
{
public:
CConcreteFactory1()
{
theApp.Trace(_T("CConcreteFactory1 构造"));
};
virtual ~CConcreteFactory1()
{
theApp.Trace(_T("CConcreteFactory1 析构"));
};
virtual CAbstractProductA* CreateProductA()
{
theApp.Trace(_T("CConcreteFactory1::CreateProductA()"));
return new CProductA1;
};
virtual CAbstractProductB* CreateProductB()
{
theApp.Trace(_T("CConcreteFactory1::CreateProductB()"));
return new CProductB1;
};
};
// 具体工厂2
class CConcreteFactory2 : public CAbstractFactory
{
public:
CConcreteFactory2()
{
theApp.Trace(_T("CConcreteFactory2 构造"));
};
virtual ~CConcreteFactory2()
{
theApp.Trace(_T("CConcreteFactory2 析构"));
};
virtual CAbstractProductA* CreateProductA()
{
theApp.Trace(_T("CConcreteFactory2::CreateProductA()"));
return new CProductA2;
};
virtual CAbstractProductB* CreateProductB()
{
theApp.Trace(_T("CConcreteFactory2::CreateProductB()"));
return new CProductB2;
};
};
Test
// 创建一族对象
CAbstractProductA *pProductA = NULL;
CAbstractProductB *pProductB = NULL;
CConcreteFactory1 oFactory1;
pProductA = oFactory1.CreateProductA();
pProductB = oFactory1.CreateProductB();
ASSERT(NULL != pProductA && NULL != pProductB);
pProductA->Operation();
pProductB->Operation();
delete pProductA;
pProductA = NULL;
delete pProductB;
pProductB = NULL;
CConcreteFactory2 oFactory2;
pProductA = oFactory2.CreateProductA();
pProductB = oFactory2.CreateProductB();
ASSERT(NULL != pProductA && NULL != pProductB);
pProductA->Operation();
pProductB->Operation();
delete pProductA;
pProductA = NULL;
delete pProductB;
pProductB = NULL;
三.Builder
http://www.cnblogs.com/Clingingboy/archive/2010/08/26/1809558.html
// 生成器抽象类
class CBuilder
{
public:
CBuilder()
{
theApp.Trace(_T("CBuilder 构造"));
};
virtual ~CBuilder()
{
theApp.Trace(_T("CBuilder 析构"));
};
virtual void BuildPart1() = 0;
virtual void BuildPart2() = 0;
};
// 具体生成器类
class CConcreteBuilder : public CBuilder
{
public:
CConcreteBuilder()
{
theApp.Trace(_T("CConcreteBuilder 构造"));
};
virtual ~CConcreteBuilder()
{
theApp.Trace(_T("CConcreteBuilder 析构"));
};
virtual void BuildPart1()
{
theApp.Trace(_T("CBuilder::BuildPart1()"));
};
virtual void BuildPart2()
{
theApp.Trace(_T("CBuilder::BuildPart2()"));
};
};
// 控制器类
class CDirector
{
public:
CDirector()
{
theApp.Trace(_T("CDirector 构造"));
};
virtual ~CDirector()
{
theApp.Trace(_T("CDirector 析构"));
};
void Construct(CBuilder &oBuilder)
{
theApp.Trace(_T("CDirector::Construct()"));
oBuilder.BuildPart1();
oBuilder.BuildPart2();
};
};
Test
// 可以用于复杂对象的创建,所谓复杂是指步骤多、条件多等
CConcreteBuilder oBuilder;
CDirector oDirector;
oDirector.Construct(oBuilder);
四.Prototype
http://www.cnblogs.com/Clingingboy/archive/2010/08/26/1809549.html
// 原型抽象类
class CPrototype
{
public:
CPrototype()
{
theApp.Trace(_T("CPrototype 构造"));
};
virtual ~CPrototype()
{
theApp.Trace(_T("CPrototype 析构"));
};
virtual CPrototype* Clone() = 0;
};
// 具体原型类1
class CConcretePrototype1 : public CPrototype
{
public:
CConcretePrototype1()
{
theApp.Trace(_T("CConcretePrototype1 构造"));
};
virtual ~CConcretePrototype1()
{
theApp.Trace(_T("CConcretePrototype1 析构"));
};
virtual CPrototype* Clone()
{
theApp.Trace(_T("CConcretePrototype1::Clone()"));
CConcretePrototype1 *pClone = new CConcretePrototype1;
*pClone = *this;
return pClone;
};
};
// 具体原型类2
class CConcretePrototype2 : public CPrototype
{
public:
CConcretePrototype2()
{
theApp.Trace(_T("CConcretePrototype2 构造"));
};
virtual ~CConcretePrototype2()
{
theApp.Trace(_T("CConcretePrototype2 析构"));
};
virtual CPrototype* Clone()
{
theApp.Trace(_T("CConcretePrototype2::Clone()"));
CConcretePrototype2 *pClone = new CConcretePrototype2;
*pClone = *this;
return pClone;
};
};
Test
// 通过拷贝创建新对象
CPrototype *pPrototype = new CConcretePrototype1;
CPrototype *pPrototypeClone = pPrototype->Clone();
delete pPrototype;
pPrototype = NULL;
delete pPrototypeClone;
pPrototypeClone = NULL;
CConcretePrototype2 oPrototype;
pPrototypeClone = oPrototype.Clone();
delete pPrototypeClone;
pPrototypeClone = NULL;
五.Singleton
http://www.cnblogs.com/Clingingboy/archive/2010/08/26/1809560.html
// 单件类
class CSingleton
{
private:
CSingleton() // 私有构造,防止外部创建实例
{
theApp.Trace(_T("CSingleton 构造"));
};
public:
virtual ~CSingleton() {};
static CSingleton& GetInstance() // 全局访问点
{
theApp.Trace(_T("CSingleton::GetInstance()"));
static CSingleton s_oSingleton; // 静态对象,保证只有一个实例
return s_oSingleton;
};
void Operation()
{
theApp.Trace(_T("CSingleton::Operation()"));
};
};
// 方便外部调用
#define theSingleton (CSingleton::GetInstance())
Test
// 可以取代全局变量,用到的时候才创建对象
theSingleton.Operation();
theSingleton.Operation();