Creational patterns answer "how do I build this object?" Reach for one when construction is non-trivial: it varies by type, has many optional parameters, or must be controlled. (For full theory, see the OOP module's creational patterns topic.)
Quick map
| Pattern | LLD trigger phrase | Reach for it when |
|---|---|---|
| Factory Method | "create a vehicle/spot/notification by type" | One product hierarchy; type decided at runtime |
| Abstract Factory | "create a family of related objects (UI for Mac vs Windows)" | Multiple related products that must stay consistent |
| Builder | "an object with many optional fields / step-by-step construction" | Telescoping constructors; immutable objects |
| Singleton | "one shared manager/registry/config for the whole system" | Exactly one instance, globally reachable |
Factory Method — type → object
Centralizes creation so adding a new type touches one place, not every new.
class VehicleFactory {
static Vehicle create(VehicleType type, String plate) {
return switch (type) {
case CAR -> new Car(plate);
case MOTORCYCLE -> new Motorcycle(plate);
case TRUCK -> new Truck(plate);
};
}
}
Abstract Factory — family → consistent set
When you must produce a whole family that has to match. The trigger is "related objects that vary together."
interface UiFactory { Button button(); Checkbox checkbox(); }
class MacFactory implements UiFactory {
public Button button() { return new MacButton(); }
public Checkbox checkbox() { return new MacCheckbox(); }
} // WindowsFactory returns Windows widgets — never a mix
Builder — many optional fields
Kills telescoping constructors and pairs perfectly with immutability.
Pizza p = new Pizza.Builder("medium")
.cheese(true).pepperoni(true).mushroom(false)
.build();
Singleton — exactly one instance
Common for a ParkingLot, Logger, or config registry. The trigger is "one shared X." Be ready for the thread-safety follow-up.
enum ParkingLot { // enum = simplest thread-safe singleton
INSTANCE;
// fields + methods here
}