观察者模式

_observers[]=$observer;
    }

    /**
     * 发送通知
     */
    public function notify(){
        foreach ($this->_observers as $observer){
            $observer->update();
        }
    }
}

/**
 * 观察者接口类
 * Interface Observer
 */
interface Observer {
    public function update();
}

class Observer1 implements Observer{

    public function update() {
        echo 'Observer1 ... ';
    }
}

class Observer2 implements Observer{

    public function update() {
        echo 'Observer2 ... ';
    }
}

//$eg = new EventGenerator();
//$eg->add(new Observer1());
//$eg->add(new Observer2());
//$eg->notify();

/**
 * 事件通知
 * Class Event
 */
class Event extends EventGenerator{
    public  function trigger(){
        $this->notify();
    }
}

$e = new Event();
$e->add(new Observer1());
$e->add(new Observer2());
$e->trigger();

你可能感兴趣的:(观察者模式)