Skip to main content
Abstract Factory

Also known as

Kit

Intent

The Abstract Factory design pattern provides a way to create families of related objects without specifying their concrete classes. This allows for code that is independent of the specific classes of objects it uses, promoting flexibility and maintainability.


iluwatarAbout 3 minCreationalAbstractionDecouplingGang of Four
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.


iluwatarAbout 3 minCreationalGang of Four
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


iluwatarAbout 2 minCreationalGang of Four
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.


iluwatarAbout 1 minCreationalExtensibility
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.


iluwatarAbout 2 minCreationalExtensibilityGang of Four
MonoState

Also known as

Borg

Intent

Monostate is an alternative approach to achieving a singleton-like behavior in object-oriented design. It enforces a unique behavior where all instances of a class share the same state. Unlike the Singleton pattern, which restricts a class to a single instance, Monostate allows for multiple instances but ensures they all have a shared state.


iluwatarAbout 3 minCreationalInstantiation
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.


iluwatarAbout 2 minCreationalInstantiation
Object Mother

Object Mother

It is used to define a factory of immutable content with separated builder and factory interfaces.

Class diagram

Object Mother

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

iluwatarAbout 2 minCreationalInstantiation
Object Pool

Also known as

Resource Pool

Intent

When objects are expensive to create and they are needed only for short periods of time it is
advantageous to utilize the Object Pool pattern. The Object Pool provides a cache for instantiated
objects tracking which ones are in use and which are available.


iluwatarAbout 1 minCreationalGame programmingPerformance
Property

Intent

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

Explanation

Real-world example

In the mystical land of "Elandria", adventurers can harness the power of ancient relics to customize their abilities. Each relic represents a unique property or skill. As adventurers explore, they discover and integrate new relics, dynamically enhancing their skills based on the relics they possess.


iluwatarAbout 1 minCreationalInstantiation