|
Posted by FluffyCat on 11/29/05 19:10
I know these patterns can look like spaghetti code at first, but if
you will be doing any serious OO programming they are extremely
important. The visitor pattern is absolutely one that is used and
useful, so it is worth taking the time to understand it.
Sometimes I can describe these patterns well and sometimes not.
Judging from the response this is probably a not. Maybe I should have
said "the current instance of itself" instead, which is what $this is,
instead of "an instance of itself", which could imply a new instance.
Maybe instead of describing these patterns I should just have the UML
and the code. I don't have a tool to create UML for PHP yet, which
defiantely hurts my examples.
I try to make these examples as simple as possible, while sticking to
the original intent of the pattern. In the GoF on page 334 - 335 the
UML shows anObjectStructure calling the concrete element (which I call
the visitee) with accept(Visitor), the concrete element then calls the
visitor, which in turn calls a method back in the concrete element.
So, I've just used my "testVisitor" in place of anObjectStructure. To
me, the anObjectStructure just kicked things off by calling the
concrete element with a visitor, so I gave the simplest representation
I could of it. Do you really think this example doesn't meet the
required intent of the visitor pattern, at least in it's simplest
form?
A useful example of the visitor pattern could show the visitor being
able to create HTML or XML output for the same concrete element. I
didn't do this because it would "muddy the waters" of the example with
whatever overhead I'd need to add to produce the HTML and XML. That
might be a nice second more robust example of the visitor pattern to
do after I complete my simple versions of all the most important
design patterns.
http://www.fluffycat.com/SDCMSv2/PHP-Design-Patterns-Visitor/
p.s. - even if you weren't exacly crazy about it, thanks very much for
checking out my pattern example and commenting on it!
On 29 Nov 2005 04:43:02 -0800, "Oli Filth" <catch@olifilth.co.uk>
wrote:
>Chung Leong wrote:
>> FluffyCat wrote:
>> > New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern
>> > Examples - the Visitor Pattern.
>> >
>> > In the Visitor pattern, one class calls a function in another class
>> > and passes an instance of itself. The called class has special
>> > functions for each class that can call it.
>> >
>> > With the visitor pattern, the calling class can have new operations
>> > added without being changed itself.
>> >
>> > http://www.fluffycat.com/SDCMSv2/PHP-Design-Patterns-Visitor/
>>
>> A class that passes an instance of itself to another class means you
>> don't freaking know what you're doing. What's next? The spaghetti
>> design pattern?
>
>I think the OP has phrased this badly. He actually has an object
>passing this ($this) to a method in another object, which is hardly
>uncommon practice in most OO languages (e.g. registering a callback
>interface with a child object).
>
>However, the OP's example is not particularly great, as it's all being
>driven from outside the objects in question.
Navigation:
[Reply to this message]
|