Questions tagged [visitor-pattern]

The visitor design pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. This tag is a synonym of the more frequently used [visitor] - use it instead of this tag.

The visitor design pattern is a way of separating an algorithm from an object structure on which it operates.

A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures.

This tag is a synonym of the more frequently used ; please use rather than this tag.

774 questions
342
votes
20 answers

When should I use the Visitor Design Pattern?

I keep seeing references to the visitor pattern in blogs but I've got to admit, I just don't get it. I read the wikipedia article for the pattern and I understand its mechanics but I'm still confused as to when I'd use it. As someone who just…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
98
votes
5 answers

What is the point of accept() method in Visitor pattern?

There is a lot of talk on decoupling the algorithms from the classes. But, one thing stays aside not explained. They use visitor like this abstract class Expr { public T accept(Visitor visitor) { return visitor.visit(this); } } class…
Val
  • 1
  • 8
  • 40
  • 64
96
votes
6 answers

Visitor pattern's purpose with examples

I'm really confused about the visitor pattern and its uses. I can't really seem to visualize the benefits of using this pattern or its purpose. If someone could explain with examples if possible that would be great.
Victor
  • 1,305
  • 2
  • 11
  • 15
56
votes
9 answers

Alternative to the visitor pattern?

I am looking for an alternative to the visitor pattern. Let me just focus on a couple of pertinent aspects of the pattern, while skipping over unimportant details. I'll use a Shape example (sorry!): You have a hierarchy of objects that implement…
Steg
  • 10,000
  • 3
  • 24
  • 17
51
votes
5 answers

Difference betwen Visitor pattern & Double Dispatch

I am reading about visitor pattern, and it appears the same like Double Dispatch. Is there any difference between the two. Do the two terms means the same thing. reference: http://www.vincehuston.org/dp/visitor.html
vamsi
  • 1,727
  • 3
  • 22
  • 25
51
votes
12 answers

What is the difference between Strategy pattern and Visitor Pattern?

I have trouble understanding these two design patterns. Can you please give me contextual information or an example so I can get a clear idea and be able to map the difference between the two of them. Thanks.
dotnetstep
  • 17,065
  • 5
  • 54
  • 72
41
votes
5 answers

Confused about the Visitor Design Pattern

So, I was just reading about the Visitor pattern and I found the back and forth between the Visitor and the Elements very strange! Basically we call the element, we pass it a visitor and then the element passes itself to the visitor. AND THEN the…
AFP_555
  • 2,392
  • 4
  • 25
  • 45
37
votes
3 answers

How to write the Visitor Pattern for Abstract Syntax Tree in Python?

My collegue suggested me to write a visitor pattern to navigate the AST. Can anyone tell me more how would I start writing it? As far as I understand, each Node in AST would have visit() method (?) that would somehow get called (from where?). That…
34
votes
3 answers

How to make a safer C++ variant visitor, similar to switch statements?

The pattern that a lot of people use with C++17 / boost variants looks very similar to switch statements. For example: (snippet from cppreference.com) std::variant v = ...; std::visit(overloaded { [](auto arg) {…
30
votes
8 answers

Difference between decorator design pattern and visitor design pattern

I believe to understand the intent of Decorator and Visitor design pattern. Though i can list following differences Decorator works on an object, Visitor works on composite structure, Decorator is Structural design pattern, visitor is Behavioral…
Tilak
  • 30,108
  • 19
  • 83
  • 131
30
votes
5 answers

Why use the Visitor Pattern?

Duplicate of: When Should I Use The Visitor Design Pattern Why would someone want to use the visitor pattern? I've read a couple of articles, but I'm not getting something. If I need a function to bill a custom, I could…
Tired
30
votes
3 answers

How does the Visitor Pattern not violate the Open/Closed Principle?

From Wikipedia : The idea was that once completed, the implementation of a class could only be modified to correct errors; new or changed features would require that a different class be created. That class could reuse coding from the original…
30
votes
2 answers

Visitor Pattern Explanation

So I've read up all the documentation about the Visitor pattern, and I'm still mightily confused. I've taken this example from another SO question, could someone help me understand? For instance when do we use a visitor design pattern? I think I may…
Trista N
  • 403
  • 1
  • 4
  • 11
29
votes
7 answers

C++ template to cover const and non-const method

I have a problem with duplication of identical code for const and non-const versions. I can illustrate the problem with some code. Here are two sample visitors, one which modifies the visited objects and one which does not. struct VisitorRead { …
paperjam
  • 8,321
  • 12
  • 53
  • 79
28
votes
1 answer

best way to do variant visitation with lambdas

I want to inline visitation of variant types with lambdas. At the moment i have the following code: struct Foo { boost::variant< boost::blank , int , string , vector< int > > var; template
lurscher
  • 25,930
  • 29
  • 122
  • 185
1
2 3
51 52