Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts

Wednesday, 24 November 2010

C++ example of State Design Pattern


The State pattern allows an object to change its behavior when its internal state changes. This pattern can be observed in a vending machine. Vending machines have states based on the inventory, amount of currency deposited, the ability to make change, the item selected, etc. When currency is deposited and a selection is made, a vending machine will either deliver a product and no change, deliver a product and change, deliver no product due to insufficient currency on deposit, or deliver no product due to inventory depletion.

The frequency of use of State Pattern is Medium but is very useful and frequently used Telecoms Protocols implementation.

Example as follows:


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//State is part of Behavioral Patterns
//Behavioral Patterns deal with dynamic interactions among societies of classes and objects
//State allows an object to alter its behavior when its internal state changes.
// The object will appear to change its class

//We will take an example Bank card where depending on the deposit the customers status changes


#include<iostream>
#include<string>
#include "main.h"


Account* State::GetAccount(void)
{

return
account_;
}

void
State::SetAccount(Account* account)
{

account_ = account;
}


double
State::GetBalance(void)
{

return
balance_;
}


void
State::SetBalance(double balance)
{

balance_ = balance;
}


string State::GetStateName(void)
{

return
stateName_;
}


RedState::RedState(State* state)
{

this
->balance_ = state->GetBalance();
this
->account_ = state->GetAccount();
Initialise();
}


void
RedState::Deposit(double amount)
{

balance_ += amount;
StateChangeCheck();
}


void
RedState::Withdraw(double amount)
{

double
newAmount = amount + serviceFee_;
if
(balance_ - newAmount < lowerLimit_)
cout<<"No funds available for withdrawal!"<<endl;
else

balance_ -= newAmount;
}


void
RedState::PayInterest()
{

//No interest is paid
}

void
RedState::StateChangeCheck()
{

if
(balance_ > upperLimit_)
{

account_->SetState(reinterpret_cast<State*>(new SilverState(this)));
delete this
;
return
;
}
}


void
RedState::Initialise()
{

stateName_ = "Red";
//Should come from a data source
interest_ = 0.0;
lowerLimit_ = -100.0;
upperLimit_ = 0.0;
serviceFee_ = 15.0;
}


SilverState::SilverState(State* state)
{

this
->balance_ = state->GetBalance();
this
->account_ = state->GetAccount();
Initialise();
}


SilverState::SilverState(double balance, Account* account)
{

this
->balance_ = balance;
this
->account_ = account;
Initialise();
}


void
SilverState::Deposit(double amount)
{

balance_ += amount;
StateChangeCheck();
}


void
SilverState::Withdraw(double amount)
{

balance_ -= amount;
StateChangeCheck();
}


void
SilverState::PayInterest()
{

balance_ = balance_ * interest_;
StateChangeCheck();
}


void
SilverState::StateChangeCheck()
{

if
(balance_ < lowerLimit_)
{

account_->SetState(reinterpret_cast<State*>(new RedState(this)));
delete this
;
return
;
}

else if
(balance_ > upperLimit_)
{

account_->SetState(reinterpret_cast<State*>(new GoldState(this)));
delete this
;
return
;
}
}


void
SilverState::Initialise()
{

stateName_ = "Silver";
//Should come from a data source
interest_ = 1.0;
lowerLimit_ = 0.0;
upperLimit_ = 1000.0;
}


GoldState::GoldState(State* state)
{

this
->balance_ = state->GetBalance();
this
->account_ = state->GetAccount();
Initialise();
}


void
GoldState::Deposit(double amount)
{

balance_ += amount;
StateChangeCheck();
}


void
GoldState::Withdraw(double amount)
{

balance_ -= amount;
StateChangeCheck();
}


void
GoldState::PayInterest()
{

balance_ = balance_ * interest_;
StateChangeCheck();
}


void
GoldState::StateChangeCheck()
{

if
(balance_ < 0.0)
{

account_->SetState(reinterpret_cast<State*>(new RedState(this)));
delete this
;
return
;
}

else if
(balance_ < lowerLimit_)
{

account_->SetState(reinterpret_cast<State*>(new SilverState(this)));
delete this
;
return
;
}

else if
(balance_ > upperLimit_)
{

cout<<"Your account is too big now. Please consider using Swiss banks"<<endl;
}
}


void
GoldState::Initialise()
{

stateName_ = "Gold";
//Should come from a data source
interest_ = 5.0;
lowerLimit_ = 1000.0;
upperLimit_ = 10000000.0;
}


