Design Pattern: Decorator Pattern in Python

Decorator pattern is used to add some additional responsibilities to an object dynamically. We can add some new tasks, checking, etc. before and/or after the original functionality of the the object. Using the Decorator pattern we can extend the behavior, with property transparency, and without affecting other objects. In this article, we discuss the implementation of the Decorator Pattern in Python.

Design Pattern: Decorator Pattern in PHP

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.

Design Pattern: Decorator Pattern

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.