Creational Patterns
Patterns that decouple code from the concrete classes it instantiates.
Creational patterns control how objects are created, so that client code depends on abstractions rather than on new ConcreteClass(). The GoF catalogue contains five: Singleton, Factory Method, Abstract Factory, Builder and Prototype. The creational patterns comparison explains how they differ from one another and from the common “simple factory” idiom.
Articles
- Abstract FactoryCreate families of related objects that must be used together, without naming their concrete classes. A runnable Java 21 example builds SQL for PostgreSQL and MySQL from one client, plus how Abstract Factory differs from Factory Method, trade-offs and interview questions.
- Factory MethodLet subclasses decide which class to instantiate. A runnable Java 21 report-export example, a clear distinction from the 'simple factory' and static factory methods, UML, trade-offs and interview questions.
- BuilderConstruct complex objects step by step. Covers both the GoF Builder (director plus interchangeable builders) and the fluent Effective Java builder for immutable objects, with runnable Java 21 examples.
- PrototypeCreate new objects by copying a configured prototype instead of building them from scratch. Copy constructors versus Cloneable, shallow versus deep copies, a prototype registry, and a runnable Java 21 example that shows the shallow-copy bug.
- SingletonEnsure a class has one instance and a global access point — and why that is harder, and more harmful, than it looks. Enum singletons, the holder idiom, double-checked locking with volatile, testability problems and the dependency-injection alternative, with a runnable Java 21 example.