Skip to content

1. Creational Patterns

Ercan Polat edited this page Jan 11, 2019 · 1 revision

Welcome to the Design Patterns wiki!

1. Creational Patterns:

1. Abstract Factory

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.


Use the Abstract Factory pattern when…

  • A system should be independent of how its products are created, composed, and represented.
  • A system can be configured with one of multiple families of products.
  • The constraint requiring products from the same factory to be used together must be enforced.
  • The emphasis is on revealing interfaces, not implementations.

2. Builder

Separate the construction of a complex object from its representation so that the same construction process can create different representations.


Use the Builder pattern when…

  • The algorithm for creating parts is independent from the parts themselves.
  • The object to be assembled might have different representations.
  • You need fine control over the construction process.

3. Factory Method

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.


Use the Factory Method pattern when…

  • Flexibility is important.
  • Objects can be extended in subclasses
  • There is a specific reason why one subclass would be chosen over another—this logic forms part of the Factory Method.
  • A client delegates responsibilities to subclasses in parallel hierarchies. Consider using instead....
  • The Abstract Factory, Prototype, or Builder patterns, which are more flexible (though also more complex).
  • The Prototype pattern to store a set of objects to clone from the abstract factory.

4. Prototype

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.


Use the Prototype pattern when… You want to:

  • Hide concrete classes from the client.
  • Add and remove new classes (via prototypes) at runtime.
  • Keep the number of classes in the system to a minimum.
  • Adapt to changing structures of data at runtime.

5. Singleton

Ensure a class has only one instance and provide a global point of access to it.


Use the Singleton pattern when …

  • You need to ensure there is only one instance of a class.
  • Controlled access to that instance is essential.
  • You might need more than one instance at a later stage.
  • The control should be localized in the instantiated class, not in some other mechanism.