Creational and Structural Patterns

Learn Singleton, Factory, Builder, Adapter, Decorator, Facade and Proxy: the problem each solves, a minimal implementation, and when not to use it.

Intermediate · about 8 hours · difficulty 3/5

Learn first: SOLID and Clean Design

Design patterns are named solutions to recurring design problems. You only need a handful for interviews, but you must know the problem each one solves, because interviewers ask "why" far more than "how".

Creational patterns (how objects get made)

  • Singleton: exactly one instance, such as a config or logger. Know the thread-safe version (double-checked locking or a static local in C++11) and the downsides: hidden global state and hard-to-test code.
  • Factory Method / Simple Factory: a function decides which concrete class to create (NotificationFactory.create("sms")), so callers do not depend on concrete types.
  • Abstract Factory: families of related objects (a light vs dark UI kit).
  • Builder: constructing objects with many optional fields step by step, avoiding constructors with ten parameters.

Structural patterns (how objects fit together)

  • Adapter: wrap an incompatible interface so it fits yours, such as adapting a third-party payment SDK.
  • Decorator: add behaviour by wrapping, such as adding toppings to a pizza or logging to a data source, without subclass explosion.
  • Facade: one simple entry point over a complicated subsystem.
  • Proxy: a stand-in that controls access, for lazy loading, caching or permission checks.
  • Composite (optional): treat trees of objects uniformly, like files and folders.

Pitfalls

  • Applying patterns because they exist. Name a pattern only when the problem it solves is actually present.
  • Confusing Decorator (adds behaviour, same interface) with Adapter (changes interface) and Proxy (controls access).

Good enough to move on: you can code Singleton, Factory, Builder and Decorator from scratch in your DSA language and give a real-world use for each.

Key concepts

  • Singleton and thread safety
  • Factory and Abstract Factory
  • Builder
  • Adapter
  • Decorator
  • Facade
  • Proxy
  • Composite
  • Prototype

Mini project

Build a notification library: a Factory that returns Email, SMS or Push senders, a Builder for Notification objects with optional fields, a Decorator that adds retry and logging to any sender, and a thread-safe Singleton config loader.

Check yourself

What is the difference between Adapter, Decorator and Proxy if all three wrap another object?

Free resources