Beispiel #1
0
    abstract function update();
}
class AObserver extends Observer
{
    function AObserver($name, $subject)
    {
        parent::Observer($name, $subject);
    }
    function update()
    {
        echo $this->name . "收到通知";
    }
}
class BObserver extends Observer
{
    function BObserver($name, $subject)
    {
        parent::Observer($name, $subject);
    }
    function update()
    {
        echo $this->name . "收到通知";
    }
}
$boss = new Boss();
$a = new AObserver('a', $boss);
$b = new BObserver('b', $boss);
$boss->attach($a);
$boss->attach($b);
$boss->notify();
Beispiel #2
0
<?php

header('Content-Type:text/html;charset=utf-8');
/*
 * 观察者模式【通知】
 * Author: Kaysen
 */
define('ROOT_PATH', dirname(__FILE__));
require_once ROOT_PATH . '/../../Loader.php';
// 老板胡汉三
$huhansan = new Boss();
// 看股票的同事
$tongshi1 = new StockObserver('魏关姹', $huhansan);
// 看NBA的同事
$tongshi2 = new NBAObserver('易管查', $huhansan);
$huhansan->attach($tongshi1);
$huhansan->attach($tongshi2);
//$huhansan->detach($tongshi1);
// 老板回来
$huhansan->setSubjectStatus('我胡汉三回来了!');
// 发出通知
$huhansan->notify();