# Decorator Pattern

The decorator pattern enables us to add new functionality to an existing class, without changing its structure. Similar to proxy pattern, the decorator pattern is also a structural pattern, where the decorator class acts as a wrapper for the existing class.

Generally, inheritance is also used to extend the functionality of a class. But as the extended functionality increases, the number of derived classes will expand as well. Therefore, the decorator pattern is more flexible.

Let's look at the following example, where we have an abstract class _Car_ and three brands of cars that implement it and override _show()_ method.

```cpp
class Car {
public:
    virtual void show() = 0;
};
​
class Bmw : public Car {
public:
    void show() {
        cout << "This is a BMW" << endl;
    }
};
​
class Audi : public Car {
public:
    void show() {
        cout << "This is an Audi" << endl;
    }
};
​
class Benz : public Car {
public:
    void show() {
        cout << "This is a Benz" << endl;
    }
};
```

Now suppose we want to add new features to these cars. Instead of using inheritance, we can define a decorator class instead. Here we have a class _CarDecorator1_ which inherits _Car_. The class has a _Car_ object as its member. The class overrides _show()_, and besides calling the _show()_ method of the _Car_ object, it implements its own functionality here. This pattern is similar to the proxy pattern, but it actively takes a _Car_ object as a parameter in its constructor. We can also write _CarDecorator2_ and _CarDecorator3_ which implements other features in a similar way.

```cpp
class CarDecorator1 : public Car {
public:
    CarDecorator1(Car *p) : pCar(p) {}
    ~CarDecorator1() {
        delete pCar;
    }
    void show() {
        pCar->show();
        cout << "Feature 1" << endl;
    }
private:
    Car *pCar;
};
```

To use the decorator, we create a decorator object, and pass in the _Car_ object to be decorated. In this way, different brand of cars can have different features without modifying the original class.

```cpp
int main() {
    Car *p1 = new CarDecorator1(new Bmw());
    Car *p2 = new CarDecorator2(new Audi());
    Car *p3 = new CarDecorator3(new Benz());
    p1->show();
    p2->show();
    p3->show();
    delete p1;
    delete p2;
    delete p3;
    return 0;
}
```

The above code has the following output:

```
This is a Bwm
Feature 1
This is an Audi
Feature 2
This is a Benz
Feature 3
```

Moreover, a single object can also have multiple features: just use the decorator in a nested way by keep passing the same pointer inside:

```cpp
int main() {
    Car *p1 = new CarDecorator1(new Bmw());
    p1 = new CarDecorator2(p1);
    p1 = new CarDecorator3(p1);
    p1->show();
    delete p1;
    return 0;
}
```

The above code has the following output:

```
This is a Bwm
Feature 1
Feature 2
Feature 3
```
