🎨 Design Patterns (Gang of Four)
Intermediate
What is the Visitor design pattern?
Answer
The Visitor pattern lets you add new operations to a class hierarchy without modifying those classes, by separating the algorithm from the object structure. You define a Visitor interface with a visit() method for each element type. Each element implements an accept(Visitor v) method that calls v.visit(this) — this "double dispatch" technique ensures the correct visitor method is called based on both the element type and the visitor type. Use it when you have a stable set of element types but need to frequently add new operations. XML/AST processors and compiler optimization passes are classic examples.
Previous
What is the Template Method design pattern?
Next
What is the difference between Factory Method and Abstract Factory?
More Design Patterns (Gang of Four) Questions
View all →- Intermediate What is the Template Method design pattern?
- Intermediate What is the difference between Factory Method and Abstract Factory?
- Intermediate When should you NOT use design patterns?
- Intermediate How are the Gang of Four patterns categorized?
- Intermediate How are GoF patterns used in real-world frameworks?