đź“…Week-8 (Day-3) - Understanding the Visitor Design Pattern in System Design

Table of contents
- NOTE: - I started my 8-week system design journey with Coder Army. I will be journaling every day, recording what I learn, reflecting on it, and sharing it with my network to help newcomers to system design.
- Namaste Developers!
- đź’ What is the Visitor Pattern?
- đź’ Why & When to Use Visitor Pattern?
- 💠Real-World Analogy – Think Airport Security!
- đź’ Double Dispatcher vs Single Dispatcher
- 💠Real-World Analogy — File System Inspections
- đź’ UML
- đź’ Code
- đź’ Strategy Pattern vs Visitor Pattern
- đź’ Benefits of Visitor Pattern
- đź’ Conclusion
- đź’ Final Thoughts

NOTE: - I started my 8-week system design journey with Coder Army. I will be journaling every day, recording what I learn, reflecting on it, and sharing it with my network to help newcomers to system design.
Namaste Developers!
Welcome to another exciting day of the #8WeeksLLDChallenge — and today, we’re unlocking a powerful design pattern that brings true flexibility to your codebase: the Visitor Design Pattern.
đź’ What is the Visitor Pattern?
The Visitor Pattern allows you to add new operations to an existing object structure without changing its classes. It separates the operations from the objects on which they operate.
or
Visitor Pattern allows you to add new operations to an object structure without modifying its classes.
(Hindi: Visitor Pattern aapko ye ability deta hai ki aap kisi bhi object structure mein naye kaam (functionality) add kar sako bina unki existing classes ko badle.)
đź’ Why & When to Use Visitor Pattern?
âś… You have a stable class structure
âś… You want to perform different, unrelated operations on those classes
✅ You don’t want to mess with existing logic
✅ You believe in Open/Closed Principle – “Open for extension, closed for modification”
âś… You want to separate algorithms from objects
💠Real-World Analogy – Think Airport Security!
Imagine airport pe aapke bag check karne ke liye alag-alag log hain:
Security officer scans for dangerous items
Size checker checks luggage size
Lock checker checks if bag is locked
You don’t change the bag (object). Instead, alag-alag visitors aake bag ko inspect karte hain for different purposes.
This is Visitor Pattern.
đź’ Double Dispatcher vs Single Dispatcher
🔸 Single Dispatcher:
Only one reference (object) decides which method to call.
Example:
animal.makeSound();
👉 animal
decides at runtime which class's method to run.
(Hindi: Yahan sirf animal
decide karega ki dog ka bark()
chalega ya cat ka meow()
.)
🔸 Double Dispatcher:
đź’ˇ Two objects (visitor + element) together decide which method to execute.
file.accept(visitor);
file
decides whichvisit()
gets calledvisitor
decides how to act onfile
(Hindi: Dono milke decide karte hain kaun sa method chalega - visitor kaunsa hai aur object ka type kya hai.)
Visitor Pattern is a classic use case of double dispatch.
💠Real-World Analogy — File System Inspections
Imagine you have files of different types:
Text Files (
.txt
)Image Files (
.jpg
)Video Files (
.mp4
)
And you want to perform these operations:
Size calculation
Compression
Virus scanning
Problem: Should we write all logic inside each file class?
❌ No! That would violate SRP and OCP.
âś… Instead, we use Visitor Pattern:
Create visitors for each operation.
Each file "accepts" the visitor to perform the job.
đź’ UML
đź’ Code
class TextFile extends FileSystemItem {
private String content;
public TextFile(String fileName, String fileContent) {
super(fileName);
this.content = fileContent;
}
public String getContent() {
return content;
}
@Override
public void accept(FileSystemVisitor visitor) {
visitor.visit(this);
}
}
class ImageFile extends FileSystemItem {
public ImageFile(String fileName) {
super(fileName);
}
@Override
public void accept(FileSystemVisitor visitor) {
visitor.visit(this);
}
}
class VideoFile extends FileSystemItem {
public VideoFile(String fileName) {
super(fileName);
}
@Override
public void accept(FileSystemVisitor visitor) {
visitor.visit(this);
}
}
// Visitor Interface
interface FileSystemVisitor {
void visit(TextFile file);
void visit(ImageFile file);
void visit(VideoFile file);
}
abstract class FileSystemItem {
protected String name;
public FileSystemItem(String itemName) {
this.name = itemName;
}
public String getName() {
return name;
}
public abstract void accept(FileSystemVisitor visitor);
}
// 1. Size calculation visitor
class SizeCalculationVisitor implements FileSystemVisitor {
@Override
public void visit(TextFile file) {
System.out.println("Calculating size for TEXT file: " + file.getName());
}
@Override
public void visit(ImageFile file) {
System.out.println("Calculating size for IMAGE file: " + file.getName());
}
@Override
public void visit(VideoFile file) {
System.out.println("Calculating size for VIDEO file: " + file.getName());
}
}
// 2. Compression Visitor
class CompressionVisitor implements FileSystemVisitor {
@Override
public void visit(TextFile file) {
System.out.println("Compressing TEXT file: " + file.getName());
}
@Override
public void visit(ImageFile file) {
System.out.println("Compressing IMAGE file: " + file.getName());
}
@Override
public void visit(VideoFile file) {
System.out.println("Compressing VIDEO file: " + file.getName());
}
}
// 3. Virus Scanning Visitor
class VirusScanningVisitor implements FileSystemVisitor {
@Override
public void visit(TextFile file) {
System.out.println("Scanning TEXT file: " + file.getName());
}
@Override
public void visit(ImageFile file) {
System.out.println("Scanning IMAGE file: " + file.getName());
}
@Override
public void visit(VideoFile file) {
System.out.println("Scanning VIDEO file: " + file.getName());
}
}
public class VisitorPattern {
public static void main(String[] args) {
FileSystemItem img1 = new ImageFile("sample.jpg");
img1.accept(new SizeCalculationVisitor());
img1.accept(new CompressionVisitor());
img1.accept(new VirusScanningVisitor());
FileSystemItem vid1 = new VideoFile("test.mp4");
vid1.accept(new CompressionVisitor());
}
}
đź’ Strategy Pattern vs Visitor Pattern
Feature | Strategy Pattern | Visitor Pattern |
Focus | Change how behavior is done | Add new operations |
Example | Fly with wings or jet | fly(), talk(), walk() |
Behavior | Same behavior, different implementation | Different behaviors altogether |
Implementation | Follows single dispatch | Follows double dispatch |
đź’ Real-life Analogy:
Strategy Pattern:
Flying ka behavior same hai — bas mode change hua:
👉 Fly with wings vs Fly with JetVisitor Pattern:
Completely different actions on object:
👉 walk(), talk(), fly()
đź’ Benefits of Visitor Pattern
âś… Easy to add new operations (just add a new visitor!)
âś… Keeps file structure untouched (OCP followed!)
âś… Clean separation of logic
âś… Great for compiler, file systems, rendering engines
đź’ Conclusion
âś… Use Visitor Pattern when:
You want to add multiple operations to objects like files
You don’t want to touch existing object logic
You want clean, extensible, testable code
đź’ Final Thoughts
Week - 8 (Day-3) Completed âś… System Design
NOTE : - A big thanks to my mentors Rohit Negi Sir and Aditya Sir for launching this amazing 8-week course absolutely free on YouTube via CoderArmy9 :- youtube.com/@CoderArmy9 . 🙌
👉 Share this blog with your connections! Let’s keep learning, growing, and supporting one another on this journey. 🚀
✍️ Payal Kumari 👩‍💻
Jai Hind 🇮🇳 | #CoderArmy #LearningInPublic #SystemDesign #TechForAll #MentorshipMatters #8weeksLLdChallenge #LowLevelDesign #LLD 👩‍💻
Subscribe to my newsletter
Read articles from Payal Kumari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Payal Kumari
Payal Kumari
I'm a passionate full-stack developer with a strong foundation in the MERN stack—building and maintaining scalable web applications using React.js, Node.js, and Next.js. My journey in open source began with Hacktoberfest 2023, where I made four impactful pull requests that sparked a love for collaborative coding, global learning, and open knowledge sharing. Since then, I’ve contributed to and mentored projects in top open source programs like GSSoC’24, SSOC’24, and C4GT’24. As a Google Gen AI Exchange Hackathon ’24 Finalist and Google’s Women Techmakers (WTM) Ambassador, I’ve been privileged to support diverse communities in building meaningful tech solutions. My work as a Top 50 Mentor for GSSoC ’24 reflects my commitment to nurturing new talent in tech. Beyond development, I serve as a Student Career Guide, Profile Building Expert & Evangelist at Topmate.io, where I conduct workshops, guide students through resume building and career strategy, and help mentees navigate open source and tech careers. Recognized among the Top 5% of mentors and featured on “Topmate Discover,” I take pride in making mentorship accessible and impactful. My technical voice has also been acknowledged by LinkedIn, where I’ve earned the Top Voice badge seven times in domains like web development, programming, and software engineering. In addition, I hold LinkedIn Golden Badges for Research Skills, Interpersonal Skills, Critical Thinking, and Teamwork—signaling a well-rounded approach to both individual contribution and team collaboration. Graduating with an MCA from Chandigarh University in 2023, I’ve continued to fuel my curiosity by writing technical articles and sharing practical MERN stack insights across platforms. Whether it’s building polished UIs, optimizing backend performance, or guiding a mentee through their first pull request, I’m driven by the power of community and continuous learning. Let’s connect! I'm open to collaborations, mentorship, or building something impactful together. Reach out to me at kumaripayal7488@gmail.com or visit my profile on Topmate.io.