執行及描述任務-------觀察者模式

uml圖php

代碼實現cookie

<?php
interface Observable{
    function attach(Observer $observer);
    function detach(observer $observer);
    function notify();
}


//主體
class Login implements Observable
{
    protected $observers;
    private $status = array();
    const LOGIN_USER_UNKNOW = 1;
    const LOGIN_WRONG_PASS = 2;
    const LOGIN_ACCESS = 3;

    function handleLogin($user,$pass,$ip)
    {
        switch (rand(1,3)) {
            case 1:
                $this->setStatus(self::LOGIN_ACCESS,$user,$ip);
                $ret = true;
                break;
            case 2:
                $this->setStatus(self::LOGIN_WRONG_PASS,$user,$ip);
                $ret = false;
                break;
            case 3:
                $this->setStatus(self::LOGIN_USER_UNKNOW,$user,$ip);
                $ret = false;
                break;
        }
        $this->notify();
        return $ret;
    }

    private function setStatus($status,$user,$ip){
        return $this->status = array($status,$user,$ip);
    }

    function getStatus(){
        return $this->status;
    }

    function __construct()
    {
        $this->observers = array();
    }

    function attach(Observer $observer)
    {
        $this->observers[] = $observer;
    }

    function detach(Observer $observer)
    {
        $newobservers = array();
        foreach ($this->observers as $obs){
            if($obs!==$observer){
                $newobservers[] = $obs;
            }
        }
        $this->observers = $newobservers;
    }

    function notify(){
        foreach ($this->observers as $obs) {
            $obs->update($this);
        }
    }

}


interface Observer
{
    function update(Observable $observable);
}


//LoginObserver類負責保證繼承該類的觀察者使用的主體是Log類型,並將本身添加到主體上
abstract class LoginObserver implements Observer
{
    private $login;

    function __construct(Login $login){
        $this->login = $login;
        $login->attach($this);
    }

    function update(Observable $observable)
    {
        if($observable === $this->login){
            $this->doUpdate($observable);
        }
    }

    protected abstract function doUpdate(Login $login);
}


class SecurityMonitor extends LoginObserver
{
    function doUpdate(Login $login)
    {
        $status = $login->getStatus();
        //發送郵件給系統管理員
        echo __CLASS__.":\tsending mail to sysadmin<br>";
        
    }
}

class GeneralLogger extends LoginObserver
{
    function doUpdate(Login $login)
    {
        $status = $login->getStatus();
        //記錄登陸數據到日誌
        echo __CLASS__.":\tadd login data to log<br>";
        
    }
}

class PartnershipTool extends LoginObserver
{
    function doUpdate(Login $login)
    {
        $status = $login->getStatus();
        //檢查IP地址
        //若是匹配列表則設置cookie
        echo __CLASS__.":\tset cookie if Ip matchers a list<br>";
        
    }
}



//client
//$log = new Login();
//$login->attach(new SecurityMonitor());

$log = new Login();
new SecurityMonitor($log);//加入觀察者
new GeneralLogger($log);//加入觀察者
new PartnershipTool($log);//加入觀察者
$log->notify();
?>
相關文章
相關標籤/搜索