Design Patterns

Decorator Pattern

The decorator pattern explained: wrap an object in layers that share its interface to add logging, checks or formatting at runtime, without editing the class.

  • 7 min read
  • 5 questions
  • +35 XP
Transcript

Decorator pattern explained by cats. Kitty love decorating. Kitty has a plain bed. She adds a bed sheet, then adds a blanket, then a pillow, then a comforter. Same bed, but cozier every time. You start with one thing, then wrap it with extra features step by step.

Each wrapper adds behavior or appearance without changing the core. In code, the bed is your base object. Each add -on, such as blanket, pillow, comforter, is a decorator class. Each decorator implements the same interface or inherits from the same abstract class.

This pattern lets you add new abilities at runtime, logging, styling, extra checks, without touching the original class. It allows you to upgrade without rewriting. It lets you wrap objects like Kitty wraps her bed. That's the decorator pattern.