Description / Meta Description
Complete your Gang of Four Behavioral Design Pattern journey by learning Iterator, Memento, Visitor, and Interpreter patterns. Understand where these advanced patterns fit, their real-world applications, advantages, limitations, and how modern Java frameworks implement similar concepts.
Completing the Behavioral Design Patterns Journey
Throughout this series we’ve covered the most frequently used behavioral patterns:
- Strategy
- Observer
- Command
- Chain of Responsibility
- State
- Template Method
- Mediator
These patterns appear daily in enterprise systems.
The remaining four GoF Behavioral Patterns are less commonly implemented directly, but they remain important because they solve specific architectural challenges.
They are:
Iterator
Memento
Visitor
Interpreter
Let’s explore each one.
Iterator Pattern
The Problem
Suppose you have a collection:
List<Employee> employees;
How should consumers traverse it?
Without Iterator:
for(int i=0; i<employees.size(); i++) {
}
The client becomes dependent on collection internals.
What is Iterator?
Iterator provides a way to:
Access elements sequentially without exposing the collection’s internal structure.
Architecture
Collection
│
▼
Iterator
│
▼
Next Element
Java Example
Every Java developer uses Iterator.
Iterator<Employee> iterator =
employees.iterator();
while(iterator.hasNext()) {
Employee employee =
iterator.next();
}
Why It Matters
Client code does not care whether:
ArrayList
LinkedList
HashSet
TreeSet
stores the data.
Traversal remains consistent.
Real World Examples
Java Collections
iterator()
Streams
stream()
Database Result Sets
ResultSet
Paginated APIs
Next Page
Previous Page
All are Iterator concepts.
Memento Pattern
The Problem
Imagine Microsoft Word.
User performs:
Type Text
Delete Text
Format Text
Then presses:
Undo
How can we restore a previous state?
What is Memento?
Memento captures:
An object’s internal state so it can be restored later without exposing implementation details.
Architecture
Originator
│
▼
Memento
▲
│
Caretaker
Example
Document:
Document doc;
Save state:
Memento snapshot =
doc.save();
Restore state:
doc.restore(snapshot);
Real World Examples
Text Editors
Undo
Redo
IDEs
Rollback Changes
Workflow Systems
Checkpoint
Restore
Database Savepoints
SAVEPOINT
ROLLBACK
Conceptually similar.
Benefits
Undo Support
History Tracking
Rollback Capability
Limitation
Large objects create:
Large Snapshots
Memory Usage
issues.
Visitor Pattern
The Problem
Imagine a document system.
Objects:
Text
Image
Table
Chart
Now new operations arrive:
Export PDF
Export HTML
Export Word
Without Visitor:
Every class requires modification.
What is Visitor?
Visitor allows:
Adding new operations to existing object structures without modifying those objects.
Architecture
Visitor
│
▼
Text
Image
Table
Chart
Example
Document Elements:
TextElement
ImageElement
TableElement
Visitors:
PdfExportVisitor
HtmlExportVisitor
WordExportVisitor
Usage:
element.accept(visitor);
Real World Examples
Compiler Design
Syntax Tree Traversal
Code Analysis Tools
Static Analysis
IDE Refactoring Tools
Code Inspection
Document Conversion
PDF
HTML
DOCX
Benefits
Add operations without changing domain objects.
Supports:
Open/Closed Principle
Drawback
Adding new object types becomes harder.
Interpreter Pattern
The Problem
Suppose users define rules:
age > 18 AND country = "US"
How can software understand these expressions?
What is Interpreter?
Interpreter provides:
A way to define a grammar and evaluate sentences written in that grammar.
Architecture
Expression
│
▼
Interpreter
│
▼
Result
Example
Expression:
A AND B
Classes:
Expression
AndExpression
OrExpression
TerminalExpression
Evaluation:
expression.interpret(context);
Real World Examples
SQL Engines
SELECT *
FROM USERS
Search Filters
status=ACTIVE
Rule Engines
Age > 18
Spring Expression Language
#{user.name}
Drools
Business Rules Engine
Benefits
Supports:
Custom Languages
Rule Engines
DSLs
Limitations
Complex grammars become difficult to maintain.
Modern parsers often replace direct Interpreter implementations.
Which of These Patterns Matter Most Today?
For modern enterprise Java:
Iterator
★★★★★
Used daily.
Memento
★★★★☆
Useful for undo and rollback systems.
Visitor
★★★☆☆
Common in compiler and tooling frameworks.
Interpreter
★★☆☆☆
Usually appears inside rule engines and DSL frameworks.
Complete Behavioral Pattern Cheat Sheet
Strategy
→ Choose Behavior
Observer
→ Broadcast Events
Command
→ Encapsulate Actions
Chain of Responsibility
→ Process Through Pipeline
State
→ Change Behavior By State
Template Method
→ Define Workflow Skeleton
Mediator
→ Coordinate Communication
Iterator
→ Traverse Collections
Memento
→ Save And Restore State
Visitor
→ Add Operations Without Modifying Objects
Interpreter
→ Evaluate Expressions
Gang of Four Design Patterns: Final Summary
Creational Patterns
Singleton
Factory Method
Abstract Factory
Builder
Prototype
Focus:
Object Creation
Structural Patterns
Adapter
Decorator
Facade
Proxy
Composite
Bridge
Flyweight
Focus:
Object Organization
Behavioral Patterns
Strategy
Observer
Command
Chain of Responsibility
State
Template Method
Mediator
Iterator
Memento
Visitor
Interpreter
Focus:
Object Communication
And Behavior
Final Thoughts
Design Patterns are not frameworks.
They are not libraries.
They are:
Proven solutions to recurring software design problems.
The true value of learning GoF patterns is not memorizing definitions.
It is recognizing situations where:
A Strategy eliminates if-else logic.
An Observer enables event-driven architecture.
A State models business workflows.
A Chain builds processing pipelines.
A Template Method standardizes processes.
A Mediator reduces coupling.
An Iterator simplifies traversal.
A Memento enables rollback.
A Visitor extends behavior safely.
An Interpreter enables custom languages.
Once you start seeing these patterns in Spring, Hibernate, Kafka, Microservices, Batch Systems, and Enterprise Applications, you’ll realize that modern frameworks are essentially sophisticated implementations of these classic design principles.
Congratulations—you’ve now completed the full journey through all 23 Gang of Four Design Patterns.