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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:31:10+00:00 2026-05-27T08:31:10+00:00

I was doing some homework today with the visitor-pattern, and i made a visitor

  • 0

I was doing some homework today with the visitor-pattern, and i made a visitor that looked somewhat like this (edited sample code from wikipedia):

class CarElementPrintVisitor implements CarElementVisitor {
    public void visit(CarElement element) {      
        if (element.getClass() == Wheel.class)
        {
            return visit((Wheel)element);
        }
        else if (element.getClass() == Engine.class)
        {
            return visit((Engine)element);
        }
        else if (element.getClass() == Body.class)
        {
            return visit((Body)element);
        }
        else //if (v.getClass() == Car car.class)
        {
            return visit((Car)element);
        }
    }

    public void visit(Wheel wheel) {      
        System.out.println("Visiting " + wheel.getName() + " wheel");
    }

    public void visit(Engine engine) {
        System.out.println("Visiting engine");
    }

    public void visit(Body body) {
        System.out.println("Visiting body");
    }

    public void visit(Car car) {      
        System.out.println("Visiting car");
    }
}

The “public void visit(CarElement element)” method is kind of ugly (long and needs to be maintained if more CarElements are added) but i want to keep the method, so i tried to do it better.

I ended up trying this out:

    public void visit(CarElement element) {      
        return visit(element.getClass().cast(element));
    }

But that just returns “visit(CarElement element)”, even though element.getClass() returns the correct class, so it ends up in a infinite loop.

Does anyone know how to do, what i’m trying to do? (If it is even possible, i’m not sure).

  • 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-27T08:31:11+00:00Added an answer on May 27, 2026 at 8:31 am

    You missed what makes the beauty of the visitor pattern: the accept(CarElementVisitor) method that must be in the CarElement interface, and which calls the visitor back with itself, using the approriate type. Re-read the wikipedia article.

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

Sidebar

Related Questions

This is a homework problem that I have. I have been doing some research
I was working on my advanced calculus homework today and we're doing some iteration
Doing some code reviews lately I came across a number of classes that have
I'm doing some homework, and having a hard time understanding closures. This is in
I would like to mention that this is a homework but am not asking
I'm doing some homework with LDAP that consists of creating LDIF files, adding them
I'm doing some homework in Lisp, using clisp to test, and I'm loading this
I've been doing some homework, wrote some code and can't actually find the reason
while doing some homework in my very strange C++ book, which I've been told
I'm doing some homework and while I have some experience with SML, Haskell has

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.