Click here to Skip to main content
15,896,269 members
Articles / Programming Languages / C++

Learn Decorator Design Pattern in Easy Steps

Rate me:
Please Sign up or sign in to vote.
4.70/5 (16 votes)
5 Aug 2010CPOL6 min read 44.2K   217   31  
Decorator Design Pattern explained in easy steps.
#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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
10 years of extensive experience in full life-cycle of S\W development inlcuding Requirements, Design, Prototyping, Implementation, Verification, Release, Sustenance.

Comments and Discussions