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
ObjectProvide an interface for creating families of related objects without specifying their concrete classes.
Builder
ObjectSeparate construction of a complex object from its representation so the same process can create different representations.
Factory Method
ClassDefine an interface for creating an object, but let subclasses decide which class to instantiate.
Prototype
ObjectSpecify the kinds of objects to create using a prototypical instance, and create new objects by copying it.
Singleton
ObjectEnsure a class has only one instance and provide a global point of access to it.
Structural
How objects compose โ 7 patterns
Adapter
Class ยท ObjectConvert the interface of a class into another interface clients expect.
Bridge
ObjectDecouple an abstraction from its implementation so the two can vary independently.
Composite
ObjectCompose objects into tree structures to represent part-whole hierarchies uniformly.
Decorator
ObjectAttach additional responsibilities to an object dynamically โ a flexible alternative to subclassing.
Facade
ObjectProvide a unified, high-level interface that makes a complex subsystem easier to use.
Flyweight
ObjectUse sharing to support large numbers of fine-grained objects efficiently.
Proxy
ObjectProvide a surrogate or placeholder for another object to control access, lazy-load, or audit it.
Behavioral
How objects collaborate โ 11 patterns
Chain of Responsibility
ObjectPass a request along a chain of handlers; each handler decides to act or pass it on.
Command
ObjectEncapsulate a request as an object so you can parameterize, queue, log, and undo it.
Interpreter
ClassGiven a language, define a representation for its grammar plus an interpreter that uses it.
Iterator
ObjectProvide a way to access elements of an aggregate sequentially without exposing its representation.
Mediator
ObjectDefine an object that encapsulates how a set of objects interact, reducing direct coupling.
Memento
ObjectCapture and externalize an object's internal state so it can be restored later.
Observer
ObjectDefine a one-to-many dependency so all dependents are notified when the subject changes.
State
ObjectAllow an object to alter its behavior when its internal state changes โ it looks like a class change.
Strategy
ObjectDefine a family of algorithms, encapsulate each one, and make them interchangeable behind a common interface.
Template Method
ClassDefine the skeleton of an algorithm and let subclasses fill in specific steps.
Visitor
ObjectRepresent 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/6Which of these is a design pattern, not an algorithm?
Diagrams
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/4Singleton
Adapter
Observer
Builder
Decorator
Strategy
Factory Method
Composite
Iterator
Creational
Structural
Behavioral
Pattern myths โ true or false?
EXERCISE 2/4A 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/4class 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/4Click on the line you think contains the bug, then press Confirm.