Abstract Factory

Also known as

Kit

Intent

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

Explanation

Real-world example

To create a kingdom we need objects with a common theme. The elven kingdom needs an elven king, elven castle, and elven army whereas the orcish kingdom needs an orcish king, orcish castle, and orcish army. There is a dependency between the objects in the kingdom.


iluwatarCreationalGang of FourAbout 3 min
Builder

Intent

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

Explanation

Real-world example

Imagine a character generator for a role-playing game. The easiest option is to let the computer create the character for you. If you want to manually select the character details like profession, gender, hair color, etc. the character generation becomes a step-by-step process that completes when all the selections are ready.


iluwatarCreationalGang of FourAbout 2 min
Context object

Name / classification

Context Object

Also known as

Context, Encapsulate Context

Intent

Decouple data from protocol-specific classes and store the scoped data in an object independent of the underlying protocol technology.

Explanation

Real-world example


iluwatarCreationalAbout 2 min
Converter

Intent

The purpose of the Converter pattern is to provide a generic, common way of bidirectional conversion between corresponding types, allowing a clean implementation in which the types do not need to be aware of each other. Moreover, the Converter pattern introduces bidirectional collection mapping, reducing a boilerplate code to minimum.


iluwatarCreationalDecouplingAbout 1 min
Dependency Injection

Intent

Dependency Injection is a software design pattern in which one or more dependencies (or services) are injected, or passed by reference, into a dependent object (or client) and are made part of the client's state. The pattern separates the creation of a client's dependencies from its own behavior, which allows program designs to be loosely coupled and to follow the inversion of control and single responsibility principles.


iluwatarCreationalDecouplingAbout 1 min
Factory

Also known as

  • Simple Factory
  • Static Factory Method

Intent

Providing a static method encapsulated in a class called the factory, to hide the implementation logic and make client code focus on usage rather than initializing new objects.

Explanation


iluwatarCreationalGang of FourAbout 2 min
Factory Kit

Also Known As

Abstract-Factory

Intent

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

Explanation

Real-world example

Imagine a magical weapon factory that can create any type of weapon wished for. When the factory is unboxed, the master recites the weapon types needed to prepare it. After that, any of those weapon types can be summoned in an instant.


iluwatarCreationalExtensibilityAbout 1 min
Factory Method

Also known as

Virtual Constructor

Intent

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

Explanation

Real-world example

Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons. Depending on the customer at hand the right type of blacksmith is summoned.


iluwatarCreationalExtensibilityGang of FourAbout 2 min
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
2