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

  • Home
  • SEARCH
  • 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 9006715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:29:12+00:00 2026-06-16T01:29:12+00:00

I do well master Visitor Pattern . However, I wonder something. Most important motivation

  • 0

I do well master Visitor Pattern.
However, I wonder something.

Most important motivation to use Visitor Pattern is to add logic involvingo specific Data Models in client side without needing to check for the real data object type. The technique used for solving is called Double-Dispatching.

So, here a code snippet of data model implementing an accept() method:

public class Ferrari extends Car {
    //......
    @Override
    public void accept(Visitor v){
      v.visit(this);
    }
  }

And here a PrintCarVisitor implementing Visitor interface:

public class PrintCarVisitor implements Visitor {
  //...
  @Override
  public void visit(Ferrari c){
    System.out.println("Ferrari");
  }
}

Hence, no if/else series and instanceof series needed.

Any client would be:

Visitor visitor = new PrintCarVisitor();
car.accept(visitor);  //no need to know the exact Car's type

However, since Visitor doesn’t keep Open/Closed Principle (because a new Data Model leads to break the class by adding its own visit method), why do we bother with double-dispatching?

Can’t we just isolate the if/else series WITHIN the visitor implementation.

With this hypothetical alternative, this part of code would disappear:

 public class Ferrari extends Car {
    //This method is not needed anymore with this alternative
    @Override
    public void accept(Visitor v){
      v.visit(this);
    }
  }

PrintCarVisitor would be:

public class PrintCarVisitor {
   public void visit(Car c){
     if(c instanceof Ferrari){
       System.out.println("Ferrari");
     }
   }      
}

With this alternative, every caller would still deal with data model abstraction like this:

new PrintCarVisitor().visit(car); //no need to know the exact data type in client side

A priori, this second approach is great since it doesn’t involve all the boilerplate generates during implementation of pure Visitor Pattern.

I imagine this approach has two drawbacks:

1) There’s no guarantee (like Visitor interface imposes) that any used visitor disposes of a method corresponding to the Car currently treated.

2) BoilerPlate code remains heavier into the Visitor implementation class with the series of instanceof and casting.

Are they any other drawbacks explaining why Visitor Pattern HAS TO use Double Dispatching and so can’t simply isolate the instanceof series inside a class (as static Factory does for example)?

  • 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-16T01:29:13+00:00Added an answer on June 16, 2026 at 1:29 am
    1. If you do this, you no longer have a Visitor, you basically have some sort of processor. Your code would just be a list iteration where every time thru the loop, you pass what formerly used accept to the processor, which was formally the visitor. Instead of the visitor visiting the visited, you are in a sense inverting the paradigm; the visited becomes the visitor, since it is initially passed to the worker. You could do it; you wouldn’t call it a Visitor though.

    2. Conventional wisdom usually dictates that use of instanceof should be reserved for last-resort. Why would you use instanceof, when you can let the polymorphism of Java do it for you? One of the points of having objects is this benefit. If you do this, why not eschew overriding methods and just use instanceof to determine what to do in a class’s methods, instead of relying on dynamic dispatch, for the case of overriding methods?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an excel document with a master sheet, as well as several child
I want to use an AJAX update panel in a web master page ,
I have a simple page inside a master page (well, in a master in
We have a custom master page that is deployed to My Site, as well
I merged my branch into master, and pushed, thinking all was well. Turns out
Question is pretty well stated in the title. Normally I would use <link... />
How can we use validation controls in a master page. The master page has
One can do something like this: class master: @combomethod def foo(param): param.bar() # Param
I am reading some Objective-C code in a well-maintained GitHub repo. https://github.com/soffes/sskeychain/blob/master/SSKeychain.m I came
I have a typical master-detail scenario. User can click Add New in the master

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.