2017-06-14 25 views
1

目前我正在使用Visitor Pattern的项目。虽然这种模式的工作,我发现自己写这样的评语:不应该重构访问者模式吗?

* @param VisitorInterface $visitor The visitor to visit.

Visitor模式包括以下接口:

VisitorInterface { 
    public function visit($object); 
} 

VisitableInterface { 
    public function accept(VisitorInterface $visitor); 
} 

现在我的问题:应该不会是相反的?

VisitorInterface { 
    public function accept($object); 
} 

VisitableInterface { 
    public function visit(VisitorInterface $visitor); 
} 

因为现在访问者会接受访问的东西,因为访问者应该访问某些东西。而可见对象现在将接受访问者。

如:

class Party implements VisitableInterface { 
    public function visit(VisitorInterface $visitor) { 
     $visitor->accept($this); 
    } 
} 

class Human implements VisitorInterface { 
    public function accept($object) { 
     // do something with object 
    } 
} 

所以现在我们有一个接受游客的党。这些访客可以被要求接受派对上的某些事情来做些事情。

我希望能够解释一下我的想法,以证明我的“关注”。请不要责怪我试图打破设计模式:-)

回答

0

方法被调用对象与参数,而不是参数。所以,如果你打电话给human.visit(party),这意味着人类访问了一些东西,并且在参数中指定了什么方。