Exemplo n.º 1
0
        }
    }
    public function buy()
    {
        //stub actions of buying
        $this->notifyObserver('purchased');
    }
}
class buyCDNotifyStreamObserver
{
    public function update(CD $cd)
    {
        $activity = "The CD named {$cd->title} by ";
        $activity .= "{$cd->band} was just purchased.";
        activityStream::addNewItem($activity);
    }
}
class activityStream
{
    public static function addNewItem($item)
    {
        //stub functions
        print $item;
    }
}
$title = 'Waste of a Rib';
$band = 'Never Again';
$cd = new CD($title, $band);
$observer = new buyCDNotifyStreamObserver();
$cd->attachObserver('purchased', $observer);
$cd->buy();
Exemplo n.º 2
0
    }
    public function buy()
    {
        $this->notifyObserver("purchased");
    }
}
//观察者类 后台处理
class buyCDNotifyStreamObserver
{
    public function update(CD $cd)
    {
        $activity = "The CD named {$cd->title} by ";
        $activity .= "{$cd->band} was just purchased.";
        activityStream::addNewItem($activity);
    }
}
//消息同事类 前台输出
class activityStream
{
    public static function addNewItem($item)
    {
        print $item;
    }
}
//测试实例
$title = "Waste of a Rib";
$band = "Never Again";
$cd = new CD($title, $band);
$observer = new buyCDNotifyStreamObserver();
$cd->attachObserver("purchased", $observer);
$cd->buy();