GoF Decorator Pattern

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Componentized Decorator Pattern

PerfectJPattern's offers a componentized version of the Decorator pattern. In order to implement decorators and take advantage of PerfectJPattern's implementation, users are required to extend AbstractDecorator.

Implementing decorators using PerfectJPattern's AbstractDecorator offers the following advantages:

  • Decorator identity i.e. equals is automatically handled to match that of the Component, thus to the outside world the Decorator still is the Component assuming equals defines identity
  • Automatic decoration: decorating interfaces with large number of features in a snap. Those Component methods not defined by the Decorator will be automatically forwarded to the Component so you don't have to. Users are only required to provide implementation for those extra methods or decorated methods minimally.
  • Maintainability: the Component interface may change but the Decorators will be minimally or not impacted at all. In the traditional manual implementation, once the Component interface changed, all Decorator also had to be changed posing a high toll in maintainability.


UML Class Design




Example