Re: When and where to use Visitor Pattern?
- From: Robert C. Martin <unclebob@xxxxxxxxxxxxxxxx>
- Date: Thu, 28 Apr 2005 15:31:52 -0500
On 28 Apr 2005 12:46:35 -0700, "Daniel Parker"
<danielaparker@xxxxxxxxxxx> wrote:
>
>Robert C. Martin wrote:
>> On Wed, 27 Apr 2005 00:51:29 -0400, "Daniel Parker"
>> <danielaparker@spam?nothanks.windupbird.com> wrote:
>>
>> >wouldn't "instanceof" do just as well? And be a little
>> >clearer? With less clutter in the core class (no visit method)?
>>
>>
>> if (x instanceof D1)...
>> else if (x instanceof D2)...
>> else if (x instanceof D3)...
>> ...
>
>> No, instanceof does not do just as well. To use instanceof you have
>> to create a if/else statement that depends on every possible
>> derivative:
>
>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)...
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.
> But
>lets say you do, take the modem example in your article...
>>
>> Not only is this time consuming, it's also heavy with dependencies.
>> visitor (especially acylic visitor) mitigates both of these problems.
>>
>I don't see it. Your UnixModemConfigurator has to implement visit
>methods for all the derivatives of interest, much as instanceof has to
>query for the derivatives of interest.
Implementing a visit method does not take execution time. Instanceof
does.
>There doesn't seem to be a fundumental difference here.
There is a huge difference in the execution timeline. There is also a
big difference in the dependency structure.
-----
Robert C. Martin (Uncle Bob) | email: unclebob@xxxxxxxxxxxxxxxx
Object Mentor Inc. | blog: www.butunclebob.com
The Agile Transition Experts | web: www.objectmentor.com
800-338-6716
"The aim of science is not to open the door to infinite wisdom,
but to set a limit to infinite error."
-- Bertolt Brecht, Life of Galileo
.
- Follow-Ups:
- Re: When and where to use Visitor Pattern?
- From: Daniel Parker
- Re: When and where to use Visitor Pattern?
- From: Nicholls . Mark
- Re: When and where to use Visitor Pattern?
- 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
- When and where to use Visitor Pattern?
- Prev by Date: Re: When and where to use Visitor Pattern?
- Next by Date: Re: Something about ACTIVE OBJECT pattern
- Previous by thread: Re: When and where to use Visitor Pattern?
- Next by thread: Re: When and where to use Visitor Pattern?
- Index(es):