PrepZone Logo
PrepZone

Decorator Pattern

Add responsibilities dynamically without subclass explosion.

Read these first

Why this matters

  • Wrapper chain — detailed pointwise breakdown in the section below.
  • Coffee shop example — detailed pointwise breakdown in the section below.
  • I/O decorators — detailed pointwise breakdown in the section below.
  • vs inheritance — detailed pointwise breakdown in the section below.
  • Patterns are forces and solutions — name the force before the pattern.
Wrapped component
LoggingDecorator
RetryDecorator
CoreService
Wrap behaviour in layers without subclass explosion.

Overview

Add responsibilities dynamically without subclass explosion. Study each section below in order — every list is interview-ready reference material.

Wrapper chain

Points to cover

  • Definition — what Wrapper chain means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design

Coffee shop example

Points to cover

  • Definition — what Coffee shop example means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design

I/O decorators

Points to cover

  • Definition — what I/O decorators means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design

vs inheritance

Points to cover

  • Definition — what vs inheritance means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design
Java
// CampusOS — apply "Wrapper chain" on paper first, then in code
public final class DecoratorPatternExample {
    // 1. Name entities from the problem statement
    // 2. Draw relationships before writing methods
    // 3. Introduce patterns only when a force appears
}

Quick recall

Everything you need if you only revisit this box.

  1. Wrapper chain — review this slice before mock interviews.
  2. Coffee shop example — review this slice before mock interviews.
  3. I/O decorators — review this slice before mock interviews.
  4. vs inheritance — review this slice before mock interviews.

Test yourself

Answer these before moving on — recall is what makes it stick.