Decorator

main:

#include
#include "DECO.h"

using namespace
std;

int main(void)
{
CUserInterface userInterface;

CRootDecorator rootDecorator(userInterface);
CTerranDecorator
terranDecorator(rootDecorator);
terranDecorator.Draw();
system("pause");

return 0;
}

CUserInterface.h:

#include

using namespace std;

class CUserInterface
{

public:
void DrawPlayField()
{
cout<<"Drawing playing
field"< }
};

class IDecorator
{
public:

virtual void Draw() = 0;
protected:
virtual void DrawDecoration() =
0;
};

class CDecorator : public IDecorator
{
public:

CDecorator(IDecorator& decorator)
:m_decorator(decorator){}
void
Draw()
{
m_decorator.Draw();
DrawDecoration();
}
protected:

IDecorator& m_decorator;
};

class CRootDecorator : public
IDecorator
{
public:
CRootDecorator(CUserInterface&
userInterface)
:m_userInterface(userInterface){}
void Draw()
{

m_userInterface.DrawPlayField();
}
protected:
void
DrawDecoration()
{
Draw();
}
CUserInterface&
m_userInterface;
};

DECO.h:

#include
#include
"CUserInterface.h"
using namespace std;

class CZergDecorator :
public CDecorator
{
public:
CZergDecorator(IDecorator&
decorator)
:CDecorator(decorator){}
protected:
void DrawDecoration()

{
cout<<"Drawing zerg decorations"< }
};


class CTerranDecorator : public CDecorator
{
public:

CTerranDecorator(IDecorator& decorator)
:CDecorator(decorator){}

protected:
void DrawDecoration()
{
cout<<"Drawing terran
decorations"< }
};

class CProtossDecorator : public
CDecorator
{
public:
CProtossDecorator(IDecorator& decorator)

:CDecorator(decorator){}
protected:
void DrawDecoration()
{

cout<<"Drawing protoss decorations"< }
};
posted on 2011-06-26 21:08  warmwar  阅读(167)  评论(0编辑  收藏  举报