Account::Account(string owner):owner_(owner)
{

state_ = reinterpret_cast<State*>(new SilverState(0.0, this)); //default
}

Account::~Account()
{

delete
state_;
}


double
Account::GetBalance(void)
{

return
state_->GetBalance();
}


void
Account::Deposit(double amount)
{

state_->Deposit(amount);
cout<<"Deposited $"<<amount<<endl;
cout<<"Balance $"<<GetBalance()<<endl;
cout<<"Status "<<state_->GetStateName()<<endl;
cout<<"\n";
}


void
Account::Withdraw(double amount)
{

state_->Withdraw(amount);
cout<<"Withdrew $"<<amount<<endl;
cout<<"Balance $"<<GetBalance()<<endl;
cout<<"Status "<<state_->GetStateName()<<endl;
cout<<"\n";
}


void
Account::PayInterest()
{

state_->PayInterest();
cout<<"Interest Paid --------"<<endl;
cout<<"Balance $"<<GetBalance()<<endl;
cout<<"Status "<<state_->GetStateName()<<endl;
cout<<"\n";
}


void
Account::SetState(State* state)
{

state_ = state;
}


State* Account::GetState(void)
{

return
state_;
}



//The Main method
int main()
{

Account* account = new Account("Dr. Who");
account->Withdraw(10.00);
account->Withdraw(30.00);
account->Withdraw(70.00);
account->Deposit(234.00);
account->Deposit(5000.00);
account->Withdraw(5200.00);
account->Deposit(1500.00);
account->Deposit(1.00);
account->Withdraw(1200.00);

delete
account;

return
0;
}


EDIT: main.h (added at a later date than the post)





using namespace
std;

//Forward Declaration
class Account;

// The 'State' abstract class
class State
{

public
:
Account* GetAccount(void);
void
SetAccount(Account* account);
double
GetBalance(void);
void
SetBalance(double balance);
string GetStateName(void);
virtual
void Deposit(double amount)=0;
virtual
void Withdraw(double amount)=0;
virtual
void PayInterest(void) = 0;
protected
:
Account* account_;
double
balance_;
double
interest_;
double
lowerLimit_;
double
upperLimit_;
string stateName_;;
};


// A 'ConcreteState' class
// Red indicates that account is overdrawn
class RedState : State
{

public
:
RedState(State* state);
void
Deposit(double amount);
void
Withdraw(double amount);
void
PayInterest();
void
StateChangeCheck();

private
:

RedState(); //Not allowed
void Initialise();
double
serviceFee_;
};


// A 'ConcreteState' class
// Silver indicates less interest bearing state
class SilverState : State
{

public
:
SilverState(State* state);
SilverState(double balance, Account* account);
void
Deposit(double amount);
void
Withdraw(double amount);
void
PayInterest();
void
StateChangeCheck();

private
:
SilverState(); //Not allowed
void Initialise();
};


// A 'ConcreteState' class
// Gold indicates high interest bearing state
class GoldState : State
{

public
:
GoldState(State* state);
void
Deposit(double amount);
void
Withdraw(double amount);
void
PayInterest();
void
StateChangeCheck();

private
:
GoldState(); //Not allowed
void Initialise();
};



// The 'Context' class - defined here as its used for forward declaration
class Account
{

public
:
Account(string owner);
~
Account();
double
GetBalance(void);
void
Deposit(double amount);
void
Withdraw(double amount);
void
PayInterest();
void
SetState(State* state);
State* GetState(void);
private
:
State* state_;
string owner_;
Account();
};




The output is as follows:

Further reading:

http://www.dofactory.com/Patterns/PatternState.aspx




Wednesday, 17 November 2010

C++ example of Observer Design Pattern

Definition: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Observers register themselves with the Subject as they are created. Whenever the Subject changes, it broadcasts to all registered Observers that it has changed.


The Observer defines a one-to-many relationship so that when one object changes state, the others are notified and updated automatically. Some auctions demonstrate this pattern. Each bidder possesses a numbered paddle that is used to indicate a bid. The auctioneer starts the bidding, and “observes” when a paddle is raised to accept the bid. The acceptance of the bid changes the bid price which is broadcast to all of the bidders in the form of a new bid.

Observer is a very popular pattern and its frequency of use is very high.

The following is an example of Observer Design Pattern:




//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Observer is part of Behavioral Patterns
//Behavioral Patterns deal with dynamic interactions among societies of classes and objects
//An Observer is a way of notifying change to a number of classes.

//We will take an example of Stock Price where, Observers can register to be told about
//the stock price change of a company

