1, Policy mode
2, individual mode
3, Factory mode
4, Observer mode
1<?PHP2 classexchangerate3 {4 Static Private $instance=NULL;5 Private $observers=Array();6 Private $exchange _rate;7 8 Static functiongetinstance ()9 {Ten if(Self::$instance==NULL) OneSelf::$instance=Newexchangerate (); A returnSelf::$instance; - } - Public functionRegisterobserver ($obj) the { - $this->observers[] =$obj; - } - Public functiongetexchangerate () + { - return $this-exchange_rate; + } A Public functionSetexchangerate ($new _rate) at { - $this->exchange_rate =$new _rate; - $this-notifyobservers (); - - } - functionnotifyobservers () in { - foreach($this->observers as $obj) { to #code ... + $obj->notify ($this); - } the } * $ Panax Notoginseng } - the classProductitem + { A Public function__construct () the { +Exchangerate::getinstance ()->registerobserver ($this); - $ } $ Public functionNotify$obj) - { - if($objinstanceof exchangerate)Print"Received update!\n"; the } - }Wuyi the - Wu $a=NewProductitem (); - $b=NewProductitem (); AboutExchangerate::getinstance ()->setexchangerate (4.5); $ - - -?>View Code
PHP Pattern Design