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:
Artist
- id
- name
- .. more scalar values ..
- worksOfArt <-- this is a collection as WorkOfArt instances
WorkOfArt
- id
- name
- .. more scalar values ..
- artists <-- this is a collection of Artist instances
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:
VisitableAggregateInterface
{
public function getVisitableChildren(); // this would return only visitable children
}
And then let both Artist and WorkOfArt extend an abstract class like this:
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.
*/
}
The goal is ultimately to end up with a concrete Visitor that will write out an XML file similar to this:
<?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>
Please advice: Am I going in the right direction here? Because the getVisitableChildren()
interface feels a bit quirky to me. Should I perhaps maybe even ditch the visitor pattern altogether and take a different approach?
Thanks.