PHP object-oriented observer mode, php object-oriented observer

Source: Internet
Author: User

PHP object-oriented observer mode, php object-oriented observer

In my understanding, the observer mode is mainly divided into the observer class and the observer class. The observer class inherits an interface (such as Observable) to add, delete, and notify the observer (that is, to notify the observer of a specific event ), it aggregates the observer class to view the original book class diagram. The Observer class inherits an interface (such as Observer) to implement an abstract method update (). In the following code example, this method also receives a parameter that is called as the Observer, determines whether to execute its own business logic based on the status of the observer. Now, let's talk about the code.

// Observer class
Interface Observable {
Function attach (Observer $ observer); // Add an Observer
Function detach (Observer $ observer); // deletes the Observer
Function Y (); // notifies the observer
}

// A class for processing login information
Class Login implements Observable {
Const LOGIN_USER_UNKNOWN = 1;
Const LOGIN_WRONG_PASS = 2;
Const LOGIN_ACCESS = 3;
Private $ status = array (); // observer status

Private $ observers; // Save the 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 () {// three login statuses are simulated here
Case 1:
$ This-> setStatus (self: LOGIN_ACCESS, $ user, $ ip); // sets the status of the observer. After the observer is notified, the observer will follow the data status to execute the corresponding business logic
$ 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-> Policy ():
Return $ ret;
//...............
}

Private function setStatus ($ status, $ user, $ ip ){
$ This-> status = array ($ status, $ user, $ ip)
}
}

// Observer class
Interface Observer {
Function update (Observable $ observable );
}


// This class is mainly used to ensure that the passed object to be observed is a login class, because only login has the setStatus method. The above Observer does not add the abstract method setStatus to ensure its universality.
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 {// If the logon password is incorrect, an email is sent to the Administrator.
Function doUpdate (Login $ login ){
$ Status = $ login-> getStatus ();
If ($ status [0] = Login: LOGIN_WRONG_PASS ){
// Send an email
}
}
}

Class GeneralLogger extends LoginObserver {//..........
Function doUpdate (Login $ login ){
$ Status = $ login-> getStatus ();
// Record login data
}
}

Class partnershipTool extends LoginObserver {//.........
Function doUpdate (Login $ login ){
$ Status = $ login-> getStatus ();
// Check the iP address and set the Cookie
}
}

// Client code
$ Login = new Login ();
New SecurityMonitor ($ login );
New GeneralLogger ($ login );
New PartnershipTool ($ login );

$ Login-> handleLogin ('test', 'test', 'test ');

Finally: The road is long, and I will try again and again!

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.