Creational Design Patterns
Creational design patterns solve common problems that arise during the creation
of objects.
Builder Pattern
The builder pattern provides an alternative to a constructor with an excessive
number of parameters. A builder object temporarily stores the state of a new
object through a chain of fluent method calls, until the target object
is constructed in one final step. The MapMaker class in the Google Guava
library is an example of the builder pattern.
Factory Pattern
The factory pattern provides an instance of an abstract class or an interface
without burdening the client with implementation details. This is useful for
encapsulating complicated creational logic or utilizing object pools for
performance. The Calendar#getInstance() method is an example of the
factory pattern.
Abstract Factory Pattern
The abstract factory pattern, also known as a factory of factories, provides
instances of factory classes without burdening the client with implementation
details. This is useful for encapsulating the creational logic of a set of related
factory classes. The DocumentBuilderFactory class is an example of the
abstract factory pattern.
Prototype Pattern
The prototype pattern creates a cloned object out of a prototype. This is useful
when an object is prohibitively expensive to create, such as the result
of a database query. The Object#clone() method is an example of the
prototype pattern.
Singleton Pattern
The singleton pattern restricts the instantiation of a class to a single instance.
Singletons are preferable to global variables because they can be lazily
initialized, although some implementations require explicit synchronization
to do so. The Runtime#getInstance() method is an example of the
singleton pattern.
Questions
What is the builder pattern? When is it useful?
What is the factory pattern? When is it useful?
What is the abstract factory pattern? When is it useful?
What is the prototype pattern? When is it useful?
What is the singleton pattern? When is it useful?
|