Design Patterns--Viewer mode _php Tutorial

Source: Internet
Author: User
[PHP]
/*
Consider the following scenario:
1 times password error, reminder to re-login
2 times wrong, out of verification code
5 times error, verification code becomes complicated
10 times wrong, locked account

General ideas:

When the user name/password mismatch is determined {

if (wrong) {
Count +1
}

if (number ==1) {
} else if (number ==2) {
} else {
}....
....

This is obviously unreasonable.

}



Determine whether the user name/password is correct or not, this belongs to the login class
Landing success/failure, reward/punishment is a bonus 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); Returns 0 on behalf of username/password error; Return 1, Login successful

Notifications are listening to all my objects
$this->noticefy ();


return $this->state;
}
}


Class Log implements observer{
Public Function Update ($obj) {
The log analyzes
echo $obj->state? ' Add 1 points, record ': ' Wrong 1 times, record and analyze ';
Echo '
';
}
}

Class Biz implements observer{
Public Function Update ($obj) {
Echo (Time ()-$obj->lastlogin) > 1000? ' Long time no come ': ' Quality customer ';
}
}



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

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


It's ======client's end, =====//.

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

/*
Consider the following scenario:
1 times password error, reminder to re-login
2 times wrong, out of verification code
5 times error, verification code becomes complicated
10 times wrong, locked account

General ideas:

When the user name/password mismatch is determined {

if (wrong) {
Count +1
}

if (number ==1) {
} else if (number ==2) {
} else {
}....
....

This is obviously unreasonable.

}

Determine whether the user name/password is correct or not, this belongs to the login class
Landing success/failure, reward/punishment is a bonus 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); Returns 0 on behalf of username/password error; Return 1, Login successful

Notifications are listening to all my objects
$this->noticefy ();


return $this->state;
}
}


Class Log implements observer{
Public Function Update ($obj) {
The log analyzes
echo $obj->state? ' Add 1 points, record ': ' Wrong 1 times, record and analyze ';
Echo '
';
}
}

Class Biz implements observer{
Public Function Update ($obj) {
Echo (Time ()-$obj->lastlogin) > 1000? ' Long time no come ': ' Quality customer ';
}
}

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

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


It's ======client's end, =====//.

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

';
}


http://www.bkjia.com/PHPjc/477316.html www.bkjia.com true http://www.bkjia.com/PHPjc/477316.html techarticle [PHP]/* Consider the following scenario: 1 password errors, reminders to re-login 2 times the wrong, out of the verification code 5 times the wrong, the verification code becomes complex 10 times wrong, locked the account general idea: When the user name/password does not match ...

  • 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.