|
#include "stdafx.h"
#pragma once
#include <iostream>
#include "Kelvin.hpp" // include the component
// Decorator needs to keep a reference to component to decorate it
//derive from Kelvin so that interface of decorator is same. This makes life easy on the client (caller) side
//this will be our base decorator class. We are adding new behavior to Kelvin thru composition
class KelvinDecorator: public Kelvin
{
public:
virtual ~KelvinDecorator()
{
delete kelvinComponent;
}
//ctor taking in component to decorate (i.e. Kelvin) as argument.
KelvinDecorator(Kelvin *kelvinArgument): kelvinComponent(kelvinArgument)
{
}
//overload common interface from Kelvin
virtual void MisBehave()
{
//just ask kelvin to misbehave!
kelvinComponent->MisBehave();
}
private:
//component to decorate
Kelvin *kelvinComponent;
//there is no KelvinDecorator without Kelvin, so hide default ctor
KelvinDecorator();
};
class SpacemanDecorator: public KelvinDecorator
{
public:
virtual ~SpacemanDecorator()
{
// std::cout << "Spaceman Dtor\n";
}
SpacemanDecorator (Kelvin *kelvin): KelvinDecorator(kelvin)
{
}
virtual void MisBehave()
{
// call previous decorator
KelvinDecorator::MisBehave();
//now add this decorator's additional functionality
std::cout<< "The fearless Spaceman Kpiff battles with the Zork!!!\n";
}
private:
SpacemanDecorator();
};
class DinoDecorator: public KelvinDecorator
{
public:
virtual ~DinoDecorator()
{
// std::cout << "Dino Dtor\n";
}
DinoDecorator (Kelvin *kelvin): KelvinDecorator(kelvin)
{
}
virtual void MisBehave()
{
// call previous decorator
KelvinDecorator::MisBehave();
//now add this decorator's additional functionality
std::cout<< "Kelvin the terrible T-Rex runs rampant in Jurassic park!\n";
}
private:
DinoDecorator();
};
class TracerBazookaDecorator: public KelvinDecorator
{
public:
virtual ~TracerBazookaDecorator()
{
//std::cout << "Tracer Dtor\n";
}
TracerBazookaDecorator (Kelvin *kelvin): KelvinDecorator(kelvin)
{
}
virtual void MisBehave()
{
// call previous decorator
KelvinDecorator::MisBehave();
//now add this decorator's additional functionality
std::cout<< "TracerBazooka: Here's looking at you kid.\n";
}
private:
TracerBazookaDecorator();
};
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
10 years of extensive experience in full life-cycle of S\W development inlcuding Requirements, Design, Prototyping, Implementation, Verification, Release, Sustenance.