Design Patterns

Reusable solutions to common design problems, grouped into creational, structural and behavioural patterns.

Cheat sheet

  • ▸Creational: Singleton, Factory, Abstract Factory, Builder, Prototype.
  • ▸Structural: Adapter, Decorator, Facade, Proxy, Composite.
  • ▸Behavioural: Observer, Strategy, Command, Iterator, State, Template Method.
  • ▸Singleton: private constructor + static instance (thread-safe via double-checked locking or enum).
  • ▸Observer: subject notifies subscribers (event listeners, pub/sub).
  • ▸Builder: step-by-step construction of complex objects.

Pattern → real example

PatternCategoryReal example
SingletonCreationalLogger, config
FactoryCreationalPayment method creator
AdapterStructuralCharger adapter / legacy API wrapper
DecoratorStructuralJava I/O streams
ObserverBehaviouralYouTube subscriptions
StrategyBehaviouralSorting / discount rules

Visual

class Logger {
  private static volatile Logger inst;
  private Logger() {}
  static Logger get() {
    if (inst == null) synchronized(Logger.class) {
      if (inst == null) inst = new Logger(); }
    return inst; } }

Top 10 campus interview questions

Quick quiz

1. Ensuring only one instance exists:

2. Java BufferedReader wrapping FileReader is:

3. Pub/sub notification follows:

4. Observer is a ___ pattern:

5. Avoiding constructors with many params: