作者在 2010-05-02 14:27:46 发布以下内容
在阅读了《GoF 23种设计模式解析附C++实现源码(2nd Edition)》之后,发现里面的代码并不能直接使用,也许是因为作者只是为了讲述设计模式的思想,所以代码中有一些new的指针并没有删除它,又或是希望这些指针让调用者用者自己管理,关于这一点,对初学者来说也是比较重要的,需要在设计时考虑到的问题。
文章中提到一些我以前没有用过的设计模式的方式,很感兴趣,比如Factory模式中,讲到使用模板来做,但代码中没有给出完整的代码,于是就动手试验一下。
//=====main.cpp=========
#include <stdlib.h>
#include <iostream>
#include <iostream>
#include "Factory.h"
int main()
{
//=================Factory模式的测试代码================
std::cout << "Factory模式的测试\n";
Creator<ConcreateProduct> *p = new ConcreateCreator<ConcreateProduct>();
p->AnOperation();
{
//=================Factory模式的测试代码================
std::cout << "Factory模式的测试\n";
Creator<ConcreateProduct> *p = new ConcreateCreator<ConcreateProduct>();
p->AnOperation();
ConcreateProduct* pProduct = p->GetProduct();
if(pProduct!=NULL)
{
pProduct->FunTest();
}
{
pProduct->FunTest();
}
delete p;
system("pause");
return 0;
}
//=====main.cpp end========
//=====Factory.h 模板方式==========
#ifndef FACTORY_H
#define FACTORY_H
#define FACTORY_H
#include <iostream>
//要实现的对象的基类
class Product
{
public:
Product(void){}
virtual ~Product(void){}
};
{
public:
Product(void){}
virtual ~Product(void){}
};
//实际要实现的类
class ConcreateProduct
: public Product
{
public:
ConcreateProduct(void);
virtual ~ConcreateProduct(void);
: public Product
{
public:
ConcreateProduct(void);
virtual ~ConcreateProduct(void);
void FunTest(void){std::cout<<"调用产品的方法!"<<std::endl;}
};
};
//创建者的模板基类
template<typename T>
class Creator
{
public:
Creator(void):m_pProduct(0){}
virtual ~Creator(void);
class Creator
{
public:
Creator(void):m_pProduct(0){}
virtual ~Creator(void);
void AnOperation(void);
T* GetProduct(void){return m_pProduct;}
protected:
T* m_pProduct;
virtual T* FactoryMethod(void) = 0;
};
T* m_pProduct;
virtual T* FactoryMethod(void) = 0;
};
template<typename T>
void Creator<T>::AnOperation(void)
{
m_pProduct = FactoryMethod();
void Creator<T>::AnOperation(void)
{
m_pProduct = FactoryMethod();
std::cout << "an operation of product\n";
}
}
template<typename T>
Creator<T>::~Creator(void)
{
if(0!=m_pProduct)
{
delete m_pProduct;
}
}
Creator<T>::~Creator(void)
{
if(0!=m_pProduct)
{
delete m_pProduct;
}
}
//生产实际对象的工厂类
template<typename T>
class ConcreateCreator
: public Creator<T>
{
public:
ConcreateCreator(void);
virtual ~ConcreateCreator(void);
template<typename T>
class ConcreateCreator
: public Creator<T>
{
public:
ConcreateCreator(void);
virtual ~ConcreateCreator(void);
protected:
virtual T* FactoryMethod();
};
virtual T* FactoryMethod();
};
template<typename T>
T* ConcreateCreator<T>::FactoryMethod()
{
return new T();
}
template<typename T>
ConcreateCreator<T>::~ConcreateCreator( void )
{
std::cout << "destruction of ConcreateCreator\n";
}
template<typename T>
ConcreateCreator<T>::ConcreateCreator(void)
{
std::cout << "construction of ConcreateCreator\n";
}
#endif
T* ConcreateCreator<T>::FactoryMethod()
{
return new T();
}
template<typename T>
ConcreateCreator<T>::~ConcreateCreator( void )
{
std::cout << "destruction of ConcreateCreator\n";
}
template<typename T>
ConcreateCreator<T>::ConcreateCreator(void)
{
std::cout << "construction of ConcreateCreator\n";
}
#endif
//=====Factory.h end=======
//=====Factory.cpp=======
#include "Factory.h"
#include <iostream>
#include <iostream>
using namespace std;
ConcreateProduct::ConcreateProduct( void )
{
std::cout << "construction of ConcreateProduct\n";
}
ConcreateProduct::~ConcreateProduct()
{
std::cout << "destruction of ConcreateProduct\n";
}
//=====Factory.cpp end=======
上面的代码,产品的指针放在的工厂内部,外部调用者只要管理工厂的指针销毁即可,那么,如果不希望工厂来管理指针(这种情况更方便统一管理产品的对象指针),先把Creator类的析构里删除指针的代码去掉,并去掉Creator类的m_pProduct指针和GetProduct函数,然后将函数AnOperation修改如下:
声明:
template<typename T>
T* AnOperation(void)
定义:
template<typename T>
T* Creator<T>::AnOperation(void)
{
std::cout << "an operation of product\n";
T* Creator<T>::AnOperation(void)
{
std::cout << "an operation of product\n";
return FactoryMethod();
}
}
使用:
Creator<ConcreateProduct> *pCreator = new ConcreateCreator<ConcreateProduct>();
ConcreateProduct* pProduct = pCreator->AnOperation();
if(pProduct!=NULL)
{
pProduct->FunTest();
}
ConcreateProduct* pProduct = pCreator->AnOperation();
if(pProduct!=NULL)
{
pProduct->FunTest();
}
delete pCreator;
delete pProduct ;
或是:
ConcreateCreator<ConcreateProduct> tCreator ;
ConcreateProduct* pProduct = tCreator.AnOperation();
if(pProduct!=NULL)
{
pProduct->FunTest();
}
ConcreateProduct* pProduct = tCreator.AnOperation();
if(pProduct!=NULL)
{
pProduct->FunTest();
}
delete pProduct ;