Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8312789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:16:32+00:00 2026-06-08T20:16:32+00:00

Why do we need both accept and visit functions in the visitor DP? if

  • 0

Why do we need both accept and visit functions in the visitor DP? if we take the typical example:

class Visitor {
  visit(A a) { }
  visit(B b) { }
}

interface Element {
  accept(Visitor v);
}

class A implements Element {
  accept(Visitor v) {
    v.visit(this); // will call visit(A)
  }
}

class B implements Element {
  accept(Visitor v) {
    v.visit(this); // will call visit(B)
  }
}

To use the visitor DP, somewhere we have an instance v of Visitor, and an instance e of Element, and we do:

e.visit(v)

which simply calls

v.visit(e)

so why can’t we simply do the second call and bypass the mediator function? Since I think the GoF are not dumb, I guess I’m missing something. What is it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:16:35+00:00Added an answer on June 8, 2026 at 8:16 pm

    Here is an excellent reference for the Visitor pattern, in which they mention the following:

    When the accept() method is called in the program, its implementation
    is chosen based on both:

    • The dynamic type of the element.
    • The static type of the visitor.

    When the associated visit() method is called, its implementation is
    chosen based on both:

    • The dynamic type of the visitor.
    • The static type of the element as known from within the implementation of the accept() method, which is the same as the
      dynamic type of the element. (As a bonus, if the visitor can’t handle
      an argument of the given element’s type, then the compiler will catch
      the error.)

    Consequently, the implementation of the visit() method is chosen based
    on both:

    • The dynamic type of the element.
    • The dynamic type of the visitor.

    Then they go on to mention the following:

    This effectively implements double dispatch…

    In this way, a single algorithm can be written for traversing a graph of
    elements, and many different kinds of operations can be performed
    during that traversal by supplying different kinds of visitors to
    interact with the elements based on the dynamic types of both the
    elements and the visitors.

    Which can be seen in the Java car example:

    class Car implements CarElement {
        CarElement[] elements;
        // ...
        public void accept(CarElementVisitor visitor) {     
            for(CarElement elem : elements) {
                elem.accept(visitor);
            }
            visitor.visit(this); 
        }
    }
    

    So, to summarize, in your example, you wont get much benefit from the original DP implementation, but if the example is more complex, for example its a Composite with several internal implementations of Element (like the Java car example above), then it can apply the visitor behavior to some or all of its internal elements.

    Based on the comments to your original question, there are 2 parts to this pattern:

    1. The original motivation for the pattern: which is to visit a complex structure of objects and perform operations on them without changing the class interfaces being visited.
    2. How the pattern is implemented: which is with the double dispatch as you mention.

    I highly recommend the following design patterns book, as it really simplifies many of the concepts. The GoF book is sometimes too academic.

    Head First Design Patterns

    According to this book, here are the pros and cons to using the Visitor:

    (In the context of the example used)

    PROs

    • Allows you to add operations to a Composite structure without changing the structure itself
    • Adding new operations is relatively easy
    • The code for operations performed by the Visitor is centralized

    CONs

    • The Composite classes’ encapsulation is broken when the Visitor is used.
    • Because the traversal function is involved, changes to the Composite structure are more difficult
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I gather the visitor's time zone information? I need both: the time
I have a parent and child class that both need to implement IDisposable .
I have this class Class Fruits: Class fruits: Are they both same or different
I have declared this interface: public interface Filter { /** Determines whether to accept
I am currently working on a complex web interface and backend, that will need
I have a table in which I need both the values to be primary
I am making a distributed java app for which I need both parts of
I use jersey-client to consume a REST service. I need both the requested Entity
I'm writing a mixed C++/CLI DLL with both managed and unmanaged classes. Both need
Project layout: /project_a /shared /project_b /shared /shared project_a and project_b both need to contain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.