The Decorator Pattern it used to alter or decorate portions of an existing object's content or functionality without modifying the structure of the original object.
Decorator Design Pattern examples
Scenario A in Drupal
The following example will add an outer wrapper to each of the body field item. We made a small decoration on the body field and we don't need to modify the body field structure.
Scenario B in Drupal
In the following example, we are trying to alter the Drupal user login form. We updated the submit button text from 'Log in' to 'Sign in' and we don't need to change the origin definition of the user login form.
Scenario A in PHP
We have a Blog class. And we can use method addEntry to add new entries and use method getEntryList to return an array of the entry objects.
Now, we want to add the index number before each of the entry title.
Bad solution: The following approach is going to modify the original structure of the class method. It works but it is not good practices.
Good solution: To create a decorator class to modify the contents.
To make small changes to content or functionality of existing objects without modifying their original class structure, we should use the Decorator Design pattern.