Observer pattern for Design patterns (PHP implementation)

Source: Internet
Author: User

GitHub Address: Https://github.com/ZQCard/design_pattern
/**/

(1) Subject.class.php (target Class)

<?phpnamespace Observer;classsubject{Private $state; Private $observers= [];  Public functiongetState () {return $this-State ; }     Public functionSestate ($state)    {        $this->state =$state; $this-notifyallobjects (); }     Public functionRegister (Observer$object)    {        $this->observers[] =$object; }     Public functionRemove$object)    {        foreach($this->observers as $key=$observer) {            if($observer==$object){                unset($this->observers[$key]); }        }    }     Public functionnotifyallobjects () {foreach($this->observers as $observer) {            $observer-update (); }    }}

(2) OBSERVER.CLASS.PHP (Observer abstract parent class)

<? phpnamespace Observer; Abstract class observer{    protected$subject;      Public Abstract function update ();}

(3) ObjectA.class.php (Concrete abstract Class A)

<?phpnamespace Observer;classObserveraextendsobserver{Private $myState= ' init A ';  Public function__construct (Subject$subject)    {        $this->subject =$subject; $this->subject->register ($this); }     Public functionUpdate () {$this->mystate =$this->subject->getState (); }     Public functiongetsate () {return"Observera Get state:".$this-MyState; }}

(4) ObserverB.class.php (specific observer B)

<?phpnamespace Observer;classObserverbextendsobserver{Private $myState= ' init B ';  Public function__construct (Subject$subject)    {        $this->subject =$subject; $this->subject->register ($this); }     Public functionUpdate () {$this->mystate =$this->subject->getState (); }     Public functiongetsate () {return"Observerb Get state:".$this-MyState; }}

(5) observer.php

<? phpspl_autoload_register (function ($className) {    $className  Str_replace(' \ \ ', '/',$className)    ; include $className. ". Class.php ";});  Use Observer\observera;  Use Observer\observerb;  Use observer\subject;/*
Observera Get State:init A
Observerb Get State:init B
Observera Get State:hello
Observerb Get State:hello

*/
$subject=NewSubject ();$objectA=NewObservera ($subject); $objectB=NewObserverb ($subject); Echo $objectA-getsate ();Echo' <br/> '; Echo $objectB-getsate ();Echo' <br/> '; $subject->sestate ("Hello"); Echo $objectA-getsate ();Echo' <br/> '; Echo $objectB-getsate ();Echo' <br/> ';

Observer pattern for Design patterns (PHP implementation)

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.