#include<iostream>
#include<string>
#include<list>

using namespace
std;


//Forward Declaration
class Stock;

// The 'Observer' interface
class IInvestor
{

public
:
virtual
void Update(Stock* stock){};
};


// The 'Subject' abstract class
class Stock
{

public
:
Stock(string symbol, double price) : symbol_(symbol), price_(price) { }
void
Attach(IInvestor* investor)
{

investors_.push_back(investor);
}

void
Detach(IInvestor* investor)
{

investors_.remove(investor);
}

void
Notify()
{

list<IInvestor*>::iterator it = investors_.begin();
while
(it != investors_.end())
{
(*
it)->Update(this);
++
it;
}
}

double
GetPrice(void)
{

return
price_;
}

void
SetPrice(double price)
{

price_ = price;
Notify();
}

string GetSymbol(void)
{

return
symbol_;
}


private
:
string symbol_;
double
price_;
list<IInvestor*> investors_;

Stock();
};


// The 'ConcreteSubject' class
class Company : public Stock
{

public
:
Company(string name, string symbol, double price) : name_(name), Stock(symbol, price) {}
string GetName(void)
{

return
name_;
}

private
:
string name_;
};


// The 'ConcreteObserver' class
class Investor : public IInvestor
{

public
:
Investor(string name) : name_(name){}
void
Update(Stock* stock)
{

cout<<"Notified "<<name_<<" about "<<(reinterpret_cast<Company*>(stock))->GetName() \
<<
" change to "<<stock->GetSymbol()<<stock->GetPrice()<<endl;
}

private
:
string name_;
Investor();
};


//The Main method
int main()
{

Company* c1 = new Company("Google", "$", 123.0);
cout<<"Created company Google with Stock Price 123.0\n"<<endl;

Investor* i1 = new Investor("Billy");
c1->Attach(i1);
cout<<"Created investor Billy following Google\n"<<endl;

c1->SetPrice(125.0);

Investor* i2 = new Investor("Timmy");
c1->Attach(i2);
Investor* i3 = new Investor("Lenny");
c1->Attach(i3);
cout<<"\nCreated investor Timmy and Lenny following Google\n"<<endl;

c1->SetPrice(145.0);

c1->Detach(i1);
c1->Detach(i3);
cout<<"\nInvestor Billy and Lenny not interested in Google anymore\n"<<endl;

c1->SetPrice(165.0);

delete
i1;
delete
i2;
delete
i3;
delete
c1;

return
0;
}



The output is as follows:
Further Reading:




Wednesday, 13 October 2010

C++ example of Proxy Design Pattern

The Proxy pattern is used when you need to represent a complex object by a simpler one. If creating an object is expensive in time or computer resources, Proxy allows you to postpone this creation until you need the actual object. A Proxy usually has the same methods as the object it represents, and once the object is loaded, it passes on the method calls from the Proxy to the actual object.

There are several cases where a Proxy can be useful:

1. If an object, such as a large image, takes a long time to load.
2. If the object is on a remote machine and loading it over the network may be slow, especially during peak network load periods.
3. If the object has limited access rights, the proxy can validate the access permissions for that user.

Proxies can also be used to distinguish between requesting an instance of an object and the actual need to access it. For example, program initialization may set up a number of objects which may not all be used right away. In that case, the proxy can load the real object only when it is needed.


The Proxy provides a surrogate or place holder to provide access to an object. A check or bank draft is a proxy for funds in an account. A check can be used in place of cash for making purchases and ultimately controls access to cash in the issuer’s account.



The frequency of use of Proxy is medium high

Lets look at the example below. Note that the actual Math class is created in the MathProxy when math operations need to be performed.



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Proxy is part of Structural Patterns
//Structural Patterns deal with decoupling the interface and implementation of classes and objects
//A Proxy provides a surrogate or placeholder for another object to control access to it.

//The example we consider here a math class
//The proxy provides an interface but the real class is only initiated when it is used

#include<iostream>
#include<string>

using namespace
std;


// The 'Subject interface
class IMath
{

public
:
virtual
double Add(double x, double y) = 0;
virtual
double Sub(double x, double y) = 0;
virtual
double Mul(double x, double y) = 0;
virtual
double Div(double x, double y) = 0;
};


// The 'RealSubject' class
class Math : public IMath
{

public
:
double
Add(double x, double y)
{

return
x + y;
}

double
Sub(double x, double y)
{

return
x - y;
}

double
Mul(double x, double y)
{

return
x * y;
}

double
Div(double x, double y)
{

return
x / y;
}
};


