$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);
    {
        return array_push($this->_collection, $element);
    }
    public function detach(Element $element)
    {
        $index = array_search($element, $this->_collection);
        if ($index !== FALSE) {
            unset($this->_collection[$index]);
        }
        return $index;
    }
    public function accept(Visitor $visitor)
    {
        foreach ($this->_collection as $element) {
            $element->accept($visitor);
        }
    }
}
// client
$elementA = new ConcreteElementA("ElementA");
$elementB = new ConcreteElementB("ElementB");
$elementA2 = new ConcreteElementB("ElementA2");
$visitor1 = new ConcreteVisitor1();
$visitor2 = new ConcreteVisitor2();
$os = new ObjectStructure();
$os->attach($elementA);
$os->attach($elementB);
$os->attach($elementA2);
$os->detach($elementA);
$os->accept($visitor1);
$os->accept($visitor2);
Beispiel #3
0
<?php

header('Content-Type:text/html;charset=utf-8');
/*
 * 访问者模式【设备访问】
 * Author: Kaysen
 */
define('ROOT_PATH', dirname(__FILE__));
require_once ROOT_PATH . '/../../Loader.php';
$os = new ObjectStructure();
$pc = new PcBrowser();
$pc->name = '360';
$os->addVisitor($pc);
$mb = new MbBrowser();
$mb->name = 'uc';
$os->addVisitor($mb);
$visitor = new EquipmentAnalyze();
$os->handle($visitor);
 public function doSomething(ObjectStructure $objectStructure, Visitor $visitor)
 {
     $elementA = $objectStructure->getElementA($visitor);
     $elementB = $objectStructure->getElementB($visitor);
     return $elementA . $elementB;
 }
Beispiel #5
0
}
//对象结构
class ObjectStructure
{
    public $elementList = array();
    public function addElement(Element $element)
    {
        $this->elementList[spl_object_hash($element)] = $element;
    }
    public function delElement(Element $element)
    {
        unset($this->elementList[spl_object_hash($element)]);
    }
    public function accept(Visitor $visitor)
    {
        foreach ($this->elementList as $element) {
            $element->accept($visitor);
        }
    }
}
//客户端代码
$objectStructure = new ObjectStructure();
//对象结构
$objectStructure->addElement(new ConcreteElementA('男人'));
$objectStructure->addElement(new ConcreteElementA('女人'));
$va = new ConcreteVisitorA('成功');
$vb = new ConcreteVisitorB('失败');
$vc = new ConcreteVisitorC('恋爱');
$objectStructure->accept($va);
$objectStructure->accept($vb);
$objectStructure->accept($vc);
        array_push($this->obj, $ele);
    }
    /**
     *     *处理请求
     *         *@param $visitor Visitor
     *             */
    public function handleRequest(Visitor $visitor)
    {
        //遍历对象结构中的元素,接受访问
        foreach ($this->obj as $ele) {
            $ele->accept($visitor);
        }
    }
}
/*测试*/
header('Content-Type: text/html; charset=utf-8');
//对象结构
$os = new ObjectStructure();
//添加元素
$ele1 = new EnterpriseCustomer();
$ele1->name = 'ABC集团';
$os->addElement($ele1);
$ele2 = new EnterpriseCustomer();
$ele2->name = 'DEF集团';
$os->addElement($ele2);
$ele3 = new PersonalCustomer();
$ele3->name = '张三';
$os->addElement($ele3);
//客户提出服务请求
$serviceVisitor = new ServiceRequestVisitor();
$os->handleRequest($serviceVisitor);
Beispiel #7
0
    {
        $this->personList[spl_object_hash($person)] = $person;
    }
    public function delPerson(Person $person)
    {
        unset($this->personList[spl_object_hash($person)]);
    }
    public function accept(Action $action)
    {
        foreach ($this->personList as $person) {
            $person->accept($action);
        }
    }
}
//客户端代码
$ObjectStructure = new ObjectStructure();
$ObjectStructure->addPerson(new Man('男人'));
$ObjectStructure->addPerson(new Woman('女人'));
$success = new Success('成功');
$failing = new Failing('失败');
$amativeness = new amativeness('恋爱');
$ObjectStructure->accept($success);
$ObjectStructure->accept($failing);
$ObjectStructure->accept($amativeness);
/*
输出如下
男人成功时,背后多有一个伟大的女人
女人成功时,背后多一个不成功的男人
男人失败时,闷头喝酒谁也劝不了
女人失败时,眼泪旺旺谁也不用劝
男人恋爱时,遇事不懂也要装懂
Beispiel #8
0
<?php

require_once './User.php';
require_once './UserVisitor.php';
require_once './AddPointVisitor.php';
require_once './NormalUser.php';
require_once './VipUser.php';
require_once './ObjectStructure.php';
$add_point_visitor = new AddPointVisitor();
$os = new ObjectStructure();
$os->addUser(new VipUser("vip_hector"));
$os->addUser(new NormalUser("continue"));
$user_tom = new NormalUser("tom");
$os->addUser($user_tom);
$os->addUser($user_tom);
$os->handleVisitor($add_point_visitor);