Decorator pattern dynamically attaches additional responsibility to an object. This pattern is used to extend functionality without affecting the existing implementation. If we just use inheritance, then we can extend the functionality of a class. But by using a decorator we can enhance/extend and/or change the functionality of an object at run time. This article demonstrates Decorator pattern implementation in PHP.
Decorator pattern adds new functionality to existing implementation, without changing the existing implementation. The additional functionality is added to individual objects (not to the entire class). This article demonstrates Decorator pattern implementations in TypeScript.
Decorator pattern add new functionality to an existing implementation by adding a new layer. The responsibility/functionality is added to individual objects. This article demonstrates Decorator pattern implementations in Go.
In Decorator design pattern, our target is to add some new functionality to an existing object. Say, there is an existing class that is responsible for printing a UI element (like a Button or Table). Later we need the ability to add a border or text color to that element. We can add new properties and functions to that existing class and introduce those properties. But that will violate the Open-Close principle of the SOLID principle.
Decorator pattern is used to attach a new functionality to an existing object, without changing the existing class. This article demonstrates Decorator pattern implementations in Java.