您好,登錄后才能下訂單哦!
怎么在PHP中定義一個觀察者模式?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
具體如下:
<?php interface Observable { function attach(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(Observer $observer) { $this->observers[] = $observer; } /** * @param Observer $observer * @author jichao.wang * detach a observer */ function detach(Observer $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->observers as $obs) { $obs->update($this); } } /** * @author jichao.wang * 執行登陸 */ 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() { return $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('用戶ip:'.$observable->ip.'登陸成功!'); echo __CLASS__.'用戶ip:'.$observable->ip.'登陸成功!'.'------------------'; } if($status == Login::LOGIN_WRONG_PASS){ // $this->sendMail('用戶ip:'.$observable->ip.'登陸失敗,密碼錯誤!'); echo __CLASS__.'用戶ip:'.$observable->ip.'登陸失敗,密碼錯誤!'.'------------------'; } if($status == Login::LOGIN_USER_UNKNOWN){ // $this->sendMail('用戶ip:'.$observable->ip.'登陸失敗,無此用戶!'); echo __CLASS__.'用戶ip:'.$observable->ip.'登陸失敗,無此用戶!'.'------------------'; } } } /** * Class PhoneObserver * @author jichao.wang */ class PhoneObserver implements Observer { function update (Observable $observable) { $status = $observable->getStatus(); if($status == Login::LOGIN_ACCESS){ // $this->sendMail('用戶ip:'.$observable->ip.'登陸成功!'); echo __CLASS__.'用戶ip:'.$observable->ip.'登陸成功!'.'------------------'; } if($status == Login::LOGIN_WRONG_PASS){ // $this->sendMail('用戶ip:'.$observable->ip.'登陸失敗,密碼錯誤!'); echo __CLASS__.'用戶ip:'.$observable->ip.'登陸失敗,密碼錯誤!'.'------------------'; } if($status == Login::LOGIN_USER_UNKNOWN){ // $this->sendMail('用戶ip:'.$observable->ip.'登陸失敗,無此用戶!'); echo __CLASS__.'用戶ip:'.$observable->ip.'登陸失敗,無此用戶!'.'------------------'; } } } class AbcObserver implements Observer { function update (Observable $observable) { $status = $observable->getStatus(); if($status == Login::LOGIN_ACCESS){ // $this->sendMail('用戶ip:'.$observable->ip.'登陸成功!'); echo __CLASS__.'用戶ip:'.$observable->ip.'登陸成功!'.'------------------'; } if($status == Login::LOGIN_WRONG_PASS){ // $this->sendMail('用戶ip:'.$observable->ip.'登陸失敗,密碼錯誤!'); echo __CLASS__.'用戶ip:'.$observable->ip.'登陸失敗,密碼錯誤!'.'------------------'; } if($status == Login::LOGIN_USER_UNKNOWN){ // $this->sendMail('用戶ip:'.$observable->ip.'登陸失敗,無此用戶!'); echo __CLASS__.'用戶ip:'.$observable->ip.'登陸失敗,無此用戶!'.'------------------'; } } } //實例化登陸信息 $login = new Login(); //實現發郵件觀察者 $login->attach(new EmailObserver()); //實現發驗證碼觀察者 $login->attach(new PhoneObserver()); //實現其他觀察者 $login->attach(new AbcObserver()); //開始登陸 $login->handleLogin(); ?>
運行結果:
EmailObserver用戶ip:41登陸失敗,無此用戶!------------------PhoneObserver用戶ip:41登陸失敗,無此用戶!------------------AbcObserver用戶ip:41登陸失敗,無此用戶!------------------
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。