php设计模式 Visitor 访问者模式
程序员文章站
2022-06-06 21:57:44
复制代码 代码如下:
<?php
/**
* 访问者模式
*
* 表示一个作用于某对象结构中的各元素的操作,可以在不改变各元素的类的前提下定义作用于这些元素的新操作
*
*/
abstract class visitor
{
abstract public function visitcroncreteelementa($element);
abstract public function visitcroncreteelementb($element);
}
class concretevisitor1 extends visitor
{
public function visitcroncreteelementa($element)
{
echo get_class($element)." visit 1a<br/>";
}
public function visitcroncreteelementb($element)
{
echo get_class($element)." visit 1b<br/>";
}
}
class concretevisitor2 extends visitor
{
public function visitcroncreteelementa($element)
{
echo get_class($element)." visit 2a<br/>";
}
public function visitcroncreteelementb($element)
{
echo get_class($element)." visit 2b<br/>";
}
}
abstract class element
{
abstract public function accept($visitor);
}
class concreteelementa extends element
{
public function accept($visitor)
{
$visitor->visitcroncreteelementa($this);
}
}
class concreteelementb extends element
{
public function accept($visitor)
{
$visitor->visitcroncreteelementb($this);
}
}
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);
复制代码 代码如下:
<?php
/**
* 访问者模式
*
* 表示一个作用于某对象结构中的各元素的操作,可以在不改变各元素的类的前提下定义作用于这些元素的新操作
*
*/
abstract class visitor
{
abstract public function visitcroncreteelementa($element);
abstract public function visitcroncreteelementb($element);
}
class concretevisitor1 extends visitor
{
public function visitcroncreteelementa($element)
{
echo get_class($element)." visit 1a<br/>";
}
public function visitcroncreteelementb($element)
{
echo get_class($element)." visit 1b<br/>";
}
}
class concretevisitor2 extends visitor
{
public function visitcroncreteelementa($element)
{
echo get_class($element)." visit 2a<br/>";
}
public function visitcroncreteelementb($element)
{
echo get_class($element)." visit 2b<br/>";
}
}
abstract class element
{
abstract public function accept($visitor);
}
class concreteelementa extends element
{
public function accept($visitor)
{
$visitor->visitcroncreteelementa($this);
}
}
class concreteelementb extends element
{
public function accept($visitor)
{
$visitor->visitcroncreteelementb($this);
}
}
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);
上一篇: Python3.2.3官方文档(中文版)
下一篇: 企业互联网营销建营销型网站到底有没有用?