设计模式-观察者模式
作者:edwin
日期:2019-09-13 20:15:22
所属分类:后端 - php

关联该模式定义了对象之间的一对多的依赖关系以便当一个对象改变状态时所有依赖于它的对象都得到通知并自动刷新

ConcreteObserver(具体的观察者)
This class maintains a reference to a ConcreteSubject object, stores state that should stay(保持) consistent(一致的) with the subject's and implements the Observer updating interface to keep its state consistent with the subject's.
(这类维护了一个ConcreteSubject对象的引用,存储状态应和主题的状态保持一致,并为了保持其状态和主题的状态一致而实现观察者更新界面。)

ConcreteSubject(具体的主题)
This class stores the state of interest to ConcreteObserver objects and sends notification to its observers when its state changes.
(这类存储兴趣的状态给所有观察都对象,并且当其状态改变时,应发送通知给它所有的观察者。)

Observer(观察者)
This class defines an updating interface for objects that should be notified of changes in a subject.
(这个类为主题中应该被通知的对象定义了一个更新接口)

Subject(主题)
This class knows its observers and provides an interface for attaching and detaching Observer objects.
(这类知道其所有的观察者和提供了一个接口用于附加和分离观察者对象。)

代码实现

<?php

interface DisplayElement
{
    public function display();
}
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") . "";
    }
}


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") . "";
    }
}

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;
    }
}

interface Observer
{
    public function update($observer);
}

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);
        }
    }
}

$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 ");

评论

全部评论 / 0