zoukankan      html  css  js  c++  java
  • php设计模式(3)-观察者模式

    <?php

    class User implements SplSubject{
    public $loginNum;
    public $hobby;
    public $observers = null;

    public function __construct($hobby){
    $this->loginNum = rand(1,10);
    $this->hobby = $hobby;
    $this->observers = new SplObjectStorage();
    }
    public function attach(SplObserver $observer){
    $this->observers->attach($observer);
    }
    public function detach(SplObserver $observer){
    $this->observers->detach($observer);
    }
    public function notify(){
    $this->observers->rewind();
    while($this->observers->valid()){
    $observer = $this->observers->current();
    $observer->update($this);
    $this->observers->next();
    }
    }
    public function login(){
    $this->notify();
    }

    }

    class Secrity implements SplObserver{
    public function update(SplSubject $subject){
    echo "第".$subject->loginNum."次登陆";
    }
    }

    class Ad implements SplObserver{
    public function update(SplSubject $subject){
    echo "爱好".$subject->hobby;
    }
    }

    $user = new User("篮球");
    $user->attach(new Secrity());
    $user->attach(new Ad());
    $user->login();

    下面通过观察者模式实现登陆

    index.php

    <html>
    <head></head>
    <body>
    <form action="main.php" method="post">
    姓名:<input type="text" name="name"/><br/>
    密码:<input type="password" name="password"/>
    <input type="submit" value="登陆"/>
    </form>

    </body>
    </html>

    main.php

    <?php

    class User implements SplSubject{
    public $name;
    public $password;
    public $observers = null;

    public function __construct($name,$password){
    $this->name = $name;
    $this->password = $password;
    $this->observers = new SplObjectStorage();
    }
    public function attach(SplObserver $observer){
    $this->observers->attach($observer);
    }
    public function detach(SplObserver $observer){
    $this->observers->detach($observer);
    }
    public function notify(){
    $this->observers->rewind();
    while($this->observers->valid()){
    $observer = $this->observers->current();
    $observer->update($this);
    $this->observers->next();
    }
    }
    public function login(){
    $this->notify();
    }

    }

    class Validate implements SplObserver{
    public function update(SplSubject $subject){
    if($subject->name=="shenming" && $subject->password=="shenm"){
    echo "success";
    }else{
    echo "false";
    }
    }
    }


    $name=$_POST['name'];
    $password=$_POST['password'];
    $user = new User($name,$password);
    $user->attach(new Validate());
    $user->login();

  • 相关阅读:
    Process 'command '/Users/lidaqiang/Library/Android/sdk/build-tools/27.0.3/aapt'' finished with non-zero exit value 1
    合同诈骗无罪裁判要旨、判决理由及评析意见
    互换不同种类毒品行为如何认定
    正当防卫指导性案例以及研析
    期待可能性理论的司法适用
    共同犯罪认定方法
    刑事案件鉴定意见常用质证要点
    张明楷的100个刑法案例
    刑事律师办案必备126部法律
    正当防卫裁判要旨16条
  • 原文地址:https://www.cnblogs.com/shenming/p/4217002.html
Copyright © 2011-2022 走看看