装饰者模式(Decorator)的c++实现(改进)

长平狐 发布于 2012/11/01 11:53
阅读 419
收藏 0

      今天看了hityct1装饰者模式(Decorator)的c++实现示例,想了一阵,才明白其中的内涵,下图为运行到return m_pMyHeader;的监视变量截图:

        其中header的展看,让我们看到了完成三次new操作后,类header通过成员变量pMyTrailer(指针)的指向情况。即从pMyComponent->prtTicket();开始调用,然后执行cout<<"具体装饰者Header打印表头/n"<<endl;,接着调用TicketDecorator::callTrailer(); ,获得pMyTrailer的值为0x00293328,然后执行Footer::prtTicket(void),接着再次调用TicketDecorator::callTrailer(); 跟上次原理一样,SalesTicket::prtTicket(void)被调用,输出cout<<"具体组件SalesTicket打印票据正文/n"<<endl;,然后回到函数Footer::prtTicket(void)内,输出cout<<"具体装饰者Footer打印表尾/n"<<endl;。

      在这个版本上我重新做了修改,即下面的版本

//工具为VS2005

#include "stdafx.h"
#include <iostream> 

using namespace std;

//================================================= 
class Component //抽象组件,即对象的接口 
{
public:
	virtual void prtTicket() = 0;
};
//================================================= 
//具体组件,即被装饰者 
class SalesTicket: public Component 
{
public:
	void prtTicket()
	{
		cout<<"具体组件SalesTicket打印票据正文/n"<<endl;
	}
};
//================================================= 
//装饰者(也是抽象组件的子类) 
class TicketDecorator: public Component 
{
public:
	
	//是否需要释放pMyTrailer?????? 
	//如果遵循“谁申请,谁释放”的原则,则不需要 
};
//================================================= 
//具体装饰者Header(是装饰者的子类) 
//功能:打印表头 
class Header: public TicketDecorator
{
protected: 
	Component *m_pMyTrailer;
public:
	Header(Component* myComponent)
	{
		m_pMyTrailer = myComponent;
		//cout<<"构造具体装饰者Header/n"<<endl; 
	}

	void prtTicket()
	{
		//功能:在表的前面加个头部 
		//注意这行代码的位置,在callTrailer()之前 
		//这是装饰者添加的功能 
		cout<<"具体装饰者Header打印表头/n"<<endl;
		m_pMyTrailer->prtTicket();
	}
};

//具体装饰者Footer(是装饰者的子类) 
//功能:打印表尾 
class Footer: public TicketDecorator
{
protected:
	Component *m_pMyTrailer;
public:
	Footer(Component* myComponent)
	{
		//cout<<"构造具体装饰者Footer/n"<<endl;
		m_pMyTrailer = myComponent;
	}

	void prtTicket()
	{
		m_pMyTrailer->prtTicket();
		cout<<"具体装饰者Footer打印表尾/n"<<endl;
	}
};
//================================================= 
class Factory//工厂 
{
public:
	Component* m_pMyComponent;
	Component* m_pMyFooter;
	Component* m_pMyHeader;

	Factory()
	{
		Component* m_pMyComponent = NULL; 
		Component* m_pMyFooter = NULL; 
		Component* m_pMyHeader = NULL;  
	}

	Component* getComponent()
	{  
		m_pMyComponent = new SalesTicket();
		m_pMyFooter    = new Footer(m_pMyComponent);
		m_pMyHeader    = new Header(m_pMyFooter);

		return m_pMyHeader;//返回最后一个指针 
	}

	~Factory()//别忘了释放空间 
	{

		delete m_pMyComponent;
		delete m_pMyFooter; 
		delete m_pMyHeader;
	}
};
//================================================= 

int _tmain(int argc, _TCHAR* argv[])
{
	Factory myFactory;  
	Component* pMyComponent = myFactory.getComponent();  
	pMyComponent->prtTicket();//打印    

	return 0;
}


    其中TicketDecorator不再有pMyTrailer,而是在Header类中加入Component *m_pMyTrailer;变量,即不再用通过TicketDecorator中的方法来访问指针,而是直接在Header和Footer类中的m_pMyTrailer访问虚函数prtTicket(),代码也简短了。

     其实用JAVA来实现上述代码,不需要用到m_pMyTrailer这个指针,直接通过 对象名.函数名 访问即可 如 Component.ptrTicket(),(JAVA很少用,应该没理解错吧)


原文链接:http://blog.csdn.net/xunyn/article/details/7485340
加载中
返回顶部
顶部