The Observer pattern for PHP design Patterns

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif;" ><?php</span>
/** * Interface Observable * Define a Observable Interface * @author jichao.wang */interface observable{function Attac    H (Observer $observer);    function Detach (Observer $observer); function notify ();}    /** * Class Login * @author Jichao.wang */class Login implements observable{private $observers;    Public $status;    Public $ip;    Const LOGIN_ACCESS = 1;    Const LOGIN_WRONG_PASS = 2;    Const LOGIN_USER_UNKNOWN = 3;    function __construct () {$this->observers = array (); }/** * @param Observer $observer * @author Jichao.wang * Attach a Observer * function attach (obser    Ver $observer) {$this->observers[] = $observer; }/** * @param Observer $observer * @author Jichao.wang * Detach a Observer * function detach (obser        Ver $observer) {$newObservers = array (); foreach ($this->observers as $key = + $obs) {if ($obs!== $observer) {$newObservers [] = $  Obs          }} $this->observers = $newObservers; }/** * @author Jichao.wang * Handle Observer Notify * function Notify () {foreach ($this-&gt        ; observers as $obs) {$obs->update ($this);        }}/** * @author Jichao.wang * Execute login */function Handlelogin () {$ip = rand (1,100);                Switch (rand (1, 3)) {Case 1: $this->setstatus (self::login_access, $IP);                $ret = true;            Break                Case 2: $this->setstatus (Self::login_wrong_pass, $IP);                $ret = false;            Break                Case 3: $this->setstatus (Self::login_user_unknown, $IP);                $ret = false;        Break        }/** * Handle event */$this->notify ();    return $ret; }/** * @param $status * @author Jichao.wang * Set Login status */FunctioN SetStatus ($status, $ip) {$this->status = $status;    $this->ip = $ip; }/** * @return mixed * @author Jichao.wang * Get login Status */function GetStatus () {R    Eturn $this->status; }}/** * Interface Observer * @author Jichao.wang */interface Observer {function update (Observable $observable);}  /** * Class emailobserver * @author Jichao.wang */class Emailobserver implements Observer {function update (Observable        $observable) {$status = $observable->getstatus ();            if ($status = = login::login_access) {//$this->sendmail (' User ip: '. $observable->ip. ' Login successful! '); echo __class__. ' User ip: '. $observable->ip. ' Login successful! '        ------------------'; if ($status = = Login::login_wrong_pass) {//$this->sendmail (' User ip: '. $observable->ip. ' Login failed, password wrong! ')            ; echo __class__. ' User ip: '. $observable->ip. ' Login failed, password wrong! '. '        ------------------'; } if ($status = = Login::login_User_unknown) {//$this->sendmail (' User ip: '. $observable->ip. ' Login failed, no this user! '); echo __class__. ' User ip: '. $observable->ip. ' Login failed, no this user! '. '        ------------------'; }}}/** * Class phoneobserver * @author Jichao.wang */class Phoneobserver implements Observer {function update (Obs        Ervable $observable) {$status = $observable->getstatus ();            if ($status = = login::login_access) {//$this->sendmail (' User ip: '. $observable->ip. ' Login successful! '); echo __class__. ' User ip: '. $observable->ip. ' Login successful! '        ------------------'; if ($status = = Login::login_wrong_pass) {//$this->sendmail (' User ip: '. $observable->ip. ' Login failed, password wrong! ')            ; echo __class__. ' User ip: '. $observable->ip. ' Login failed, password wrong! '. '        ------------------'; if ($status = = Login::login_user_unknown) {//$this->sendmail (' User ip: '. $observable->ip. ' Login failed, no this user! ')            ); echo __class__. ' User ip: '. $observable->ip. ' Login failed, no this user! '. ' ------------------'; }}}class Abcobserver implements Observer {function update (Observable $observable) {$status = $observable-        >getstatus ();            if ($status = = login::login_access) {//$this->sendmail (' User ip: '. $observable->ip. ' Login successful! '); echo __class__. ' User ip: '. $observable->ip. ' Login successful! '        ------------------'; if ($status = = Login::login_wrong_pass) {//$this->sendmail (' User ip: '. $observable->ip. ' Login failed, password wrong! ')            ; echo __class__. ' User ip: '. $observable->ip. ' Login failed, password wrong! '. '        ------------------'; if ($status = = Login::login_user_unknown) {//$this->sendmail (' User ip: '. $observable->ip. ' Login failed, no this user! ')            ); echo __class__. ' User ip: '. $observable->ip. ' Login failed, no this user! '. '        ------------------'; The}}//instantiates the login information $login = new Login (),//implements the E-mail observer $login->attach (new Emailobserver ());//implements the verification Code observer $login->attach (new Phoneobserver ());//implement other observer $login->attach (new Abcobserver ());//Start Landing $login->handlelOgin ();? >

The Observer pattern for PHP design Patterns

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.