Ejemplo n.º 1
0
 public function testObserver()
 {
     $subject = new ConcreteSubject();
     $subject->attach(new ConcreteObserver());
     $subject->attach(new ConcreteObserver());
     $subject->attach($c = new ConcreteObserver());
     $subject->state = "New State...";
     $subject->Notify();
     $this->assertEquals($subject->state, $c->state);
 }
Ejemplo n.º 2
0
 public function __construct()
 {
     $sub = new ConcreteSubject();
     $ob1 = new ConcreteObserverPhone();
     $ob2 = new ConcreteObserverTablet();
     $ob3 = new ConcreteObserverDT();
     $sub->attachObser($ob1);
     $sub->attachObser($ob2);
     $sub->attachObser($ob3);
     $sub->setState("decoCar");
 }
Ejemplo n.º 3
0
 public function __construct()
 {
     echo "Tworzenie 3 konkretnych obserwatorów i konkretnego podmiotu <BR>";
     $ob1 = new ConcreteObserver();
     $ob2 = new ConcreteObserver();
     $ob3 = new ConcreteObserver();
     $subject = new ConcreteSubject();
     $subject->setObservers();
     $subject->setData("Oto twoje dane!");
     $subject->attach($ob1);
     $subject->attach($ob2);
     $subject->attach($ob3);
     $subject->notify();
     echo "Odpięcie obserwatora ob3. Powiadamiane będą ob1 i ob2<BR>";
     $subject->detach($ob3);
     $subject->notify();
     echo "Reset danych, podpięcie ob3 i dopięcie ob2, tylko ob1 i ob3 będą powiadamiane: <BR>";
     $subject->setData("Dane dla obiektów ob1 i ob3");
     $subject->attach($ob3);
     $subject->detach($ob2);
     $subject->notify();
 }
Ejemplo n.º 4
0
    public function update(SplSubject $subject)
    {
        echo 'ConcreteObserver2 state is ', $subject->getState(), '<br>';
    }
}
/**
 * 具体观察者 3
 *
 */
class ConcreteObserver3 extends bserver implements SplObserver
{
    function __construct($state)
    {
        parent::__construct($state);
    }
    public function update(SplSubject $subject)
    {
        echo 'ConcreteObserver3 state is ', $subject->getState(), '<br>';
    }
}
$subject = new ConcreteSubject();
$observer1 = new ConcreteObserver1(1);
$observer2 = new ConcreteObserver2(2);
$observer3 = new ConcreteObserver3(3);
$subject->attach($observer1);
$subject->attach($observer2);
$subject->attach($observer3);
echo 'Subject state is 1', '<br>';
$subject->setState(1);
echo 'Subject state is 2', '<br>';
$subject->setState(3);
Ejemplo n.º 5
0
Archivo: 0.php Proyecto: ruyicoder/php
 * 具体通知者类
 */
class ConcreteSubject extends Subject
{
    public $state;
}
/*
 * 具体观察者
 */
class ConcreteObserver extends Observer
{
    private $_name;
    private $_subject;
    public function __construct($name, Subject $subject)
    {
        $this->_name = $name;
        $this->_subject = $subject;
    }
    public function update()
    {
        var_dump(sprintf('观察者:%s,通知者的状态是:%s,请做出相应的反应。', $this->_name, $this->_subject->state));
    }
}
###################################################################################
$concreteSubject = new ConcreteSubject();
//通知者
$concreteObserver = new ConcreteObserver('张三', $concreteSubject);
//观察者
$concreteSubject->attach($concreteObserver);
$concreteSubject->state = 'abc';
$concreteSubject->notify();
Ejemplo n.º 6
0
 public function testSubject()
 {
     $subject = new ConcreteSubject();
     $subject->addObserver(new ConcreteObserver());
     $subject->doSomething();
 }
Ejemplo n.º 7
0
<?php

require_once 'observer.php';
require_once 'subject.php';
require_once 'concreteSubject.php';
require_once 'observerA.php';
require_once 'observerB.php';
$subject = new ConcreteSubject();
$observerA = new ObserverA();
$observerB = new ObserverB();
$subject->registerObserver($observerA);
$subject->registerObserver($observerB);
$subject->setValue(5);
Ejemplo n.º 8
0
class ConcreteObserver implements Observer
{
    /**
     * 观察者的名称
     * @var <type>
     */
    private $_name;
    public function __construct($name)
    {
        $this->_name = $name;
    }
    /**
     * 更新方法
     */
    public function update()
    {
        echo 'Observer', $this->_name, ' has notified.<br />';
    }
}
//实例化类:
$subject = new ConcreteSubject();
/* 添加第一个观察者 */
$observer1 = new ConcreteObserver('Martin');
$observer2 = new ConcreteObserver('phppan');
$subject->attach($observer1);
//添加第一个观察者
$subject->attach($observer2);
//添加第二个观察者
$subject->detach($observer1);
//删除第一个观察者
$subject->notifyObservers();
Ejemplo n.º 9
0
 /**
  * Main program.
  */
 public static function main()
 {
     $subject = new ConcreteSubject();
     /* 添加第一个观察者 */
     $observer1 = new ConcreteObserver("Martin");
     $subject->attach($observer1);
     echo "\nThe First notify:\n";
     $subject->notifyObservers();
     /* 添加第二个观察者 */
     $observer2 = new ConcreteObserver("phppan");
     $subject->attach($observer2);
     echo "\nThe Second notify:\n";
     $subject->notifyObservers();
     /* 删除第一个观察者 */
     $subject->detach($observer1);
     echo "\nThe Third notify:\n";
     $subject->notifyObservers();
 }
Ejemplo n.º 10
0
    {
        echo 'I must have supper.I am so hungry.<br />';
    }
}
class Observer3 extends AbstractObserver
{
    function action()
    {
        echo 'I must go to bed.<br />';
    }
}
class Observer4 extends AbstractObserver
{
    function action()
    {
        echo 'I will go to see my classmate tomorrow.<br />';
    }
}
//test
$subject = new ConcreteSubject();
$subject->add('Observer1');
$subject->add('Observer2');
$subject->add('Observer3');
$subject->notify(9);
var_dump($subject->__get('observers'));
$subject->add('Observer4');
var_dump($subject->__get('observers'));
$subject->notify(10);
$subject->splice('Observer2');
var_dump($subject->__get('observers'));
$subject->notify(14);