Home > php教程 > php手册 > body text

设计模式--观察者模式

WBOY
Release: 2016-06-13 10:57:45
Original
1044 people have browsed it

 

 

 


[php]
/*
考虑如下场景:
1次密码错,提醒重登陆
2次错,出验证码
5次错,验证码变复杂
10次错,锁死账号
 
常规思路:
 
当判断用户名/密码不匹配后 {
 
    if(错) {
        次数+1
    }
 
    if(次数==1) {
    } else if(次数==2){
    } else {
    }....
    ....
 
    这显然不合理
 
}
 
 
 
判断用户名/密码 正确与否  这属于登陆类
登陆成功/失败,进行奖励/惩罚 属于奖惩类.
 
*/ 
 
 
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); // 返回0 代表用户名/密码错; 返回1,登陆成功  
         
        // 通知正在监听我的所有对象  
        $this->noticefy(); 
 
         
        return $this->state; 
    } 

 
 
class Log implements Observer{ 
    public function update($obj) { 
        // 日志分析了  
        echo $obj->state?'加1分,记录':'错1次,记录并分析'; 
        echo '
'; 
    } 

 
class Biz implements Observer{ 
    public function update($obj) { 
        echo (time() - $obj->lastlogin) > 1000?'好久没来了':'优质客户'; 
    } 

 
 
 
$user = new User(); 
$log = new log(); 
$biz = new Biz(); 
 
$user->attach('log',$log); 
$user->attach('biz',$biz); 
 
 
//======client端的事了=====//  
 
for($i=1;$i     $user->login(); 
    echo '


'; 

/*
考虑如下场景:
1次密码错,提醒重登陆
2次错,出验证码
5次错,验证码变复杂
10次错,锁死账号

常规思路:

当判断用户名/密码不匹配后 {

    if(错) {
        次数+1
    }

    if(次数==1) {
    } else if(次数==2){
    } else {
    }....
    ....

    这显然不合理

}

 

判断用户名/密码 正确与否  这属于登陆类
登陆成功/失败,进行奖励/惩罚 属于奖惩类.

*/


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); // 返回0 代表用户名/密码错; 返回1,登陆成功
       
        // 通知正在监听我的所有对象
        $this->noticefy();

       
        return $this->state;
    }
}


class Log implements Observer{
    public function update($obj) {
        // 日志分析了
        echo $obj->state?'加1分,记录':'错1次,记录并分析';
        echo '
';
    }
}

class Biz implements Observer{
    public function update($obj) {
        echo (time() - $obj->lastlogin) > 1000?'好久没来了':'优质客户';
    }
}

 

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

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


//======client端的事了=====//

for($i=1;$i     $user->login();
    echo '


';
}

 

 


 

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!