Welcome to tiger's blog!

What lead to success, what we are seeking...
posts - 47, comments - 23, trackbacks - 0, articles - 8
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

设计模式(Decorator)

Posted on 2007-04-14 15:06 tiger 阅读(311) 评论(0)  编辑 收藏 引用

// Component.h: interface for the CComponent class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_COMPONENT_H__9FC0172F_E325_422F_82D4_719D1E4DB8A6__INCLUDED_)
#define AFX_COMPONENT_H__9FC0172F_E325_422F_82D4_719D1E4DB8A6__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <iostream>
using namespace std;

class CComponent 
{
public:
 CComponent();
 virtual ~CComponent();

public:
 virtual void Func(void) = 0;

};

#endif // !defined(AFX_COMPONENT_H__9FC0172F_E325_422F_82D4_719D1E4DB8A6__INCLUDED_)

// Component.cpp: implementation of the CComponent class.
//
//////////////////////////////////////////////////////////////////////

#include "Component.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CComponent::CComponent()
{

}

CComponent::~CComponent()
{

}


// Decorator.h: interface for the CDecorator class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_DECORATOR_H__D1305BFE_4F87_4D1F_A661_8496D9C28CFE__INCLUDED_)
#define AFX_DECORATOR_H__D1305BFE_4F87_4D1F_A661_8496D9C28CFE__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "Component.h"

class CDecorator : public CComponent 
{
public:
 CDecorator(CComponent *pComponent);
 virtual ~CDecorator();

protected:
 CComponent *m_pComponent;

};

class CConcreteDecorator : public CDecorator
{
public:
 CConcreteDecorator(CComponent *pComponent);
 virtual ~CConcreteDecorator();

public:
 void Func(void);

private:
 void AdditionFunc(void);
};

#endif // !defined(AFX_DECORATOR_H__D1305BFE_4F87_4D1F_A661_8496D9C28CFE__INCLUDED_)

// Decorator.cpp: implementation of the CDecorator class.
//
//////////////////////////////////////////////////////////////////////

#include "Decorator.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDecorator::CDecorator(CComponent *pComponent)
{
 m_pComponent = pComponent;
}

CDecorator::~CDecorator()
{

}

//////////////////////////////////////////////////////////////////////
CConcreteDecorator::CConcreteDecorator(CComponent *pComponent)
 : CDecorator(pComponent)
{
}

CConcreteDecorator::~CConcreteDecorator()
{
}

void CConcreteDecorator::Func()
{
 m_pComponent->Func();
 AdditionFunc();
}

void CConcreteDecorator::AdditionFunc()
{
 cout<<"CConcreteDecorator::AdditionFunc"<<endl;
}
//////////////////////////////////////////////////////////////////////

//main.cpp
#include "ConcreteComponent.h"
#include "Decorator.h"

void main()
{
 CComponent *pComponent = new CConcreteComponent();
 CComponent *pDecorator = new CConcreteDecorator(pComponent);
 pDecorator->Func();
 delete pDecorator;
 delete pComponent;
}


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理