80
            
            votes
        
            
                
                Accepted
            
        
            
            
        Visitor Pattern: what's the point of the `accept` method?
                    In your simple example, you know exactly the real type of the object on which you invoke the visitor and can therefore chose yourself the right visitor method:
makeSoundVisitor.visitCat(cat);      // ...
                
            
       
        
            
                18
            
            votes
        
        
            
            
        Visitor Pattern: what's the point of the `accept` method?
                    But I really don't see the point of that accept call. If you've got the visitor and the objects to be visited, why not just pass these objects directly to the visitor and avoid the indirection?
...
                
            
       
        
            
                8
            
            votes
        
            
                
                Accepted
            
        
        Circular dependency problem
                    You can do a two pass compilation.
In the first pass emit the machine instructions as you do now.  When you encounter a label record its address in a symbol table.  When you encounter a reference to a ...
                
            
       
        
            
                7
            
            votes
        
            
                
                Accepted
            
        
            
        Can't I just use interfaces to implement the visitor pattern?
                    Using interfaces to describe the Accept(visitor) method or to describe the required structure of the visitor is perfectly fine. However:
the visitor pattern is often used together with a particular ...
                
            
       
        
            
                6
            
            votes
        
            
                
                Accepted
            
        
            
        Does the visitor pattern prevent the need to modify existing code when adding new data types?
                    I'm not sure that the visitor solves the "expression problem".  But your understanding is correct: 
You can derive new visitors based on the abstract visitor. This allows to define new operations that ...
                
            
       
        
            
                4
            
            votes
        
        
            
        Combining composite, decorator and visitor patterns
                    From your description, I get the feeling that your code looks similar to this
class Composite 
{
    accept<T>(Visitor<T> visitor) {
        visitor.visitComposite(this)
    }
}
class ...
                
            
       
        
            
                4
            
            votes
        
        
            
        Visitor Pattern: what's the point of the `accept` method?
                    Like the other answers, I have to admit Christophe's answer is spot on, but there's some confusion around why one might want to getRandomAnimal().
The frustrating reality is that very few books that ...
                
            
       
        
            
                4
            
            votes
        
        
            
            
        Message Dispatching: If, Visitor, or something else
                    I should be able to add new types of messages AND/OR new types of handlers without affecting the existing messages or handlers (i.e., loose coupling).
