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

The Archive Base Latest Questions

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

I was studying the visitor pattern and came across this useful example: https://stackoverflow.com/a/2604798/974594 .

  • 0

I was studying the visitor pattern and came across this useful example: https://stackoverflow.com/a/2604798/974594. The post is very clear and very easy to understand, altought, i’m having problems understanding the last part, starting here:

With that said, visitors are usually overkill, and they have a
tendency grossly complicate APIs, and it can be very cumbersome to
define a new visitor for every new kind of behavior.

Usually, simpler patterns like inheritance should be used in place of
visitors. For example, in principle I could write a class like:

class FruitPricer : IFruitVisitor
{
    public double Price { get; private set; }
    public void Visit(Orange fruit) { Price = 0.69; }
    public void Visit(Apple fruit) { Price = 0.89; }
    public void Visit(Banana fruit) { Price = 1.11; }
}

It works, but what’s the advantage over this trivial modification:

abstract class Fruit

{
    public abstract void Accept(IFruitVisitor visitor);
    public abstract double Price { get; }
}

I’m not getting what is he saying here. I mean, if he now want to implement the feature ‘price’, what must be changed/added to the existing code (based on this pattern approach)?=

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

    The answer you reference misses much of the point of Visitor. It says “visitors are used to implement type-testing without sacrificing type-safety”, which is simply incorrect. The GOF book says “visitors let you define a new operation without changing the classes of the elements on which it operates”. Visitors can certainly be used for testing objects on things other than type, and for carrying out operations on objects that don’t involve testing.

    “Visitor is overkill” is frequently stated, but it’s usually said by people trying to use Visitor for things it wasn’t intended for, and then finding that -surprise – it doesn’t really work for it.

    The poster is right in that the second piece of code they quote is a much easier way of implementing the functionality, but it misses the point in that Visitor is intended for when you don’t want to modify the Fruit class.

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

Sidebar

Related Questions

I was studying method overriding in Java when ai came across the this keyword.
I was studying an open source code where I came across the following line
studying some sample code from an iOS programming course (cs193p fall2010) i came across
After studying the Perl require docs and other links like this on Stackoverflow I'm
While studying an implementation of Group signature I came across code where Group certificate
I am studying about various trees, and came across AVL trees and splay trees.
In studying actionscript 3's graphics class, I've come across the undocumented drawRoundRectComplex() method. It's
After studying the docs for a while, I came up with my first jQuery
While studying for the SCJP 6 exam, I ran into this question in a
Currently I am studying Standard Template Library (STL). In this program I am storing

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.