The core of the observer mode is to separate the customer element (observer) from a central class (entity. When the subject knows that the time has occurred, the observer needs to be notified. At the same time, hard encoding is not required between the subject and the observer.
The intention of the Observer Pattern in design patterns is described as follows: "defining one-to-many dependencies between objects. When the State of an object changes, all objects dependent on it can be notified and automatically updated"
The following object-oriented principles are also worth noting:
1. Object self-responsibility: there are multiple observer objects, but all the information required by the subject object mobile phone is complete by yourself.
2. abstract class (Interface): The Observer class (Interface) represents the concept of "objects to be notified", which provides a public interface for the target to notify the observer.
3. multi-state encapsulation: subject does not know to communicate with that observer. In fact, the observer class encapsulates a variety of specific observers. This assumes that if I have a new observer in the future, I do not need to change the subject class.
The following is an example of the PHP object-oriented mode and practice:
Assume that we have a login class to process user login information. When a user logs on, the corresponding action is performed based on the user's login status. If the login object and the corresponding action are not hard-coded. The observer mode is an excellent choice.
Observable interface (subject ):
interface Observable{ function attach(Observer $observer); function detach(Observer $observer); function notify();}
Specific login (logon subject ):
class login implements Observable{ const LOGIN_ACCESS = 1; const LOGIN_WRONG_PASS = 2; const LOGIN_USER_UNKNOWN = 3; private $status = array(); private $observers; public function __construct(){ $this->observers = array(); } public function attach(Observer $observer){ $this->observers[] = $observer; } public function detach(Observer $observer){ $newObservers = array(); foreach($this->observers as $obs){ if($obs != $observer){ $newObservers[] = $obs; } } $this->observers = $newObservers; } private function setStatus($status,$user,$ip){ $this->status = array($status,$user,$ip); } public function getStatus(){ return $this->status; } public function notify(){ foreach($this->observers as $obs){ $obs->update($this); } } public function handlerLogin( $user,$pass,$ip){ switch(rand(1,3)){ case 1: $this->setStatus(Self::LOGIN_ACCESS,$user,$ip); $ret = true;break; case 2: $this->setStatus(Self::LOGIN_WRONG_PASS,$user,$ip); $ret = false;break; case 3: $this->setStatus(Self::LOGIN_USER_UNKNOWN,$user,$ip); $ret = true;break; } }}
Observer interface:
interface Observer{ function update(Observable $observable);}
Abstract login observer class:
abstract class LoginObserver implements Observer{ private $login; public function __construct(Login $login){ $this->login = $login; $login->attach($this); } public function update(Observable $observable){ if($observable == $this->login){ $this->doUpdate($observable); } } abstract private function doUpdate( Login $login );}
Specific observer class:
// Observer 1: Class securitymonitor extends loginobserver {private function doupdate (LOGIN $ login) {$ status = $ login-> getstatus (); if ($ status [0] = login: login_wrong_pass) {print _ class __. "\ tsending mail to SysAdmin" ;}}// observer 2: record the logon log class generallogger extends loginobserver {private function doupdate (LOGIN $ login) {$ status = $ login-> getstatus (); // log print _ class __. "[Status]: {$ status} \ Tadd login data to log" ;}}// observer 3: Set cookieclass partnershiptool extends loginobserver {private function doupdate (LOGIN $ login) {$ status = $ login-> getstatus (); // cookie print _ class __. "[cookie]: {$ status} \ t set cookie to a list ";}}
Shows the graph relationships between classes:
The built-in SPL extension of PHP provides original support for the observer mode. The observer consists of three elements: splobserver, splsubject, and splstorage. splobserver and splsubject are interfaces. Splstorage is a tool class used to better store and delete objects.
With the support of SPL, it is easier to implement the observer mode.