The observer pattern, as I understand it, is divided into two parts: The Observer class and the Observer class. The Observer class inherits an interface (for example: Observable) to implement additions, deletions, and notifications to the observer (that is, notifies the observer that a particular event has occurred), which aggregates the Observer class to look at the original book Class diagram. While the Observer class inherits an interface (such as: Observer) to implement an abstract method update (), this method in the following code example also receives a parameter that is observed by the observer, deciding whether to execute its own business logic, depending on the state of the Observer. Well, gossip doesn't say much about the code directly.
By the Observer class
Interface Observable {
function Attach (Observer $observer); Add Observer
function Detach (Observer $observer); Delete Viewer
function notify (); Notify the Viewer
}
A class for handling login information
Class Login implements observable{
Const LOGIN_USER_UNKNOWN = 1;
Const LOGIN_WRONG_PASS = 2;
Const LOGIN_ACCESS = 3;
Private $status = Array (); The State of the Observer
Private $observers; Save Observer object
function __construct () {
$this->observers = Array ();
}
function Attach (Observer $observer) {
$this->observers[] = $observer;
}
function Detach (Observer $observer) {
$newobservers = Array ();
foreach (| $this->observers as $obs) {
if ($obs!== $observer) {
$newobservers []= $obs;
}
}
$this->observers = $newobservers;
}
function Notify () {
foreach ($this->observers as $obs) {
$obs->update ($this);
}
}
function Handlelogin ($user, $pass, $ip) {
Switch (rand (1,3)) {//Here simulates 3 login statuses
Case 1:
$this->setstatus (self::login_access, $user, $IP); Sets the state of the Observer, notifies the Observer, and the observer then executes the corresponding business logic according to the status of the observed person
$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 =false;
Break
}
$this->notify ():
return $ret;
//...............
}
Private Function SetStatus ($status, $user, $ip) {
$this->status = Array ($status, $user, $ip)
}
}
Viewer class
Interface observer{
function Update (Observable $observable);
}
This class is primarily to ensure that the object being passed in is the login class, because only login has the SetStatus method, and the above observer does not add abstract methods to ensure its versatility SetStatus
Abstract class Loginobserver implements observer{
Private $login;
function __construct (Login $login) {
$this->login= $login;
$login->attach ($this);
}
function Update (Observable $observable) {
if ($observable = = = $this->login) {
$this->doupdate ($observable);
}
}
Abstract function DoUpdate (Login $login);
}
Class Securitymonitor extends loginobserver{//login When the password is wrong, send an email to the manager
function DoUpdate (Login $login) {
$status = $login->getstatus ();
if ($status [0] = = Login::login_wrong_pass) {
Send mail
}
}
}
Class Generallogger extends loginobserver{//....
function DoUpdate (Login $login) {
$status = $login->getstatus ();
Log log in Data
}
}
Class Partnershiptool extends loginobserver{//...
function DoUpdate (Login $login) {
$status = $login->getstatus ();
Check IP, set cookies
}
}
Finally: The road long its repair far XI, I will go up and down and quest!
PHP Object-oriented observer pattern