Design Pattern-Observer pattern

Source: Internet
Author: User
[Php]/* Consider the following scenarios: one wrong password, two wrong login attempts, five wrong verification codes, and 10 wrong verification codes. the general idea of locking the account is as follows: {if (error) {Times + 1} if (Times = 1) {} elseif (Times = 2) {} else... "/> <scripttype =" text/plain CR

 

 

 


[Php]
/*
Consider the following scenarios:
The password is incorrect once, prompting you to log on again
Two errors. the verification code is displayed.
Five errors make the verification code more complex
10 errors, locked account
 
General Idea:
 
{
 
If (error ){
Times + 1
}
 
If (Times = 1 ){
} Else if (Times = 2 ){
} Else {
}....
....
 
This is obviously unreasonable.
 
}
 
 
 
Determining whether the user name/password is correct is a login type
Logon success/failure, rewards/punishments are rewards and punishments.
 
*/
 
 
Interface Observer {
Function update ($ obj );
}
 
 
Interface Post {
Function attach ($ key, $ obj );
Function detach ($ key );
 
Function noticefy ();
}
 
Class User implements Post {
Public $ state = null;
Public $ lastlogin = 0;
 
Protected $ observers = array ();
 
Public function attach ($ key, $ obj ){
$ This-> observers [$ key] = $ obj;
}
 
Public function detach ($ key ){
Unset ($ this-> observers [$ key]);
}
 
Public function noticefy (){
Foreach ($ this-> observers as $ obj ){
$ Obj-> update ($ this );
}
}
 
Public function Login (){
$ This-> state = rand (); // if 0 is returned, the user name or password is incorrect. if 1 is returned, the logon is successful.

// The notification is listening to all my objects.
$ This-> noticefy ();
 

Return $ this-> state;
}
}
 
 
Class Log implements Observer {
Public function update ($ obj ){
// Log analysis
Echo $ obj-> state? 'Add 1 cent, record ': 'error 1 time, record and analytics ';
Echo'
';
}
}
 
Class Biz implements Observer {
Public function update ($ obj ){
Echo (time ()-$ obj-> lastlogin)> 1000? 'Long time has not come ': 'Excellent customer ';
}
}
 
 
 
$ User = new User ();
$ Log = new log ();
$ Biz = new Biz ();
 
$ User-> attach ('log', $ log );
$ User-> attach ('biz', $ biz );
 
 
// ======= Client-side events ====== //
 
For ($ I = 1; $ I <10; $ I ++ ){
$ User-> login ();
Echo'

';
}

/*
Consider the following scenarios:
The password is incorrect once, prompting you to log on again
Two errors. the verification code is displayed.
Five errors make the verification code more complex
10 errors, locked account

General Idea:

{

If (error ){
Times + 1
}

If (Times = 1 ){
} Else if (Times = 2 ){
} Else {
}....
....

This is obviously unreasonable.

}

 

Determining whether the user name/password is correct is a login type
Logon success/failure, rewards/punishments are rewards and punishments.

*/


Interface Observer {
Function update ($ obj );
}


Interface Post {
Function attach ($ key, $ obj );
Function detach ($ key );

Function noticefy ();
}

Class User implements Post {
Public $ state = null;
Public $ lastlogin = 0;

Protected $ observers = array ();

Public function attach ($ key, $ obj ){
$ This-> observers [$ key] = $ obj;
}

Public function detach ($ key ){
Unset ($ this-> observers [$ key]);
}

Public function noticefy (){
Foreach ($ this-> observers as $ obj ){
$ Obj-> update ($ this );
}
}

Public function Login (){
$ This-> state = rand (); // if 0 is returned, the user name or password is incorrect. if 1 is returned, the logon is successful.

// The notification is listening to all my objects.
$ This-> noticefy ();


Return $ this-> state;
}
}


Class Log implements Observer {
Public function update ($ obj ){
// Log analysis
Echo $ obj-> state? 'Add 1 cent, record ': 'error 1 time, record and analytics ';
Echo'
';
}
}

Class Biz implements Observer {
Public function update ($ obj ){
Echo (time ()-$ obj-> lastlogin)> 1000? 'Long time has not come ': 'Excellent customer ';
}
}

 

$ User = new User ();
$ Log = new log ();
$ Biz = new Biz ();

$ User-> attach ('log', $ log );
$ User-> attach ('biz', $ biz );


// ======= Client-side events ====== //

For ($ I = 1; $ I <10; $ I ++ ){
$ User-> login ();
Echo'

';
}

 

 


 

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.