// The 'Proxy Object' class
class MathProxy : public IMath
{

public
:
MathProxy()
{

math_ = NULL;
}

virtual
~MathProxy()
{

if
(math_)
delete
math_;
}

double
Add(double x, double y)
{

return
getMathInstance()->Add(x, y);
}

double
Sub(double x, double y)
{

return
getMathInstance()->Sub(x, y);
}

double
Mul(double x, double y)
{

return
getMathInstance()->Mul(x, y);
}

double
Div(double x, double y)
{

return
getMathInstance()->Div(x, y);
}

private
:
Math* math_;
Math* getMathInstance(void)
{

if
(!math_)
math_ = new Math();
return
math_;
}
};


//The Main method
int main()
{

// Create math proxy
MathProxy proxy;

//Do the math
cout<<"4 + 2 = "<<proxy.Add(4, 2)<<endl;
cout<<"4 - 2 = "<<proxy.Sub(4, 2)<<endl;
cout<<"4 * 2 = "<<proxy.Mul(4, 2)<<endl;
cout<<"4 / 2 = "<<proxy.Div(4, 2)<<endl;

return
0;
}



The output is as follows:



For more information see:

http://sourcemaking.com/design_patterns/proxy

http://www.patterndepot.com/put/8/proxy.pdf

http://www.dofactory.com/Patterns/PatternProxy.aspx

Wednesday, 6 October 2010

C++ example of Flyweight Design Pattern

The Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive cost. Each “flyweight” object is divided into two pieces: the state-dependent (extrinsic) part, and the state-independent (intrinsic) part. Intrinsic state is stored (shared) in the Flyweight object. Extrinsic state is stored or computed by client objects, and passed to the Flyweight when its operations are invoked.


The Ant, Locust, and Cockroach classes can be “light-weight” because their instance-specific state has been de-encapsulated, or externalized, and must be supplied by the client.
The frequency of use of the flyweight pattern is low.
The following example illustrates the Flyweight pattern:




//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Flyweight is part of Structural Patterns
//Structural Patterns deal with decoupling the interface and implementation of classes and objects
//A Flyweight uses sharing to support large numbers of fine-grained objects efficiently.

//We will take an example of charachter class. Each charachter is unique and can have different size
//but the rest of the features will remain the same.

#include<iostream>
#include<string>
#include<map>

using namespace
std;

// The 'Flyweight' abstract class
class Character
{

public
:
virtual
void Display(int pointSize) = 0;

protected
:
char
symbol_;
int
width_;
int
height_;
int
ascent_;
int
descent_;
int
pointSize_;
};


// A 'ConcreteFlyweight' class
class CharacterA : public Character
{

public
:
CharacterA()
{

symbol_ = 'A';
width_ = 120;
height_ = 100;
ascent_ = 70;
descent_ = 0;
pointSize_ = 0; //initialise
}
void
Display(int pointSize)
{

pointSize_ = pointSize;
cout<<symbol_<<" (pointsize "<<pointSize_<<" )"<<endl;
}
};


// A 'ConcreteFlyweight' class
class CharacterB : public Character
{

public
:
CharacterB()
{

symbol_ = 'B';
width_ = 140;
height_ = 100;
ascent_ = 72;
descent_ = 0;
pointSize_ = 0; //initialise
}
void
Display(int pointSize)
{

pointSize_ = pointSize;
cout<<symbol_<<" (pointsize "<<pointSize_<<" )"<<endl;
}
};


//C, D, E,...

// A 'ConcreteFlyweight' class
class CharacterZ : public Character
{

public
:
CharacterZ()
{

symbol_ = 'Z';
width_ = 100;
height_ = 100;
ascent_ = 68;
descent_ = 0;
pointSize_ = 0; //initialise
}
void
Display(int pointSize)
{

pointSize_ = pointSize;
cout<<symbol_<<" (pointsize "<<pointSize_<<" )"<<endl;
}
};


// The 'FlyweightFactory' class
class CharacterFactory
{

public
:
virtual
~CharacterFactory()
{

while
(!characters_.empty())
{

map<char, Character*>::iterator it = characters_.begin();
delete
it->second;
characters_.erase(it);
}
}

Character* GetCharacter(char key)
{

Character* character = NULL;
if
(characters_.find(key) != characters_.end())
{

character = characters_[key];
}

else

{

switch
(key)
{

case
'A':
character = new CharacterA();
break
;
case
'B':
character = new CharacterB();
break
;
//...
case 'Z':
character = new CharacterZ();
break
;
default
:
cout<<"Not Implemented"<<endl;
throw
("Not Implemented");
}

characters_[key] = character;
}

return
character;
}

private
:
map<char, Character*> characters_;
};



