Decorator: Introduction

Get a brief introduction to the Decorator design pattern.

The Decorator design pattern allows developers to modify the functionality of existing objects without modifying the objects themselves. Just like the Adapter pattern, the Decorator pattern provides a wrapper for an existing object. However, unlike the Adapter pattern, it implements exactly the same interface as the original object. This allows us to apply additional processing steps to the original API without modifying its original behavior.

Summarized concept of the Decorator pattern

The Decorator pattern can be summarized as follows:

  • There’s a class, which we’ll refer to as Original Object.
  • Original Object implements Service Interface.
  • There’s a Decorator class that also implements Service Interface.
  • Because the Decorator class acts as a wrapper for Service Interface, any implementation of Service Interface can be used, including another Decorator.
  • A Decorator can add additional functionality to any public members of its internal Service Interface implementation, such as preprocessing or post-processing steps.

Get hands-on with 1200+ tech skills courses.