Skip to main content
Composite

Also known as

  • Object Tree
  • Composite Structure

Intent

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

Explanation

Real-world example


iluwatarAbout 3 minStructuralGang of FourObject compositionRecursion
Extension Objects

Also known as

  • Interface Extensions

Intent

The Extension Objects pattern allows for the flexible extension of an object's behavior without modifying its structure, by attaching additional objects that can dynamically add new functionality.

Explanation


iluwatarAbout 3 minStructuralEncapsulationExtensibilityObject compositionPolymorphism
Facade

Intent

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Explanation

Real-world example

How does a goldmine work? "Well, the miners go down there and dig gold!" you say. That is what you believe because you are using a simple interface that goldmine provides on the outside, internally it has to do a lot of stuff to make it happen. This simple interface to the complex subsystem is a facade.


iluwatarAbout 3 minStructuralCode simplificationEncapsulationGang Of FourObject composition
Factory Kit

Also known as

  • Object Kit
  • Toolkit

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 2 minCreationalDecouplingEncapsulationGenericInstantiationObject composition
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 minCreationalEncapsulationGang of FourInstantiationObject compositionPolymorphism
Fluent Interface

Also known as

  • Fluent API
  • Method Chaining

Intent

A fluent interface provides an easy-readable, flowing interface, that often mimics a domain specific language. Using this pattern results in code that can be read nearly as human language.

Explanation


iluwatarAbout 3 minBehavioralAPI designCode simplificationDecouplingObject compositionReactive
Flyweight

Intent

The Flyweight pattern's primary intent is to reduce the number of objects created, decrease memory footprint and increase performance by sharing as much data as possible with similar objects.

Explanation

Real-world example

Alchemist's shop has shelves full of magic potions. Many of the potions are the same so there is no need to create a new object for each of them. Instead, one object instance can represent multiple shelf items so the memory footprint remains small.


iluwatarAbout 3 minStructuralGang of FourMemory managementObject compositionOptimizationPerformance