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 6967265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:17:56+00:00 2026-05-27T16:17:56+00:00

In this question it is said I can use visitor pattern instead of a

  • 0

In this question it is said I can use visitor pattern instead of a bunch of instanceofs. Jmg said “If you are not free to change A, B, and C, you could apply the visitor pattern to achieve the same.”

As far as I understand I still have to make A, B and C support visitor (have an accept() method, for example).

My problem is I have absolutely no possibility to change A, B and C. I just get the Car object from the foreign library and have to call wash() method specific for trucks, race cars and buses.

I think I still need an if-else-ifconstruction with instanceofs. Am I right?

  • 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-05-27T16:17:56+00:00Added an answer on May 27, 2026 at 4:17 pm

    Yes, to implement the visitor pattern now you need access to the source of A, B and C, unless all the classes have the same signature (so all have the wash() method with the same name). If that’s the case, you can use polymorphism to call the correct method.

    Otherwise, it is possible to add functionality to classes you don’t have access to at source code level. On the Wikipedia article on the Visitor pattern (http://en.wikipedia.org/wiki/Visitor_pattern) there is a small footnote below the Java example:

    Note: A more flexible approach to this pattern is to create a wrapper class implementing the interface defining the accept method. The wrapper contains a reference pointing to the CarElement which could be initialized through the constructor. This approach avoids having to implement an interface on each element. [see article Java Tip 98 article below]

    It references this article: https://www.infoworld.com/article/2077602/java-tip-98–reflect-on-the-visitor-design-pattern.html

    So, all in all it’s possible, but it gives an enormous number of classes for the small task you want to do. I’d stick with the instanceof’s if I were you.

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

Sidebar

Related Questions

Contiuned from : This is my question So now i can use this code
In this question , it is said in the most upvoted answer: Use liquid
In answer to this question Joel Coehoorn said Finally, only after the site's gone
In a comment on this answer to another question , someone said that they
This question is about removing sequences from an array, not duplicates in the strict
Two days ago I wrote this question: How can I retrieve an object on
There was another question with an answer saying that you can use event.down.x to
I've made this question: PHP mutual exclusion (mutex) As said there, I want several
This is an interview question: how can we store the objects of let's say
I fully admit I am a Ruby newb, and this question could come simply

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.