php设计模式 Observer(观察者模式)
程序员文章站
2023-02-26 15:08:53
复制代码 代码如下:
<?php
/**
* 观察者模式
*
* 定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新
* 能够便利地创建查看目标对象状态的对象,并且提供与核心对象非耦合的指定功能
* 插件系统
*/
class observerable
{
private $_observers = array();
public function registerobserver($observer)
{
$this->_observers[] = $observer;
}
public function removeobserver($observer)
{
$key = array_search($observer,$this->_observers);
if(!($key === false))
{
unset($this->_observers[$key]);
}
}
public function notifyobservers()
{
foreach($this->_observers as $observer)
{
if($observer instanceof observer) $observer->update($this);
}
}
}
interface observer
{
public function update($observer);
}
interface displayelement
{
public function display();
}
// -- 实例类定义
class newsobserverable extends observerable
{
private $_sports_news;
public function setsportsnews($data)
{
$this->_sports_news = $data;
$this->notifyobservers();
}
public function getsportsnews()
{
return $this->_sports_news;
}
private $_local_news;
public function setlocalnews($data)
{
$this->_local_news = $data;
$this->notifyobservers();
}
public function getlocalnews()
{
return $this->_local_news;
}
}
class sportsnews implements observer,displayelement
{
private $_data = null;
public function update($observer)
{
if($this->_data != $observer->getsportsnews())
{
$this->_data = $observer->getsportsnews();
$this->display();
}
}
public function display()
{
echo $this->_data.date("y-m-d h:i:s")."<br/>";
}
}
class localnews implements observer,displayelement
{
private $_data = null;
public function update($observer)
{
if($this->_data != $observer->getlocalnews())
{
$this->_data = $observer->getlocalnews();
$this->display();
}
}
public function display()
{
echo $this->_data.date("y-m-d h:i:s")."<br/>";
}
}
// -- 实例化 ---
$objobserver = new newsobserverable();
$local = new localnews();
$sports = new sportsnews();
$objobserver->registerobserver($local);
$objobserver->registerobserver($sports);
$objobserver->setsportsnews("sports news 1 ");
$objobserver->setlocalnews("local news 1 ");
$objobserver->removeobserver($sports);
$objobserver->setlocalnews("local news 2 ");
$objobserver->setsportsnews("sports news 2 ");
$objobserver->removeobserver($local);
$objobserver->setlocalnews("local news 3 ");
复制代码 代码如下:
<?php
/**
* 观察者模式
*
* 定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新
* 能够便利地创建查看目标对象状态的对象,并且提供与核心对象非耦合的指定功能
* 插件系统
*/
class observerable
{
private $_observers = array();
public function registerobserver($observer)
{
$this->_observers[] = $observer;
}
public function removeobserver($observer)
{
$key = array_search($observer,$this->_observers);
if(!($key === false))
{
unset($this->_observers[$key]);
}
}
public function notifyobservers()
{
foreach($this->_observers as $observer)
{
if($observer instanceof observer) $observer->update($this);
}
}
}
interface observer
{
public function update($observer);
}
interface displayelement
{
public function display();
}
// -- 实例类定义
class newsobserverable extends observerable
{
private $_sports_news;
public function setsportsnews($data)
{
$this->_sports_news = $data;
$this->notifyobservers();
}
public function getsportsnews()
{
return $this->_sports_news;
}
private $_local_news;
public function setlocalnews($data)
{
$this->_local_news = $data;
$this->notifyobservers();
}
public function getlocalnews()
{
return $this->_local_news;
}
}
class sportsnews implements observer,displayelement
{
private $_data = null;
public function update($observer)
{
if($this->_data != $observer->getsportsnews())
{
$this->_data = $observer->getsportsnews();
$this->display();
}
}
public function display()
{
echo $this->_data.date("y-m-d h:i:s")."<br/>";
}
}
class localnews implements observer,displayelement
{
private $_data = null;
public function update($observer)
{
if($this->_data != $observer->getlocalnews())
{
$this->_data = $observer->getlocalnews();
$this->display();
}
}
public function display()
{
echo $this->_data.date("y-m-d h:i:s")."<br/>";
}
}
// -- 实例化 ---
$objobserver = new newsobserverable();
$local = new localnews();
$sports = new sportsnews();
$objobserver->registerobserver($local);
$objobserver->registerobserver($sports);
$objobserver->setsportsnews("sports news 1 ");
$objobserver->setlocalnews("local news 1 ");
$objobserver->removeobserver($sports);
$objobserver->setlocalnews("local news 2 ");
$objobserver->setsportsnews("sports news 2 ");
$objobserver->removeobserver($local);
$objobserver->setlocalnews("local news 3 ");