Design Patterns Cheat Sheet
Design Patterns Cheat Sheet
Factory Method
Design
Patterns
CHEAT SHEET
Abstract Factory
KISS, DRY
What’s a Design Pattern? Creational Patterns
Categories of Design
Analogy
Patterns
The LoggerService class, using the Singleton pattern,
ensures efficient memory use in a project by creating
Creational Patterns a single, globally accessible instance for logging
information, warnings, and errors, thus preventing
Creational design patterns simplify object out-of-memory errors.
creation in software design, allowing for
flexibility and code reuse. By providing
mechanisms to handle varying object
nature, these patterns reduce complexity
and enhance the overall design.
Structural Patterns
Behavioral Patterns
01 Design Patterns
Abstract Factory
Phone Tablet Laptop
The Abstract Factory pattern is an extension of the Factory
Android
pattern. It allows the creation of object families without
specifying concrete classes, resembling a hierarchical
factory of factories.
Analogy iOS
Structure
4
ConcreteFactory1
...
+createProductA(): ProductA
+createProductB(): ProductB
Concrete Concrete
ProductA1 2 ProductB1
3 5
<interface> Client
AbstractFactory -factory: AbstractFactory
ProductA 1 ProductB
+createProductA(): ProductA +Client(f: AbstractFactory)
+createProductB(): ProductB +someOperation()
Concrete Concrete
ProductA2 2 ProductB2
4
ConcreteFactory2
...
+createProductA(): ProductA
+createProductB(): ProductB
02 Design Patterns
Builder Prototype
The Builder pattern constructs complex The Prototype pattern creates new objects
objects incrementally, similar to building a by copying existing ones, ensuring code
multi-layered cake. Each step adds an independence from their classes. It enables
ingredient or component, allowing the creation of prototype objects that
customization and diversity using a replicate the original ones.
consistent building process.
Analogy
Analogy
Say, you want to create an exact copy of an
Customer details are stored in a database object. To perform this, you need to know
using a Customer class with mandatory the class of the object and its fields which
fields (First Name, Last Name, Primary may be private. Prototype delegates the
Email, and Primary Mobile Number) and work of cloning objects to the actual
optional fields (Middle Name, Secondary object itself.
Email, and Secondary Mobile Number)
passed as null values using the builder One Month After
design pattern for flexible object creation. “ Do you think weʼd be
able to copy it? ” “Perfect!”
Structure
Structure
03 Design Patterns
Factory Method
Analogy
Consider a milkshake bar. Customers order
from a waiter, the order goes to the kitchen,
and the kitchen prepares the milkshakes.
The kitchen is like a shake factory, taking
and fulfilling orders.
Structure
04 Design Patterns
Structural Patterns
Adapter
Analogy
Application
A weather forecasting application retrieves Core Classes
JSON-formatted weather data from multiple JSON JSON CSV
x
sources and displays visually appealing
forecasts and maps. To improve functionality, Weather Data Machine Learning
Provider Library
integrating a machine learning library that
only supports CSV data format becomes a
challenge.
Structure
Interface
Image Viewer
Interface
Advanced Image Viewer
+Show(): Void
+ShowPng(): void
+Showjpg(): void implements implements
implements implements
Image Adapter Gallery App
-advancedImage -imageAdapter:
Png Shower Jpg Shower
Viewer: ImageAdapter
uses uses
AdvanceImage
Viewer
+ShowPng() +ShowPng()
+ShowJpg() +ShowJpg() +ImageAdapter()
+Show(): Void
+Show()
05 Design Patterns
Bridge
Analogy
In designing a multimedia application,
different views like Long and Short views
are implemented to present media content
such as artist information and music albums.
Structure
06 Design Patterns
Composite
Structure
The Composite pattern creates tree-like 4
structures by combining objects, allowing Client
uniform manipulation of object collections
regardless of hierarchy. It enhances code
extensibility and modularity when working 1
with complex object arrangements. <<interface>>
Component
+execute()
Analogy
Consider a scenario where you have two
2 3
types of objects: Files and Folders. A Folder Leaf Composite
can contain multiple Files as well as smaller ... -children: Component
Folders. These smaller Folders can also hold
+execute() +add(c: Component)
Files or even smaller Folders, forming a
+remove(c: Component)
hierarchical structure. +getChildren(): Component[]
+execute()
Complex Folder
Decorator
Analogy
In the decorator design pattern, a pizza
File C File D File E
servesas the original class while various
toppings represent added functionalities.
Customers can customize their pizza by
adding toppings without altering the base
structure.
07 Design Patterns
Facade
Structure
The Facade pattern provides a simple
interface to interact with a complex system,
shielding clients from underlying complexities
and dependencies. It acts as a bridge, offering
easy access to desired functionalities without
revealing internal workings.
Analogy
In the Facade design pattern, when placing
an order over the phone, the operator acts as
a simplified interface to access various
services offered by the shop. The facade layer
assigns tasks to the appropriate subsystem
based on user input.
Structure
08 Design Patterns
Flyweight Facade
The Flyweight design pattern optimizes The Proxy pattern acts as a substitute for a
memory usage by sharing common state real object, serving as an intermediary
among multiple objects instead of between the client and the actual object.
duplicating it, allowing for efficient It provides control over actual object-access,
utilization of available resources when allows for additional functionality, and
creating numerous objects. enables client access restrictions.
Analogy Analogy
Brown tree
Green tree
Green tree
Green tree
Brown tree
Green tree
Green tree Brown tree
Structure
Structure
Proxy Design Pattern
09 Design Patterns
Behavioral Patterns
Structure Structure
10 Design Patterns
Iterator
Analogy
In an application that tracks a collection of
alerts, the Iterator design pattern enables
iteration through the alerts without exposing
the underlying structure. This pattern ensures
seamless and controlled traversal of the
collection.
Structure
11 Design Patterns
Mediator
Analogy
In a restaurant kitchen, chefs rely on an
expediter who acts as a central communicator.
The expediter relays orders, ensuring smooth
coordination. Without this, chefs face potential
confusion and delays.
Structure
12 Design Patterns
Memento
Input Memento Application
Memento is a behavioral design pattern that
lets you save and restore the previous state of
an object without revealing the details of its
implementation.
Structure
13 Design Patterns
Observer State
Observer is a behavioral design pattern that The State pattern allows an object to change
lets you define a subscription mechanism to its behavior based on internal state changes,
notify multiple objects about any events that resembling finite state machines. It avoids
happen to the object theyʼre observing. excessive conditional statements and provides
flexibility in altering an object's state.
Analogy
Analogy
Subscribing to a newspaper or magazine
ensures direct delivery to your mailbox, In a video game, the behavior of buttons and
eliminating the need to check availability at controls changes depending on the game
a store. Publishers send new issues promptly state. Such as during gameplay, pressing
or in advance for convenience. buttons triggers various in-game actions,
when the game is paused, pressing any
button brings up the pause menu and so on.
The New York Times
Structure
Structure
1 2
Context <<interface>>
-state State
-Context(initialState) +doThis()
+changeState(state) +doThat()
+doThis()
+doThat()
3
ConcreteStates
-context
+setContext(context)
Client +doThis()
+doThat()
14 Design Patterns
Strategy Template Method
Strategy is a behavioral design pattern that The Template Method pattern defines the
lets you define a family of algorithms, put structure of an algorithm in a superclass,
each of them into a separate class, and make allowing subclasses to override specific steps
their objects interchangeable. without changing the overall structure. It
provides a skeleton of functions, enabling
child classes to implement method details
Analogy
while the parent class maintains the
When going to the airport, transportation algorithm's framework and sequencing.
strategies such as bus, cab, or bicycle offer
options based on factors like budget and
Analogy
time constraints, enabling you to choose
the most suitable approach. Frameworks extensively utilize the Template
Method Design Pattern, executing immutable
domain components and providing
placeholders for user modifications. The
framework becomes central, while client
modifications are secondary.
x x x x x
Structure x x x x x
1 2
Context <<interface>>
Structure
-strategy Strategy
+setStrategy(strategy) +execute(data)
+doSomething(
3
4 ConcreteStrategies
Client
+execute(data)
15 Design Patterns
Visitor
Analogy
The Visitor design pattern decouples algorithms
from shape objects, allowing easy addition of
new algorithms without modifying the objects.
This promotes extensibility and adheres to the Output Output
Open/Closed principle.
Structure
16 Design Patterns
VI. Design Pattern Principles
Start Learning
17 Design Patterns