我很努力将访问者模式应用于一些具有标量成员并且同时聚集成员(集合)的对象。在聚合对象上实现访问者模式
这些是我的对象:
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
正如你所看到的,结构也将是递归的,但这是以后关注我。 ;-)
我的问题是:什么是最好的方式来实现访问者模式,它允许我访问的对象,也只有他们可见的孩子(集合)。
我想创建一个像这样的接口:
VisitableAggregateInterface
{
public function getVisitableChildren(); // this would return only visitable children
}
然后让这两个艺术家和WorkOfArt扩展抽象类是这样的:
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.
*/
}
我们的目标是最终与一个具体落得将写出类似如下的XML文件的访问者:
<?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>
请指教:我在这里朝着正确的方向走吗?因为getVisitableChildren()
界面对我来说有点古怪。我应该甚至可能完全抛弃访问者模式并采取不同的方法?
谢谢。
你说服我留下访客模式。我并不知道访问者模式通常用于递归数据。我决定采取你建议的方法,虽然更灵活一点,因为我也需要输出部分片段。但你不可能知道这一点。谢谢。 – 2010-08-29 22:54:05