Home  >  Article  >  Backend Development  >  Design Pattern--Observer Pattern_PHP Tutorial

Design Pattern--Observer Pattern_PHP Tutorial

WBOY
WBOYOriginal
2016-07-14 10:11:28897browse


[php]
/*
Consider the following scenario:
Wrong password once, reminder to log in again
2 errors, verification code appears
If you make 5 mistakes, the verification code becomes complicated
10 mistakes, the account will be locked

Conventional ideas:

When it is determined that the username/password does not match {

If(wrong) {
Number of times +1
}

If (number of times == 1) {
} else if(number of times==2){
} else {
}....
....

This is obviously unreasonable

}



Determine whether the username/password is correct or not. This belongs to the login category
Login success/failure, reward/punishment, belongs to the reward and punishment category.

*/


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(0,1); // Returning 0 means the username/password is wrong; Returning 1 means the login is successful
                                   
                    // Notify all objects that are listening for me                                                                               $this->noticefy();

                                   
Return $this->state;
}  
}


class Log implements Observer{
Public function update($obj) {
// Log analysis
              echo $obj->state?'Add 1 point, record': 'Wrong 1 time, record and analyze';
echo '
';
}  
}

class Biz implements Observer{
Public function update($obj) {
echo (time() - $obj->lastlogin) > 1000?'Long time no see':'Quality customer';
}  
}



$user = new User();
$log = new log();
$biz = new Biz();

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


//======It’s about the client======//

for($i=1;$i<10;$i++) {
$user->login();
echo '


';
}

/*

Consider the following scenario:
Wrong password once, reminder to log in again
2 errors, verification code appears
If you make 5 mistakes, the verification code becomes complicated
10 mistakes, the account will be locked

General ideas:

When it is determined that the username/password does not match {

if (wrong) {

Number of times +1
}

if (number of times == 1) {

} else if(number of times==2){
} else {
}....
....

This is obviously unreasonable

}

Determine whether the username/password is correct or not. This belongs to the login category
Login success/failure, reward/punishment, belongs to the reward and punishment category.

*/


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(0,1); // Returning 0 means the username/password is wrong; Returning 1 means the login is successful
                         
                   // Notify all objects that are listening for me
$this->noticefy();

 
          return $this->state;
}
}


class Log implements Observer{
Public function update($obj) {
// Log analysis
              echo $obj->state?'Add 1 point, record': 'Wrong 1 time, record and analyze';
echo '
';
}
}

class Biz implements Observer{
Public function update($obj) {
echo (time() - $obj->lastlogin) > 1000?'Long time no see':'Quality customer';
}
}

$user = new User();
$log = new log();
$biz = new Biz();

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


//======It’s about the client=====//

for($i=1;$i<10;$i++) {
$user->login();
echo '


';
}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477316.htmlTechArticle[php] /* Consider the following scenario: 1 wrong password, reminder to log in again 2 wrong times, verification code If you make 5 mistakes, the verification code will become complicated. If you make 10 mistakes, the account will be locked. Conventional thinking: When it is determined that the username/password does not match...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn