PHP Pattern Design

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.