0/0
Design Patterns

Introduction to Design Patterns

Why patterns exist, how they are classified, and how to choose one.

Explore Topics

๐Ÿงญ
What Is a Design Pattern?
A customizable blueprint โ€” not a library you copy.
Pattern Catalog Explorer โ€” 23 GoF patterns at a glance
Purpose:
Scope:
Creational
How objects get made โ€” 5 patterns
Abstract Factory
Object
Provide an interface for creating families of related objects without specifying their concrete classes.
Builder
Object
Separate construction of a complex object from its representation so the same process can create different representations.
Factory Method
Class
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Prototype
Object
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying it.
Singleton
Object
Ensure a class has only one instance and provide a global point of access to it.
Structural
How objects compose โ€” 7 patterns
Adapter
Class ยท Object
Convert the interface of a class into another interface clients expect.
Bridge
Object
Decouple an abstraction from its implementation so the two can vary independently.
Composite
Object
Compose objects into tree structures to represent part-whole hierarchies uniformly.
Decorator
Object
Attach additional responsibilities to an object dynamically โ€” a flexible alternative to subclassing.
Facade
Object
Provide a unified, high-level interface that makes a complex subsystem easier to use.
Flyweight
Object
Use sharing to support large numbers of fine-grained objects efficiently.
Proxy
Object
Provide a surrogate or placeholder for another object to control access, lazy-load, or audit it.
Behavioral
How objects collaborate โ€” 11 patterns
Chain of Responsibility
Object
Pass a request along a chain of handlers; each handler decides to act or pass it on.
Command
Object
Encapsulate a request as an object so you can parameterize, queue, log, and undo it.
Interpreter
Class
Given a language, define a representation for its grammar plus an interpreter that uses it.
Iterator
Object
Provide a way to access elements of an aggregate sequentially without exposing its representation.
Mediator
Object
Define an object that encapsulates how a set of objects interact, reducing direct coupling.
Memento
Object
Capture and externalize an object's internal state so it can be restored later.
Observer
Object
Define a one-to-many dependency so all dependents are notified when the subject changes.
State
Object
Allow an object to alter its behavior when its internal state changes โ€” it looks like a class change.
Strategy
Object
Define a family of algorithms, encapsulate each one, and make them interchangeable behind a common interface.
Template Method
Class
Define the skeleton of an algorithm and let subclasses fill in specific steps.
Visitor
Object
Represent an operation to be performed on elements of a structure without changing the elements themselves.
Click any pattern card for its full intent and a known JDK use. All 23 from Gamma et al. (1995); the rest of this module deep-dives the ones you will use most.
๐Ÿงญ
What Is a Design Pattern?
A customizable blueprint โ€” not a library you copy.

A design pattern is a typical, named solution to a problem that keeps showing up in object-oriented design. Patterns are not finished code you import; they are blueprints you adapt to your own classes. The key contrast is with algorithms: an algorithm tells you exact STEPS to reach a result, like a cooking recipe. A pattern tells you the SHAPE of a solution and which parts must stay variable, more like a blueprint for a house.

Key Ideas
  • A pattern is a description of a recurring design problem and a vocabulary for talking about its solution.
  • Patterns are customized for each codebase โ€” there is no single canonical "Strategy.java" you can drop in.
  • Pattern vs Algorithm: algorithms have one correct result; patterns have one correct SHAPE with several valid fillings.
  • A pattern usually has a "hot-spot" โ€” the place where the variability lives, often expressed as an interface.
  • Libraries vs patterns: a library is reusable CODE; a pattern is reusable STRUCTURE.
Strengths
  • โœฆLets two implementations of the same idea be recognized as "the same thing."
  • โœฆGives the variable parts a place to live, isolating change from the rest of the code.
  • โœฆSurvives across languages โ€” the Strategy idea applies to Java, Python, Go, Rust alike.
  • โœฆNames the design so it can be discussed without paraphrasing it every time.
Weaknesses
  • โ–ณA pattern is harder to apply than a function call โ€” you must translate the blueprint into your own classes.
  • โ–ณConfusing "pattern" with "library" leads developers to hunt for an import that does not exist.
  • โ–ณMemorizing pattern names without understanding intent produces cargo-cult design.
Used in:Java's Collections framework uses Iterator (a behavioral pattern). java.util.Iterator is the BLUEPRINT; ArrayList, HashSet, and LinkedList each provide their own customized iterator that fits the shape.
Code Snippets

Quiz

What Is a Design Pattern? Quiz
QUIZ 1/6
Which of these is a design pattern, not an algorithm?

Diagrams

GoF classification โ€” Purpose ร— Scope (representative patterns)
Loading diagram...
From buildings to bytes โ€” the pattern timeline
Loading diagram...

Practice

Sort each pattern into its GoF purpose category. (Hint: the first letter of each category is a strong mnemonic โ€” Creational, Structural, Behavioral.)
EXERCISE 1/4
Singleton
Adapter
Observer
Builder
Decorator
Strategy
Factory Method
Composite
Iterator
Creational
Structural
Behavioral
Pattern myths โ€” true or false?
EXERCISE 2/4
A design pattern is a library you can import and reuse directly.
An algorithm describes the exact steps to a result; a pattern describes the structural shape of a solution.
The Gang of Four published 23 patterns in 1977.
Architectural patterns are higher-level than design patterns.
Class patterns use composition; object patterns use inheritance.
Christopher Alexander was a software engineer.
Knowing pattern vocabulary compresses design discussions.
Refactor the if-chain into a Strategy
EXERCISE 3/4
class CharacterMover { private movement; CharacterMover( m) { this.movement = m; } void setMovement( m) { this.movement = m; } void act() { ; } } interface { void execute(); } class Walk implements { public void execute() { System.out.println("walking"); } } class Run implements { public void execute() { System.out.println("running"); } }
Why does this "Singleton" not actually enforce one instance?
EXERCISE 4/4
Click on the line you think contains the bug, then press Confirm.
1public class Settings {
2 private static Settings instance;
3
4 public Settings() {} // <-- look here
5
6 public static Settings getInstance() {
7 if (instance == null) instance = new Settings();
8 return instance;
9 }
10}

References

๐Ÿ“š

Gamma, Helm, Johnson, Vlissides โ€” Design Patterns: Elements of Reusable Object-Oriented Software (1995)

pearson.com

book
๐Ÿ“š

Christopher Alexander โ€” A Pattern Language (1977)

global.oup.com

book
๐Ÿ“„

Refactoring.guru โ€” Design Patterns catalog (all 23, with diagrams)

refactoring.guru

article
๐Ÿ“„

Source Making โ€” Design Patterns (intent-first explanations)

sourcemaking.com

article
๐Ÿ“„

Erich Gamma on Flexibility and Reuse (Artima interview, 2005)

artima.com

article
๐Ÿ“‹

Wikipedia โ€” Software design pattern (classification overview)

en.wikipedia.org

docs