17

Can someone give me an example of the Decorator design pattern in C++ ? I have come across the Java version of it, but found it difficult to understand the C++ version of it (from the examples I found).

Thanks.

4 Answers 4

14

Vince Huston Design Patterns, even though its layout is poor, has C++ implementation for most design patterns in the Gang of Four book.

Click for Decorator.

There isn't much difference with Java, except the manual memory handling that you'd better wrap with smart pointers :)

2
  • +1 for this website.I always refer to this for cpp implementation.I also think that the messy memory management is the reason you don't see this pattern much in cpp. Commented Jun 7, 2010 at 8:53
  • I don't always refer to the implementation, but I appreciate not being spammed by ads and the advices on when to apply, when not to and what it brings in comparison to other related patterns. Commented Jun 7, 2010 at 9:10
11

I've found the website Sourcemaking to be a pretty good one when it comes to explaining different Design Patterns.

The Decorator design pattern has C++ examples, such as an overview example, a "before and after", and an example with packet encoding/decoding.

2
  • +1 for the great answer. I checked the second example provided there (sourcemaking.com/design_patterns/decorator/cpp/2) - isn't it missing a virtual destructor in the interface class (the Widget class) ? Commented Jan 19, 2018 at 14:07
  • 1
    @GuyAvraham - thanks. You are correct, the base class(es) should have virtual destructors as a general "correct practice". However, in this example, their code will work fine since a) they aren't deleting any objects through a base class pointer (they aren't deleting them at all?!?!? They are just returning / exiting, kind of sloppy...) and b) the constructors of the derived objects' don't allocate any resources / open any files / etc., so technically, not calling their destructors won't cause problems... TODAY. But you are right -- as the code grows, this could change. Good catch!
    – Dan
    Commented Jan 21, 2018 at 3:32
2
#include <iostream>
using namespace std;

class Computer
{
public:
    virtual void display()
    {
        cout << "I am a computer..." << endl;
    }
};

class CDDrive : public Computer
{
private:
    Computer* c;
public:
    CDDrive(Computer* _c)
    {
        c = _c;
    }
    void display()
    {
        c->display();
        cout << "with a CD Drive..." << endl;
    }
};

class Printer : public Computer
{
private:
    CDDrive* d;
public:
    Printer(CDDrive* _d)
    {
        d = _d;
    }
    void display()
    {
        d->display();
        cout << "with a printer..." << endl;
    }
};

int main()
{
    Computer* c = new Computer();
    CDDrive* d = new CDDrive(c);
    Printer* p = new Printer(d);

    p->display();
}
1
  • 8
    this is a nice solution to a different problem, but this is not a "truly" decorator pattern, this design is missing the decorator class that is inherited by any decorator in this case printer and CD drive Commented Mar 2, 2014 at 9:51
0

c++ decorator pattern, plantuml

I'm learning c++ and design pattern recently, I'll give an example from <Head First Design Pattern>.

the main.cpp is here:

#include <iostream>

#include "Decaf.h"
#include "Espresso.h"
#include "MochaDecorator.h"
#include "MilkDecorator.h"

using std::cout;
using std::endl;


int main(){
    cout << "---------------------------" << endl;
   
    // only Decaf, no addsOn
    Beverage* decafOnly = new Decaf();
    cout << decafOnly->getDescription() << endl;
    cout << decafOnly->cost() << endl;
    delete decafOnly;
    cout << "---------------------------" << endl;

    // Decaf + Mocha
    Beverage* decaf = new Decaf();
    MochaDecorator mocha_decaf = MochaDecorator(decaf);
    cout << mocha_decaf.getDescription() << endl;
    cout << mocha_decaf.cost() << endl;
    delete decaf;
    cout << "---------------------------" << endl;

    // Espresso + Milk
    Beverage* espresso = new Espresso();
    MilkDecorator milk_espresso = MilkDecorator(espresso);
    cout << milk_espresso.getDescription() << endl;
    cout << milk_espresso.cost() << endl;
    delete espresso;
    cout << "---------------------------" << endl;

    // Note: decorators can wrap not only components but the other decorators as well

    // Esresso + Mocha + Milk + Milk,  method 1-------------------------------
    // Beverage* espresso2 = new Espresso();                               // Espresso
    // MochaDecorator* mocha_espresso = new MochaDecorator(espresso2);     // Espresso + Mocha
    // MilkDecorator* milk_deco = new MilkDecorator(mocha_espresso);       // Espresso + Mocha + Milk
    // MilkDecorator* milk_deco2 = new MilkDecorator(milk_deco);           // Espresso + Mocha + Milk + Milk
    // cout << milk_deco2->getDescription() << endl;
    // cout << milk_deco2->cost() << endl;
    // delete espresso2;
    // delete mocha_espresso;
    // delete milk_deco;
    // delete milk_deco2;

    // Esresso + Mocha + Milk + Milk,  method 2------------------------------
    Beverage* espresso2 = new MilkDecorator(new MilkDecorator(new MochaDecorator(new Espresso())));
    cout << espresso2->getDescription() << endl;
    cout << espresso2->cost() << endl;
    delete espresso2;
 
    
    cout << "---------------------------" << endl;
    return 0;
}

other files please see my github:

https://github.com/jwbecalm/Head-First-Design-Patterns-in-CPP/tree/main/ch03_Decorator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.