Decorator design pattern in C++ with real life example

As you might know, there are different types of a design pattern are there but it is important to know that a decorator design pattern is a structural design pattern

Suppose you want to add new functionalities to the single object of the class but not the entire class dynamically in that case decorator design pattern will be helpful

Consider a simple example where user interface Car can let us add properties (with music system, with GPS navigator) or behaviours to any user interface component

Participants

  1. Component (Car)
  2. Concrete component(BMW, Audi)
  3. Decorator
  4. Concrete decorator( with music system, with GPS navigator)

Structure

Rendered by QuickLaTeX.com

#include <iostream>
#include <string>
using namespace std;
// Component
class Car
{
public:
    virtual string Fit_parts () = 0;
    virtual float Cost() = 0;
    virtual ~Car(){}
};

// Concrete Component
class Audi : public Car
{
public:
    string Fit_parts ()
    {
        return "Audi Car";
    }
    float Cost()
    {
        return 3000;
    }
};
class BMW : public Car
{
public:
    string Fit_parts ()
    {
        return "BMW Car";
    }
    float Cost()
    {
        return 6000;
    }
};
// Decorator
class CarDecorator: public Car
{
protected:
    Car *m_Car;
public:
    CarDecorator(Car *Audi): m_Car(Audi){}
    string Fit_parts ()
    {
        return m_Car->Fit_parts ();
    }
    float Cost()
    {
        return m_Car->Cost();
    }
};

// Concrete Decorator
class With_music_system: public CarDecorator
{
public:
   With_music_system(Car *audi): CarDecorator(audi){}
    string Fit_parts ()
    {
        return m_Car->Fit_parts () + " decorated with music system ";
    }
    float Cost()
    {
        return m_Car->Cost() + 400;
    }
};

class With_GPS_navigator: public CarDecorator
{
public:
    With_GPS_navigator(Car *audi): CarDecorator(audi){}
    string Fit_parts ()
    {
        return m_Car->Fit_parts () + " decorated With GPS navigator ";
    }
    float Cost()
    {
        return m_Car->Cost() + 800;
    }
};
int main()
{
  Car *audi = new Audi();
  cout << audi -> Fit_parts () << endl;
  cout << audi -> Cost() << endl;
  Car *bmw = new BMW();
  cout << bmw -> Fit_parts () << endl;
  cout << bmw -> Cost() << endl;
  Car *decoratedCar = new With_music_system(bmw);
  cout << decoratedCar -> Fit_parts () << endl;
  cout << decoratedCar -> Cost() << endl;
  decoratedCar = new With_GPS_navigator(audi);
  cout << decoratedCar -> Fit_parts () << endl;
  cout << decoratedCar -> Cost() << endl;

  return 0;
}

Output

Audi car
 3000
 BMW Car 6000
 BMW Car decorated with music system
 6400
 Audi Car decorated with GPS  navigator
3800

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.