[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
When it is determined that the username/password does not match {
if (wrong) {
Number of times +1
}
} else if(number of times==2){
} else {
}....
....
}
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 '