PHP in-depth viewer mode

Source: Internet
Author: User
# # # #观察者模式 * Designed to let an object track a state, know how the state changes, once the state changes, all subscribed objects can be notified # # #使用SPL实现观察者模式 * Splsubject interface has three abstract methods attach (), Detach (), notify () These methods do not return any results * Splobserver interface has a method update () it will get the latest state of the subject state and hand it to the Observer instance * Splobjectstorage Class is described as a mapping from an object to a set of data or objects
 Observers = new Splobjectstorage ();    }//Add observer public Function attach (\splobserver $observer) {$this->observers->attach ($observer);    }//Delete Observer public function detach (\splobserver $observer) {$this->observers->detach ($observer); }//Notification forward public function notify () {foreach ($this->observers as $observer) {$observer->update        ($this);    }}//Set data public function SetData ($dataNow) {$this->data = $dataNow;    } public Function GetData () {return $this->data; }}//SPL Specific Viewer class Concreteobserver implements splobserver{Public Function update (Splsubject $subject) {echo $    Subject->getdata (); }}class client{Public Function __construct () {echo "

Create three new concrete observers "; Create observer $ob 1 = new Concreteobserver (); $ob 2 = new Concreteobserver (); $ob 3 = new Concreteobserver (); $subject = new ConcreteSubject (); $subject->setobservers (); $subject->setdata ("Here is U data".)
"); $subject->attach ($ob 1); $subject->attach ($ob 2); $subject->attach ($ob 3); $subject->notify (); echo "

Reset Data and Reattach OB3

"; $subject->setdata ("More data, only".)
"); $subject->attach ($ob 3); $subject->attach ($ob 2); $subject->notify (); }} $data = new Client (); "' # # #自由的PHP和观察者模式" Observers, $obser); The Public Function Detachobser (Observer $obser) {//Removes data from the array $position = 0; foreach ($this->observers as $viwer) {+ + $position; if ($viwer = = $obser) {array_splice ($this->observers, ($position), 1); }}} public function notify () {foreach ($this->observers as $viewer) {//Call the Update method $viewer->update ($this); }}}class ConcreteSubject extends subject{public function setState ($stateSet) {$this->statenow = $stateSe T } public Function GetState () {return $this->statenow; }}class Concreteobserverdt implements observer{private $currentState; Public Function Update (Subject $subject) {$this->currentstate = $subject->getstate (); echo "DT"; }}class Concreobservertablet implements observer{private $currentState; Public Function Update (Subject $subject) {$this->curreNtstate = $subject->getstate (); echo "tablet"; }}class Concreobserverphone implements observer{private $currentState; Public Function Update (Subject $subject) {$this->currentstate = $subject->getstate (); echo "Phone"; }}class client{Public Function __construct () {$sub = new ConcreteSubject (); $ob 1 = new Concreobserverphone (); $ob 2 = new Concreobservertablet (); $ob 3 = new Concreteobserverdt (); $sub->attachobser ($ob 1); $sub->attachobser ($ob 2); $sub->attachobser ($ob 3); $sub->setstate ("fuck"); $sub->detachobser ($ob 1); $sub->notify (); }} $data = new Client ();
  • 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.