Re: When and where to use Visitor Pattern?
- From: "Daniel Parker" <danielaparker@spam?nothanks.windupbird.com>
- Date: Fri, 29 Apr 2005 08:06:06 -0400
"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
.
- References:
- When and where to use Visitor Pattern?
- From: Sam Hwang
- Re: When and where to use Visitor Pattern?
- From: Ilja Preuß
- Re: When and where to use Visitor Pattern?
- From: Rich MacDonald
- Re: When and where to use Visitor Pattern?
- From: Daniel Parker
- Re: When and where to use Visitor Pattern?
- From: Robert C . Martin
- Re: When and where to use Visitor Pattern?
- From: Daniel Parker
- Re: When and where to use Visitor Pattern?
- From: Robert C . Martin
- When and where to use Visitor Pattern?
- Prev by Date: Re: Confusion about splitting classes to allow sharing of resources
- Next by Date: Re: [Q] XP and Estimation
- Previous by thread: Re: When and where to use Visitor Pattern?
- Next by thread: Re: When and where to use Visitor Pattern?
- Index(es):
Relevant Pages
|