PHP design pattern Observer (Viewer mode) _php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/**
* Viewer Mode
*
* Defines a one-to-many dependency between objects so that all objects that depend on it are notified and automatically refreshed when the state of an object changes
* Ability to conveniently create objects that view the state of the target object and provide non-coupled, specified functionality to the core object
* Plug-in System
*/
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 ();
}

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

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

--Instantiation---

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

http://www.bkjia.com/PHPjc/323800.html www.bkjia.com true http://www.bkjia.com/PHPjc/323800.html techarticle Copy the code as follows:? PHP/** * Viewer mode * Defines a one-to-many dependency between objects so that when the state of an object changes, all objects that depend on it ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.