//The Main method
int main()
{

string document = "AAZZBBZB";
const
char* chars = document.c_str();

CharacterFactory* factory = new CharacterFactory;

// extrinsic state
int pointSize = 10;

// For each character use a flyweight object
for(size_t i = 0; i < document.length(); i++)
{

pointSize++;
Character* character = factory->GetCharacter(chars[i]);
character->Display(pointSize);
}


//Clean memory
delete factory;

return
0;
}




Wednesday, 29 September 2010

C++ example for Facade Design Pattern



Frequently, as your programs evolve and develop, they grow in complexity. In fact, for all the excitement about using design patterns, these patterns sometimes generate so many classes that it is difficult to understand the program’s flow. Furthermore, there may be a number of complicated subsystems, each of which has its own complex interface.

The Façade pattern allows you to simplify this complexity by providing a simplified interface to these subsystems. This simplification may in some cases reduce the flexibility of the underlying classes, but usually provides all the function needed for all but the most sophisticated users. These users can still, of course, access the underlying classes and methods.

Facade takes a “riddle wrapped in an enigma shrouded in mystery”, and interjects a wrapper that tames the amorphous and inscrutable mass of software.

The frequency of use for Facade is very high.

C++ example as follows:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Façade is part of Structural Patterns
//Structural Patterns deal with decoupling the interface and implementation of classes and objects
//A Facade is single class that represents an entire subsystem

//The example we consider here is a case of a customer applying for mortgage
//The bank has to go through various checks to see if Mortgage can be approved for the customer
//The facade class goes through all these checks and returns if the morgage is approved

#include<iostream>
#include<string>

using namespace
std;

// Customer class
class Customer
{

public
:
Customer (const string& name) : name_(name){}
const
string& Name(void)
{

return
name_;
}

private
:
Customer(); //not allowed
string name_;
};


// The 'Subsystem ClassA' class
class Bank
{

public
:
bool
HasSufficientSavings(Customer c, int amount)
{

cout << "Check bank for " <<c.Name()<<endl;
return
true;
}
};


// The 'Subsystem ClassB' class
class Credit
{

public
:
bool
HasGoodCredit(Customer c, int amount)
{

cout << "Check credit for " <<c.Name()<<endl;
return
true;
}
};


// The 'Subsystem ClassC' class
class Loan
{

public
:
bool
HasGoodCredit(Customer c, int amount)
{

cout << "Check loans for " <<c.Name()<<endl;
return
true;
}
};


// The 'Facade' class
class Mortgage
{

public
:
bool
IsEligible(Customer cust, int amount)
{

cout << cust.Name() << " applies for a loan for $" << amount <<endl;
bool
eligible = true;

eligible = bank_.HasSufficientSavings(cust, amount);

if
(eligible)
eligible = loan_.HasGoodCredit(cust, amount);

if
(eligible)
eligible = credit_.HasGoodCredit(cust, amount);

return
eligible;
}


private
:
Bank bank_;
Loan loan_;
Credit credit_;
};


//The Main method
int main()
{

Mortgage mortgage;
Customer customer("Brad Pitt");

bool
eligible = mortgage.IsEligible(customer, 1500000);

cout << "\n" << customer.Name() << " has been " << (eligible ? "Approved" : "Rejected") << endl;

return
0;
}



The output is as follows:


More on Facade:

http://sourcemaking.com/design_patterns/facade

http://www.patterndepot.com/put/8/facade.pdf

http://sourcemaking.com/design_patterns/facade

Wednesday, 22 September 2010

C++ example for Decorator Design Pattern


The Decorator Pattern is used for adding additional functionality to a particular object as opposed to a class of objects. It is easy to add functionality to an entire class of objects by subclassing an object, but it is impossible to extend a single object this way. With the Decorator Pattern, you can add functionality to a single object and leave others like it unmodified.

A Decorator, also known as a Wrapper, is an object that has an interface identical to an object that it contains. Any calls that the decorator gets, it relays to the object that it contains, and adds its own functionality along the way, either before or after the call. This gives you a lot of flexibility, since you can change what the decorator does at runtime, as opposed to having the change be static and determined at compile time by subclassing. Since a Decorator complies with the interface that the object that it contains, the Decorator is indistinguishable from the object that it contains. That is, a Decorator is a concrete instance of the abstract class, and thus is indistinguishable from any other concrete instance, including other decorators. This can be used to great advantage, as you can recursively nest decorators without any other objects being able to tell the difference, allowing a near infinite amount of customization.

