欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP设计模式之观察者模式定义与用法示例

程序员文章站 2022-06-24 23:13:38
本文实例讲述了php设计模式之观察者模式定义与用法。分享给大家供大家参考,具体如下:

本文实例讲述了php设计模式之观察者模式定义与用法。分享给大家供大家参考,具体如下:

<?php
/**
 * interface observable
 * define a observable interface
 * @author jichao.wang
 */
interface observable
{
  function attach(observer $observer);
  function detach(observer $observer);
  function notify();
}
/**
 * class login
 * @author jichao.wang
 */
class login implements observable
{
  private $observers;
  public $status;
  public $ip;
  const login_access = 1;
  const login_wrong_pass = 2;
  const login_user_unknown = 3;
  function __construct()
  {
    $this->observers = array();
  }
  /**
   * @param observer $observer
   * @author jichao.wang
   * attach a observer
   */
  function attach(observer $observer)
  {
    $this->observers[] = $observer;
  }
  /**
   * @param observer $observer
   * @author jichao.wang
   * detach a observer
   */
  function detach(observer $observer)
  {
    $newobservers = array();
    foreach ($this->observers as $key => $obs) {
      if ($obs !== $observer) {
        $newobservers[] = $obs;
      }
    }
    $this->observers = $newobservers;
  }
  /**
   * @author jichao.wang
   * handle observer notify
   */
  function notify()
  {
    foreach ($this->observers as $obs) {
      $obs->update($this);
    }
  }
  /**
   * @author jichao.wang
   * 执行登陆
   */
  function handlelogin()
  {
    $ip = rand(1,100);
    switch (rand(1, 3)) {
      case 1:
        $this->setstatus(self::login_access, $ip);
        $ret = true;
        break;
      case 2:
        $this->setstatus(self::login_wrong_pass, $ip);
        $ret = false;
        break;
      case 3:
        $this->setstatus(self::login_user_unknown, $ip);
        $ret = false;
        break;
    }
    /**
     * handle event
     */
    $this->notify();
    return $ret;
  }
  /**
   * @param $status
   * @author jichao.wang
   * set login status
   */
  function setstatus($status,$ip)
  {
    $this->status = $status;
    $this->ip = $ip;
  }
  /**
   * @return mixed
   * @author jichao.wang
   * get login status
   */
  function getstatus()
  {
    return $this->status;
  }
}
/**
 * interface observer
 * @author jichao.wang
 */
interface observer {
  function update(observable $observable);
}
/**
 * class emailobserver
 * @author jichao.wang
 */
class emailobserver implements observer {
  function update (observable $observable) {
    $status = $observable->getstatus();
    if($status == login::login_access){
//      $this->sendmail('用户ip:'.$observable->ip.'登陆成功!');
      echo __class__.'用户ip:'.$observable->ip.'登陆成功!'.'------------------';
    }
    if($status == login::login_wrong_pass){
//      $this->sendmail('用户ip:'.$observable->ip.'登陆失败,密码错误!');
      echo __class__.'用户ip:'.$observable->ip.'登陆失败,密码错误!'.'------------------';
    }
    if($status == login::login_user_unknown){
//      $this->sendmail('用户ip:'.$observable->ip.'登陆失败,无此用户!');
      echo __class__.'用户ip:'.$observable->ip.'登陆失败,无此用户!'.'------------------';
    }
  }
}
/**
 * class phoneobserver
 * @author jichao.wang
 */
class phoneobserver implements observer {
  function update (observable $observable) {
    $status = $observable->getstatus();
    if($status == login::login_access){
//      $this->sendmail('用户ip:'.$observable->ip.'登陆成功!');
      echo __class__.'用户ip:'.$observable->ip.'登陆成功!'.'------------------';
    }
    if($status == login::login_wrong_pass){
//      $this->sendmail('用户ip:'.$observable->ip.'登陆失败,密码错误!');
      echo __class__.'用户ip:'.$observable->ip.'登陆失败,密码错误!'.'------------------';
    }
    if($status == login::login_user_unknown){
//      $this->sendmail('用户ip:'.$observable->ip.'登陆失败,无此用户!');
      echo __class__.'用户ip:'.$observable->ip.'登陆失败,无此用户!'.'------------------';
    }
  }
}
class abcobserver implements observer {
  function update (observable $observable) {
    $status = $observable->getstatus();
    if($status == login::login_access){
//      $this->sendmail('用户ip:'.$observable->ip.'登陆成功!');
      echo __class__.'用户ip:'.$observable->ip.'登陆成功!'.'------------------';
    }
    if($status == login::login_wrong_pass){
//      $this->sendmail('用户ip:'.$observable->ip.'登陆失败,密码错误!');
      echo __class__.'用户ip:'.$observable->ip.'登陆失败,密码错误!'.'------------------';
    }
    if($status == login::login_user_unknown){
//      $this->sendmail('用户ip:'.$observable->ip.'登陆失败,无此用户!');
      echo __class__.'用户ip:'.$observable->ip.'登陆失败,无此用户!'.'------------------';
    }
  }
}
//实例化登陆信息
$login = new login();
//实现发邮件观察者
$login->attach(new emailobserver());
//实现发验证码观察者
$login->attach(new phoneobserver());
//实现其他观察者
$login->attach(new abcobserver());
//开始登陆
$login->handlelogin();
?>

运行结果:

emailobserver用户ip:41登陆失败,无此用户!------------------phoneobserver用户ip:41登陆失败,无此用户!------------------abcobserver用户ip:41登陆失败,无此用户!------------------

更多关于php相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php数组(array)操作技巧大全》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。