Skip to content

Latest commit

 

History

History
26 lines (21 loc) · 1.7 KB

patterns.md

File metadata and controls

26 lines (21 loc) · 1.7 KB

Design Patterns

Exercises: AppConfig (Singleton), Pizza (Builder), EventBus (Observer) · Tests: matching *Test classes Run: ./run.sh patterns

The ones interviewers actually ask

  • Singleton: one instance. Best options: enum singleton, initialization-on-demand holder, or DI container scope. Naive lazy init is a race; double-checked locking needs volatile. Beware reflection and serialization breaking it.
  • Factory (Method / Abstract): hide new behind a method/interface so callers depend on abstractions.
  • Builder: many optional params, immutability, validation in build(). Alternative to telescoping constructors.
  • Strategy: swap an algorithm at runtime (in Java: often just a lambda/Comparator).
  • Observer: one-to-many notification (EventBus, listeners). Watch memory leaks (forgotten unsubscribe) and exceptions in listeners.
  • Decorator: wrap to add behaviour (BufferedInputStream(new FileInputStream(...))).
  • Adapter, Proxy, Template Method, Command, State are also fair game.

SOLID in one line each

  • Single responsibility · Open/closed · Liskov substitution · Interface segregation · Dependency inversion.

Common interview questions

  1. Implement a thread-safe Singleton. Why is volatile needed in DCL?
  2. Factory vs Abstract Factory vs Builder?
  3. Where does the JDK use Observer, Decorator, Factory, Iterator, Strategy? (java.io, Comparator, Collections, Calendar.getInstance)
  4. Inheritance vs composition: why "favour composition"?

Links