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.
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.
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.
The above code has the following output:
Moreover, a single object can also have multiple features: just use the decorator in a nested way by keep passing the same pointer inside:
The above code has the following output:
Last updated