Decorator | Cheat Sheet

Structural Pattern — Design Patterns Series

Itchimonji
CP Massive Programming
3 min readJun 29, 2021

--

For many frameworks the Decorator pattern is indispensable, e.g. for NestJS or .NetCore. In real life we can adapt this pattern with burger or pizza toppings, scrolling functionalities to a page, or notifications for different social platforms.

The Decorator is a dynamic extension of the functionality of an object with Object Composition and a flexible alternative to subclassing. It is not identical with its base component, but serves as an enclosure (Transparent Enclosure). In some reference books it is also known as Wrapper.

Real-life examples

  • Several layers of clothes for different situations
  • Pizza or burger toppings
  • Zooming or scrolling functionalities for a page
  • Notifications for different platforms

Meaning

Applicability (suitable if …)

  • Individual objects are supposed to be dynamically equipped with further functionality
  • Functionality is supposed to be added and removed dynamically
  • Function extension is not possible by subclassing — and if it is, this would lead to explosive numerical increase of subclasses (due to high combination rate)

Assets and Drawbacks

  • More flexibility than static inheritance by dynamically adding or removing functions as well as combining different Decorator objects
  • Avoidance of function overloaded classes, because functions can be added only when they are needed (by simple combination) — YAGNI Principle
  • Decorator object is not identical with its base component, but serves it as an enclosure (Transparent Enclosure)
  • System consists of many small objects, which limits the clear overview

Example

Let’s cook some burgers for this example. First of all, you need an interface and a base class for the burger.

model to decorate

After that, you can fill your menu with certain burgers.

specific burger models

For these base burgers you can implement some toppings now.

Decorator model

To extend the topping Decorator you can inherit.

Inherited Decorator model

The client can combine base burgers with different toppings dynamically and individually.

Client

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and serve as a significant introduction into object composition (besides inheritance) and dependency inversion.

Structural Patterns are indispensable when objects are combined to create new functionalities. Comprehensive structures are built by using compositions of classes and objects. This provides flexibility due to variable composition structure depending on the runtime. One thus achieves an increase in efficiency and consistency.

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links