Implementing visitor pattern on aggregate objects

yazari

New Member
I'm struggling with applying the visitor pattern on some objects that have scalar members and at the same time aggregate members (collections).These are the objects that I have:\[code\]Artist - id - name - .. more scalar values .. - worksOfArt <-- this is a collection as WorkOfArt instancesWorkOfArt - id - name - .. more scalar values .. - artists <-- this is a collection of Artist instances\[/code\]As you can see, the structure will also be recursive, but that's of later concern to me. ;-)My question is: what is the best way to implement the visitor pattern, that allows me visit the objects and also only their visitable children (the collections).I thought creating an interface like this:\[code\]VisitableAggregateInterface{ public function getVisitableChildren(); // this would return only visitable children}\[/code\]And then let both Artist and WorkOfArt extend an abstract class like this:\[code\]VisitableAggregateAbstract implements VisitableAggregateInterface{ public function accept( Visitor $visitor ) { $visitor->visit( $this ); foreach( $this->getVisitableChildren() as $visitableChild ) { $visitableChild->accept( $visitor ); } } /* VisitableAggregateInterface::getVisitableChildren() will be implemented by Artist and WorkOfArt and will only return visitable children (like collections), and not scalar values. */}\[/code\]The goal is ultimately to end up with a concrete Visitor that will write out an XML file similar to this:\[code\]<?xml version="1.0" encoding="utf-8" standalone="yes"?><artgallery> <artists> <artist> <id>1</id> <name></name> <worksOfArt> <workOfArt refid="11"/> <workOfArt refid="12"/> </worksOfArt> <artist> <artists> <worksOfArt> <workOfArt> <id>11</id> <artists> <artist refid="1"/> </artists> <name></name> <info><![CDATA[some info]]></info> </workOfArt> <workOfArt> <id>12</id> <artists> <artist refid="1"/> </artists> <name></name> <info><![CDATA[some info]]></info> </workOfArt> </worksOfArt></artgallery>\[/code\]Please advice: Am I going in the right direction here? Because the \[code\]getVisitableChildren()\[/code\] interface feels a bit quirky to me. Should I perhaps maybe even ditch the visitor pattern altogether and take a different approach?Thanks.
 
Back
Top