Re: When and where to use Visitor Pattern?



"Robert C. Martin" <unclebob@xxxxxxxxxxxxxxxx> wrote in message
news:3qh271hf9s63k1ooqqbs88csimoijsvrqj@xxxxxxxxxx
> On 28 Apr 2005 12:46:35 -0700, "Daniel Parker"
> <danielaparker@xxxxxxxxxxx> wrote:
>
>>Well, no, you need to query for the interfaces of interest. In most
>>cases that wouldn't be every possible derivative, it would be a small
>>number of interfaces that every possible derivative implements.
>
> I'm not following you here, unless you are talking about Acyclic
> Visitor.
>
> I thought that you were proposing:
> if (x instanceof D1)...
> else if (x instanceof D2)...
> else if (x instanceof D3)...
>

I was thinking more along the lines of D1 implements I1, D2 implements I1,
D3 implements I2, and querying on the I's, a smaller and more stable set.
In the modem example that would be HayesModem1 implements HayesModem,
HayesModem2 implements HayesModem, etc....but I'm not actually proposing
that here.
>
> Clearly in this case, if there are N derivative, then we will do N/2
> instanceof calls on average. Visitor, on the other hand, make just
> two polymorphic dispatches (one to accept, and the other to visit).
> Acyclic visitor makes *one* instanceof call in addition to these two
> polymorphic dispatches.

You're right, a long list of instanceof's is a very inefficent way of
solving this problem but...
>
>>There doesn't seem to be a fundumental difference here.
>

>... There is also a big difference in the dependency structure.

I don't see it. What is the difference in the dependency structure between:

> if (x instanceof D1)...
> else if (x instanceof D2)...
> else if (x instanceof D3)...

and

class Configurator implements ModemVisitor {
void visit(D1);
void visit(D2);
void visit(D3);
}

Both enumerate all the D's.

It seems to me that for a configuration problem like this, you want an
associate map from a Modem to a ModemConfigurator. You want to be able to
add a new Modem, say HayesModem99 of a certain type without having to add a
new Configurator, but be able to use an existing configurator that works for
that type, albeit less effiiciently, and maybe later provide a specialized
configurator. And you'd like to at some point to move these settings out of
code, into a configuration file.

I don't think that road leads to Visitor. And I don't think starting with
Visitor helps going that road.

Regards,
Daniel Parker


.



Relevant Pages

  • Re: Improving String.equals() implementation
    ... instanceof test anyhow: ... But that still entails a null test when you get down to the machine code, unless, perhaps, you implement null internally as a reference to a singleton object of class $Null -- which introduces some weird twists and turns of its own. ... The classID would tend to be a pointer to a data structure representing information such as superclass and inherited interfaces, ...
    (comp.lang.java.programmer)
  • Re: 7.0 wishlist?
    ... There isn't any real distinction between marker interfaces ... it's also not possible to make obj instanceof FooInterface ... An enum that can be any of X, Y, or Z is a subtype, ... enum Sub wrapspartsof Base { ...
    (comp.lang.java.programmer)