So, what you might be running into here is known ...
                
            
       
        
            
                4
            
            votes
        
            
                
                Accepted
            
        
        Use of Visitor pattern rather than enums
                    Yes, the visitor pattern can be used as an alternative to enums+switch/case. I would consider this if your enum variants are more complicated (need to carry associated data, not just represent an ...
                
            
       
        
            
                4
            
            votes
        
        
            
        Does the Visitor Pattern necessitate traversing a structure of instances?
                    I have a class hierarchy of elements (more static) with different
operations on them in another class hierarchy (more flexible).  ... I
decided to use the Double Dispatch technique:
elem.do(...
                
            
       
        
            
                3
            
            votes
        
        
            
            
        Message Dispatching: If, Visitor, or something else
                    There are a load of ways to at least partly decouple visitation, but they're effectively different ways to implement Double Dispatch.
This really just means you have a function that is dispatched on ...
                
            
       
        
            
                3
            
            votes
        
        
            
            
        Does every Visited class have to be aware of the Visitor interface?
                    Visitor provides a way to treat all 'visited' (element) variants transparently in the same way, and the mechanism by which this is achieved requires all of the 'visited' classes to be aware of the ...
                
            
       
        
            
                3
            
            votes
        
        
            
        How to implement visitor if not all functions are supported for each Element?
                    Here is a straightforward solution, expressing the case literally: implement ElementA.accept in terms of the base class Visitor, and ElementB.accept in terms of Visitor2:
public ElementA implements ...
                
            
       
        
            
                3
            
            votes
        
        
            
        Appropriate use for the visitor design pattern
                    So the visitor pattern is intended to be a way to build an algorithm without modifying the classes you are visiting.  This really isn't an example where the visitor pattern works.  The fact you have ...
                
            
       
        
            
                3
            
            votes
        
            
                
                Accepted
            
        
            
        Rock Paper Scissors with Visitor Pattern
                    This is an interesting use of the implementation techniques behind the visitor pattern.  However, is does not reflect the essence of the visitor pattern which is:
Represent an operation to be ...
                
            
       
        
            
                2
            
            votes
        
        
            
        Combining composite, decorator and visitor patterns
                    The fix is to remove the composite.
I mean, you can certainly keep it for the structure, but the entire point of the composite is that things can operate against some composite ignorant interface, ...
                
            
       
        
            
                2
            
            votes
        
        
            
        How to use visitor pattern when objects change frequently
                    The structure of the classic visitor pattern gives each combination of "object type" and "action type" a place for a method which handles the specific combination of both. It does ...
                
            
       
        
            
                2
            
            votes
        
        
            
        Visitor Pattern: what's the point of the `accept` method?
                    While other answers focus mostly on polymorphism, I think it's important to answer one of the specific questions you've presented.
It's about adding operations to classes, without modifying the ...
                
            
       
        
            
                2
            
            votes
        
            
                
                Accepted
            
        
        Is using lambdas and overload resolution a recommended way to write a visitor for a variant?
                    Is this a recommended way of doing things?
Yes, that's a fairly normal way of visiting an overloaded function. The issue it solves is that f doesn't denote an object, but instead an overload-set of ...
                
            
       
        
            
                2
            
            votes
        
            
                
                Accepted
            
        
        Alternative to the Visitor Design Pattern
                    What you need is an intermediary unified representation. The problem now is that your serialization procedures need to understand the details/semantics of the various shape types. Instead, what you ...
                
            
       
        
            
                2
            
            votes
        
            
                
                Accepted
            
        
        Avoid use of the visitor pattern in this very common scenario
                    The problem with this architecture is that when we implement a new sender service we are also forced to modify the MessageDispatcher.
This is arguably a good thing, since the compiler will force you ...
                
            
       
        
            
                1
            
            vote
        
        
        Does the Visitor Pattern necessitate traversing a structure of instances?
                    Avoiding the question of what to call what you're doing, here's a possible alternative.
With data oriented programming, we have data structures that would collect and keep the elements of a certain ...
                
            
       
        
            
                1
            
            vote
        
            
                
                Accepted
            
        
        How to implement the visitor pattern without inheritance
                    Based upon the comments on my question, one possible alternative would be to use std::variant and std::visit as such:
#include <variant>
#include <iostream>
class ClassA {};
class ClassB {...
                
            
       
        
            
                1
            
            vote
        
        
        Use of Visitor pattern rather than enums
                    It seems to me that that's an odd implementation of the visitor pattern.
You want to be able to write new visitors that ignore, or group element types, you don't want the element type holding logic ...
                
            
       
        
            
                1
            
            vote
        
        
        Alternative to the Visitor Design Pattern
                    A good start is to avoid reinventing the wheel and look at Boost serialization library.  Even if you can't use the library, its design is worth studying.
Your first solution has a drawback:  you ...
                
            
       
        
            
                1
            
            vote
        
            
                
                Accepted
            
        
        Loose coupling vs transparency in visitor pattern that visits composite
                    There is no single best answer to your question. It’s not without reason that GoF let this question open:
(p.339) Who is responsible for the traversal of the object structure? (...) We can put ...
                
            
       
        
            
                1
            
            vote
        
            
                
                Accepted
            
        
            
        Inheritance and domain-specific logic loses its independency?
                    Keeping the domain logic completely independent is impossible as you noted. Even your first version is not completely independent.
Yes, it is independent of the frameworks, but you have already made ...
                
            
       
        
            
                1
            
            vote
        
        
        Visitor Pattern: what's the point of the `accept` method?
                    accept is a statically type-safe way to permit an if-ladder based on something's type.
if ( thing instanceof Foo ) {
    Foo foo = ( Foo )thing;
    BODY1
} else if ( thing instanceof Bar ) {
    Bar ...
                
            
       
        
            
                1
            
            vote
        
        
            
        Visitor Pattern: what's the point of the `accept` method?
                    Dear StackExchange readers:
You have called upon this post to provide an answer to the question.  And I want to write this methodanswer to be personalized to you.
It's clear that you're a ...
                
            
       
        
            
                1
            
            vote
        
        
        Determine equality of a DAG
                    A directed graph can be expressed as a set of Node / Edge pairs (assuming nodes and edges have some kind of unique identity), to determine if two graphs are equivalent you can perform the symmetric ...
                
            
       
        Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
visitor-pattern × 42design-patterns × 26
design × 8
object-oriented × 8
c++ × 8
java × 5
object-oriented-design × 4
c# × 3
php × 3
parsing × 3
compiler × 2
composition × 2
iterator × 2
python × 1
data-structures × 1
refactoring × 1
functional-programming × 1
inheritance × 1
solid × 1
patterns-and-practices × 1
single-responsibility × 1
polymorphism × 1
graph × 1
trees × 1
liskov-substitution × 1