Examples of Observer Pattern Definition and Usage of PHP Design Pattern

  • 2021-10-25 06:13:20
  • OfStack

This paper illustrates the definition and usage of observer pattern of PHP design pattern. Share it for your reference, as follows:


<?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
   *  Execute login 
   */
  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(' Users ip:'.$observable->ip.' Successful login !');
      echo __CLASS__.' Users ip:'.$observable->ip.' Successful login !'.'------------------';
    }
    if($status == Login::LOGIN_WRONG_PASS){
//      $this->sendMail(' Users ip:'.$observable->ip.' Login failed, password error !');
      echo __CLASS__.' Users ip:'.$observable->ip.' Login failed, password error !'.'------------------';
    }
    if($status == Login::LOGIN_USER_UNKNOWN){
//      $this->sendMail(' Users ip:'.$observable->ip.' Login failed, there is no such user !');
      echo __CLASS__.' Users ip:'.$observable->ip.' Login failed, there is no such user !'.'------------------';
    }
  }
}
/**
 * Class PhoneObserver
 * @author jichao.wang
 */
class PhoneObserver implements Observer {
  function update (Observable $observable) {
    $status = $observable->getStatus();
    if($status == Login::LOGIN_ACCESS){
//      $this->sendMail(' Users ip:'.$observable->ip.' Successful login !');
      echo __CLASS__.' Users ip:'.$observable->ip.' Successful login !'.'------------------';
    }
    if($status == Login::LOGIN_WRONG_PASS){
//      $this->sendMail(' Users ip:'.$observable->ip.' Login failed, password error !');
      echo __CLASS__.' Users ip:'.$observable->ip.' Login failed, password error !'.'------------------';
    }
    if($status == Login::LOGIN_USER_UNKNOWN){
//      $this->sendMail(' Users ip:'.$observable->ip.' Login failed, there is no such user !');
      echo __CLASS__.' Users ip:'.$observable->ip.' Login failed, there is no such user !'.'------------------';
    }
  }
}
class AbcObserver implements Observer {
  function update (Observable $observable) {
    $status = $observable->getStatus();
    if($status == Login::LOGIN_ACCESS){
//      $this->sendMail(' Users ip:'.$observable->ip.' Successful login !');
      echo __CLASS__.' Users ip:'.$observable->ip.' Successful login !'.'------------------';
    }
    if($status == Login::LOGIN_WRONG_PASS){
//      $this->sendMail(' Users ip:'.$observable->ip.' Login failed, password error !');
      echo __CLASS__.' Users ip:'.$observable->ip.' Login failed, password error !'.'------------------';
    }
    if($status == Login::LOGIN_USER_UNKNOWN){
//      $this->sendMail(' Users ip:'.$observable->ip.' Login failed, there is no such user !');
      echo __CLASS__.' Users ip:'.$observable->ip.' Login failed, there is no such user !'.'------------------';
    }
  }
}
// Instantiate login information 
$login = new Login();
// Implement email observer 
$login->attach(new EmailObserver());
// Realize the observer of sending verification code 
$login->attach(new PhoneObserver());
// Implement other observers 
$login->attach(new AbcObserver());
// Start landing 
$login->handleLogin();
?>

Run results:

EmailObserver user ip: 41 login failed, no such user! ------------PhoneObserver user ip: 41 login failed, no such user! -----------AbcObserver User ip: 41 Log-in failed, no such user! -----

More readers interested in PHP can check out the topics of this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: