Decorator (aka Wrapper)

Intent

  • Attach additional responsibilities to an object dynamically.

  • Decorators provide a flexible alternative to subclassing for extending functionality.

Problem

  • How can responsibilities be added to an object dynamically?

  • How can the functionality of an object be extended at run-time?

Solution

  • Define separate Decorator objects that add responsibilities to an object.

  • Work through Decorator objects to extend the functionality of an object at run-time.

Advantages

✅ Provides a flexible alternative to subclassing. ✅ Allows an open-ended number of added functionalities.

Disadvantages

❌ Provides no reliability on object identity, due to transparent enclosure.

Notes

  • A nice article on decorator and how to use them for embellishment.

Last updated