Object creation control. When making an object is expensive, or the exact type isnât known until runtime, or you need exactly one instance, exactly one family of related instances, or a fast supply of near-identical copies, one of these six covers it.
- Singleton Guarantees exactly one instance exists, and makes the lazy double-checked construction safe when multiple threads race to create it first.
- Factory Method One method owns âwhich concrete class to build,â so every caller picks a type without duplicating the same if/else ladder.
- Abstract Factory Guarantees every object built through one factory belongs to the same family, so you canât accidentally mix a Windows audio player with a VLC video player.
- Builder Splits required fields, enforced in the constructor, from optional ones set via chained calls, so you skip telescoping constructors and half-built objects.
- Prototype Clones existing instances instead of rebuilding from scratch, using copy constructors instead of Javaâs Cloneable.
- Object Pool Reuses a fixed set of expensive objects instead of constructing and discarding a fresh one per request.
Comments