Decorators add the ability to dynamically alter the behavior of an object because a decorator can be added or removed from an object without the client realizing that anything changed. It is a good idea to use a Decorator in a situation where you want to change the behaviour of an object repeatedly (by adding and subtracting functionality) during runtime.

The dynamic behavior modification capability also means that decorators are useful for adapting objects to new situations without re-writing the original object's code.

The frequency for use of Decorator is medium. The following is example code for this pattern:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Decorator is part of Structural Patterns
//Structural Patterns deal with decoupling the interface and implementation of classes and objects
//The Decorator pattern provides us with a way to modify the behavior of individual objects without
// having to create a new derived class.

//The example here shows a Library that contains information about the books
//After the class was created, it was decided to have a borrowable functionality added

#include<iostream>
#include<string>
#include<list>

using namespace
std;

// The 'Component' abstract class
class LibraryItem
{

public
:
void
SetNumCopies(int value)
{

numCopies_ = value;
}

int
GetNumCopies(void)
{

return
numCopies_;
}

virtual
void Display(void)=0;
private
:
int
numCopies_;
};


// The 'ConcreteComponent' class#1
class Book : public LibraryItem
{

public
:
Book(string author, string title, int numCopies) : author_(author), title_(title)
{

SetNumCopies(numCopies);
}

void
Display(void)
{

cout<<"\nBook ------ "<<endl;
cout<<" Author : "<<author_<<endl;
cout<<" Title : "<<title_<<endl;
cout<<" # Copies : "<<GetNumCopies()<<endl;
}

private
:
Book(); //Default not allowed
string author_;
string title_;
};


// The 'ConcreteComponent' class#2
class Video : public LibraryItem
{

public
:
Video(string director, string title, int playTime, int numCopies) : director_(director), title_(title), playTime_(playTime)
{

SetNumCopies(numCopies);
}

void
Display(void)
{

cout<<"\nVideo ------ "<<endl;
cout<<" Director : "<<director_<<endl;
cout<<" Title : "<<title_<<endl;
cout<<" Play Time : "<<playTime_<<" mins"<<endl;
cout<<" # Copies : "<<GetNumCopies()<<endl;
}

private
:
Video(); //Default not allowed
string director_;
string title_;
int
playTime_;
};


// The 'Decorator' abstract class
class Decorator : public LibraryItem
{

public
:
Decorator(LibraryItem* libraryItem) : libraryItem_(libraryItem) {}
void
Display(void)
{

libraryItem_->Display();
}

int
GetNumCopies(void)
{

return
libraryItem_->GetNumCopies();
}

protected
:
LibraryItem* libraryItem_;
private
:
Decorator(); //not allowed
};

// The 'ConcreteDecorator' class
class Borrowable : public Decorator
{

public
:
Borrowable(LibraryItem* libraryItem) : Decorator(libraryItem) {}
void
BorrowItem(string name)
{

borrowers_.push_back(name);
}

void
ReturnItem(string name)
{

list<string>::iterator it = borrowers_.begin();
while
(it != borrowers_.end())
{

if
(*it == name)
{

borrowers_.erase(it);
break
;
}
++
it;
}
}

void
Display()
{

Decorator::Display();
int
size = (int)borrowers_.size();
cout<<" # Available Copies : "<<(Decorator::GetNumCopies() - size)<<endl;
list<string>::iterator it = borrowers_.begin();
while
(it != borrowers_.end())
{

cout<<" borrower: "<<*it<<endl;
++
it;
}
}

protected
:
list<string> borrowers_;
};


//The Main method
int main()
{

Book book("Erik Dahlman","3G evolution",6);
book.Display();

Video video("Peter Jackson", "The Lord of the Rings", 683, 24);
video.Display();

cout<<"Making video borrowable"<<endl;
Borrowable borrowvideo(&video);
borrowvideo.BorrowItem("Bill Gates");
borrowvideo.BorrowItem("Steve Jobs");
borrowvideo.Display();

return
0;
}



The output is as follows:


For more information see:

http://www.dofactory.com/Patterns/PatternDecorator.aspx

http://sourcemaking.com/design_patterns/decorator

http://www.patterndepot.com/put/8/Decorator.pdf

http://www.exciton.cs.rice.edu/javaresources/designpatterns/decoratorpattern.htm