|
Posted by Oli Filth on 10/15/22 11:33
FluffyCat wrote:
> 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.
>
> 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.
Exactly; what's the point in calling Visitee->accept(Visitor), whose
only job is to call Visitor->visit(this)? If ObjectStructure has a
reference to both Visitor and Visitee, why doesn't it just call
Visitor->visit(Visitee)? This would make for a simpler, more intuitive
arrangement.
> 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.
But even that wouldn't require the double dispatch, e.g.:
$book = new Book();
$HTMLThing = new HTMLThing();
$XMLThing = new XMLThing();
$HTMLThing->display($book);
$XMLThing->display($book);
P.S.: Please don't top-post...
--
Oli
Navigation:
[Reply to this message]
|