// 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;
}