代表这种模式表示一个作用于某对象结构中的各元素的操作它允许您在其运作时不改变各元素的类情况下定义一个新的操作
ConcreteVisitor2(具体访问者2)
This class implements each operation declared by Visitor. Each operation implements a fragment(碎片) of the algorithm defined for the corresponding class of object in the structure. ConcreteVisitor provides the context for the algorithm and stores its local state. This state often(['ɔfən, 'ɔftən, 'ɔ:-] 时常)accumulates (累积)results during(在…的时候) the traversal(遍历) of the structure.
(这个类实现了访客所宣布的每个操作。每个操作实现了在这结构中的对象相应的类中算法的一个片段。ConcreteVisitor提供算法和存储本地状态的上下文。在结构中遍历的时候,这个状态时常积累结果。)
ConcreteVisitor1(具体访问者1)
This class implements each operation declared by Visitor. Each operation implements a fragment(碎片) of the algorithm defined for the corresponding class of object in the structure. ConcreteVisitor provides the context for the algorithm and stores its local state. This state often(['ɔfən, 'ɔftən, 'ɔ:-] 时常)accumulates (累积)results during(在…的时候) the traversal(遍历) of the structure.
(这个类实现了访客所宣布的每个操作。每个操作实现了在这结构中的对象相应的类中算法的一个片段。ConcreteVisitor提供上下文的算法和存储本地状态。在结构中遍历的时候,这个状态时常积累结果。)
Visitor(访问者)
This class declares a Visit operation for each class of ConcreteElement in the object structure. The operation's name and signature identifies the class that sends the Visit request to the visitor. That lets the visitor determine(决定) the concrete class of the element being visited. Then the visitor can access the element directly through its particular([pə'tikjulə] 待定地) interface.
(这类为该对象结构中的每一个具体元素类声明一个访问操作。操作的名字和标识该类的签名发送访问请求到客人。让客人确定被访问元素的具体类。然后客人就可以直接访问该元素通过其特定的接口。)
ConcreteElementB(具体元素B)
This class implements an Accept operation that takes a Visitor as an argument.
(这个类实现了一个接受操作,采用访问者作为参数。)
ConcreteElementA(具体元素A)
This class implements an Accept operation that takes a Visitor as an argument.
(这个类实现了一个接受操作,采用访问者作为参数。)
Element(元素)
This class defines an Accept operation that takes a visitor as an argument.
(这个类定义了一个接受操作,以一个访客作为参数。)
ObjectStructure(对象结构)
This class can enumerate(枚举) its elements, may provide a high-level interface to allow the visitor to visit its elements and may either be a composite(复合) or a collection such as a list or a set.
(这个类可以列举它的元素,可以提供一个高级的接口,允许访问者访问它的元素,可以是复合或收集,比如列表或集合。)
<?php
class ConcreteVisitor1 extends Visitor
{
public function visitCroncreteElementA($element) {
echo get_class($element) . " visit 1A";
}
public function visitCroncreteElementB($element) {
echo get_class($element) . " visit 1B";
}
}
class ConcreteVisitor2 extends Visitor
{
public function visitCroncreteElementA($element) {
echo get_class($element) . " visit 2A";
}
public function visitCroncreteElementB($element) {
echo get_class($element) . " visit 2B";
}
}
abstract class Visitor {
abstract public function visitCroncreteElementA($element);
abstract public function visitCroncreteElementB($element);
}
class ConcreteElementA extends Element
{
public function accept($visitor) {
$visitor->visitCroncreteElementA($this);
}
}
class ConcreteElementB extends Element
{
public function accept($visitor) {
$visitor->visitCroncreteElementB($this);
}
}
abstract class Element {
abstract public function accept($visitor);
}
class ObjectStructure
{
private $_elements = array();
public function attach($element) {
$this->_elements[] = $element;
}
public function detach($element) {
if ($key = array_search($element, $this->_elements) !== false)
unset($this->_elements[$key]);
}
public function accept($visitor) {
foreach ($this->_elements as $element) {
$element->accept($visitor);
}
}
}
$objOS = new ObjectStructure();
$objOS->attach(new ConcreteElementA());
$objOS->attach(new ConcreteElementB());
$objCV1 = new ConcreteVisitor1();
$objCV2 = new ConcreteVisitor2();
$objOS->accept($objCV1);
$objOS->accept($objCV2);
评论