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)?=

Community
  • 1
  • 1
recoInrelax
  • 697
  • 2
  • 16
  • 33
  • The example isn't written in Java (C# perhaps?) – Peter Lawrey Dec 01 '11 at 15:12
  • ya, i took as example and made the right modifications for java. it is tagged java idk why – recoInrelax Dec 01 '11 at 15:12
  • 2
    @recoInrelax Not sure what modifications you made to make it Java-y, it still isn't. – Dave Newton Dec 01 '11 at 15:13
  • ya, i'm not refering about the code. I simply copied from the other post. Forge the syntax, basically what is he saying is that, altought the first block of code is the 'pattern' way to implement the price feature, but it is too overkill in comparison to the second that is trivial and easier to do ? – recoInrelax Dec 01 '11 at 15:17
  • Example doesn't make sense no matter how you... *slice* it. – Doug Moscrop Dec 01 '11 at 15:18
  • Frankly that's not a good post, and there's good reason its not the accepted answer. – DJClayworth Dec 01 '11 at 15:45
  • Sorry, just to be clear I didn't mean this question is bad, I meant the answer it references. – DJClayworth Dec 01 '11 at 15:49
  • hm, the accepted answer is not that one, but as far as i know, the accept answer is given by the author of the post isn't? I may be wrong, and i'm looking for a good example of this pattern, but as far as i know, this example have twice the votes than the accepted answer and i find it easy to undertand, altought im not sure if it is the best answer. @Dᴀᴠᴇ Nᴇᴡᴛᴏɴ can i have your opinion here? – recoInrelax Dec 01 '11 at 16:01

2 Answers2

2

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.

DJClayworth
  • 26,349
  • 9
  • 53
  • 79
1

Add a class-specific price implementation to each sub-class.

The point is that sometimes a Visitor is overkill, and functionality can be added in a less-abstract fashion.

Now, whether or not it makes sense for a Fruit to have a Price is a different issue. It might make more sense for an Item to have a price, and that Item has a Fruit, or Fruit becomes a subclass of Item, or they're composited, or...

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • How about if different shops sell fruit at a different price? There are dozens of scenarios in which the trivial modification doesn't work, and that's when you (may) need a visitor. – DJClayworth Dec 01 '11 at 17:05
  • @DJClayworth ... obviously. IMO a visitor is still the wrong solution to the problem. I answered the question as asked. – Dave Newton Dec 01 '11 at 17:07