Creational patterns in LLD context
Creational patterns answer: who constructs this object, and how do we hide the complexity? In interviews, you rarely name the pattern — you reach for it when constructors explode or construction logic scatters across callers.
| Pattern | Use when | Skip when |
|---|---|---|
| Factory Method | Subclass decides which product to create (DocumentParser.create()) | One product type, trivial new |
| Abstract Factory | Families of related products (UI theme: Button + Checkbox) | Single product, no family constraint |
| Builder | Many optional fields, stepwise validation (HttpRequest, PizzaOrder) | ≤ 4 required fields |
| Singleton | Truly one instance in the process (config loader) | Testability, DI, or "global state" smell |
Factory vs Builder
Factory encapsulates which concrete type to instantiate. Builder encapsulates how to assemble a complex immutable object. They compose: HttpClient client = HttpClientBuilder.create().timeout(30).build(); — Builder constructs, Factory may select the builder implementation.
Singleton controversy
Singleton is a global variable with a dress code. Problems: hidden dependencies, hard to mock, lifecycle tied to class loader. Prefer dependency injection of a single shared instance (Spring @Bean, manual constructor injection). If you must use Singleton in Java, enum singleton (Effective Java Item 3) is the safest variant.
Object pool awareness
Object Pool (less common in LLD) reuses expensive instances (DB connections). Mention only if interviewer brings up connection limits — otherwise Factory + DI covers 95% of creational needs in loops.
Senior-level signal
Say: "I'd inject a VehicleFactory so ParkingLot doesn't know about Car vs Truck — that keeps OCP intact and makes tests pass a MockVehicleFactory." For Builder, walk through why telescoping constructors fail: parameter order ambiguity and invalid partial states.
Where this goes next
Structural Patterns covers wrapping and layering — Adapter, Decorator, Facade, and Proxy — the patterns you use once objects exist and need to interact.
Further Reading
- Head First Design Patterns — Ch. 4: Factory Method & Ch. 5: One-of-a-Kind Objects (Singleton pitfalls)Book45m
- Refactoring.Guru — Factory Method, Abstract Factory, Builder, Singleton (one page each — diagrams + when to use)Reference30m
- Effective Java — Item 3: Enforce the singleton property with a private constructor or enum typeBook15m
Practice Tasks (Optional)
Design or implement locally in any language — no autograding. Focus on class structure, extensibility, and being able to explain trade-offs out loud.
- Design a Builder for a complex object20m
Pick HttpRequest, PizzaOrder, or DatabaseConfig. Write the Builder API (method chaining) on paper with 5+ optional fields. Explain why constructor telescoping fails here.