assessmentee.com

Home | Start test | Contact
 

Activerecord | Adapter | Decorator | DependencyInjectionContainer | Facade | Factory | Hydration | Inheritance | Iterator | Mapper | MVC | Observer | Prototype | Proxy | Registry | ServiceLocator | Singleton | Specification | Strategy | TableGateway | ZF1_TableGateway | ZF2_TableGateway |




    <?php
/*
 * Observer design pattern
 *
 * when this subject changes state, several observers will be notified.
 */

class Subject {
    protected $observers = array();

    public function attach($observer){
        if(!in_array($observer, $this->observers)){
            array_push($this->observers, $observer);
        }
    }
    public function notify($message){
        foreach($this->observers AS $observer){
            $observer->notify($message);
        }
    }
}
/*
 * one of many observers waiting for subject to change state
 */

class Observer {
    public function notify($message){
        echo "we ran out of ".$message;
    }
}
$customer = new Subject();
$shopOwner = new Observer();
$customer->attach($shopOwner);
$customer->notify('jam');
?>


    

Outputs:


we ran out of jam



 

© 2018 assessmentee.com