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
Adapter

Also known as

Wrapper

Intent

The Adapter pattern converts the interface of a class into another interface that clients expect, enabling compatibility.

Explanation

Real-world example

Consider that you have some pictures on your memory card and you need to transfer them to your computer. To transfer them, you need some kind of adapter that is compatible with your computer ports so that you can attach a memory card to your computer. In this case card reader is an adapter.
Another example would be the famous power adapter; a three-legged plug can't be connected to a two-pronged outlet, it needs to use a power adapter that makes it compatible with the two-pronged outlets.
Yet another example would be a translator translating words spoken by one person to another


iluwatarAbout 3 minStructuralCompatibilityGang of FourIntegration
Bridge

Also known as

Handle/Body

Intent

Decouple an abstraction from its implementation so that the two can vary independently.

Explanation

Real-world example

Consider you have a weapon with different enchantments, and you are supposed to allow mixing
different weapons with different enchantments. What would you do? Create multiple copies of each
of the weapons for each of the enchantments or would you just create separate enchantment and set
it for the weapon as needed? Bridge pattern allows you to do the second.


iluwatarAbout 2 minStructuralGang 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 2 minCreationalGang of Four
Chain of responsibility

Intent

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to
handle the request. Chain the receiving objects and pass the request along the chain until an object
handles it.

Explanation

Real-world example

The Orc King gives loud orders to his army. The closest one to react is the commander, then
an officer, and then a soldier. The commander, officer, and soldier form a chain of responsibility.


iluwatarAbout 2 minBehavioralGang of Four
Command

Also known as

Action, Transaction

Intent

Encapsulate a request as an object, thereby letting you parameterize clients with different
requests, queue or log requests, and support undoable operations.

Explanation

Real-world example

There is a wizard casting spells on a goblin. The spells are executed on the goblin one by one.
The first spell shrinks the goblin and the second makes him invisible. Then the wizard reverses
the spells one by one. Each spell here is a command object that can be undone.


iluwatarAbout 3 minBehavioralGang of Four
Composite

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

Every sentence is composed of words which are in turn composed of characters. Each of these
objects are printable and they can have something printed before or after them like sentence
always ends with full stop and word always has space before it.


iluwatarAbout 2 minStructuralGang of Four
Decorator

Also known as

Wrapper

Intent

Attach additional responsibilities to an object dynamically. Decorators provide a flexible
alternative to subclassing for extending functionality.

Explanation

Real-world example

There is an angry troll living in the nearby hills. Usually, it goes bare-handed but sometimes it
has a weapon. To arm the troll it's not necessary to create a new troll but to decorate it
dynamically with a suitable weapon.


iluwatarAbout 2 minStructuralGang of FourExtensibility
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 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
2