MonoState

Also known as

Borg

Intent

Enforces a behaviour like sharing the same state amongst all instances.

Class diagram

Applicability

Use the Monostate pattern when

  • The same state must be shared across all instances of a class.
  • Typically this pattern might be used everywhere a Singleton might be used. Singleton usage however is not transparent, Monostate usage is.
  • Monostate has one major advantage over singleton. The subclasses might decorate the shared state as they wish and hence can provide dynamically different behaviour than the base class.

iluwatarCreationalInstantiationLess than 1 minute
Multiton

Also known as

Registry

Intent

Ensure a class only has a limited number of instances and provide a global point of access to them.

Explanation

Real-world example

The Nazgûl, also called ringwraiths or the Nine Riders, are Sauron's most terrible servants. By definition, there's always nine of them.


iluwatarCreationalInstantiationAbout 2 min
Object Mother

Object Mother

Define a factory of immutable content with separated builder and factory interfaces.

Class diagram

Applicability

Use the Object Mother pattern when

  • You want consistent objects over several tests
  • You want to reduce code for creation of objects in tests
  • Every test should run with fresh data

iluwatarCreationalInstantiationLess than 1 minute
Property

Intent

Create hierarchy of objects and new objects using already existing objects as parents.

Class diagram

Applicability

Use the Property pattern when

  • When you like to have objects with dynamic set of fields and prototype inheritance

iluwatarCreationalInstantiationLess than 1 minute
Prototype

Intent

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

Explanation

First, it should be noted that the Prototype pattern is not used to gain performance benefits. It's only used for creating new objects from prototype instances.


iluwatarCreationalGang Of FourInstantiationAbout 2 min
Registry

Intent

Stores the objects of a single class and provide a global point of access to them. Similar to Multiton pattern, only difference is that in a registry there is no restriction on the number of objects.

Explanation

In Plain Words

Registry is a well-known object that other objects can use to find common objects and services.


iluwatarCreationalInstantiationLess than 1 minute
Step Builder

Intent

An extension of the Builder pattern that fully guides the user through the creation of the object with no chances of confusion. The user experience will be much more improved by the fact that he will only see the next step methods available, NO build method until is the right time to build the object.


iluwatarCreationalInstantiationLess than 1 minute
Value Object

Intent

Provide objects which follow value semantics rather than reference semantics. This means value objects' equality is not based on identity. Two value objects are equal when they have the same value, not necessarily being the same object.

Explanation

Real-world example


iluwatarCreationalInstantiationAbout 1 min