zoukankan      html  css  js  c++  java
  • php模式设计

    1,策略模式

    2,个体模式

    3,工厂模式

    4,观察者模式

     1 <?php
     2 class ExchangeRate
     3 {
     4     static private $instance = NULL;
     5     private $observers = array();
     6     private $exchange_rate;
     7 
     8     static function getInstance()
     9     {
    10         if (self::$instance == NULL)
    11             self::$instance = new ExchangeRate();
    12         return self::$instance;
    13     }
    14     public function registerObserver($obj)
    15     {
    16         $this->observers[] = $obj;
    17     }
    18     public function getExchangeRate()
    19     {
    20         return $this->exchange_rate;
    21     }
    22     public function setExchangeRate($new_rate)
    23     {
    24         $this->exchange_rate = $new_rate;
    25         $this->notifyObservers();
    26 
    27     }
    28     function notifyObservers()
    29     {
    30         foreach ($this->observers as $obj) {
    31             # code...
    32             $obj->notify($this);
    33         }
    34     }
    35 
    36 
    37 }
    38 
    39 class ProductItem
    40 {
    41     public function __construct()
    42     {
    43         ExchangeRate::getInstance()->registerObserver($this);
    44 
    45     }
    46     public function notify($obj)
    47     {
    48         if($obj instanceof ExchangeRate) print "Received update!
    ";
    49     }
    50 }
    51 
    52 
    53 
    54 $a = new ProductItem();
    55 $b = new ProductItem();
    56 ExchangeRate::getInstance()->setExchangeRate(4.5);
    57 
    58 
    59 
    60 ?>
    View Code
  • 相关阅读:
    Validate US Telephone Numbers
    7月份总结
    Arguments Optional
    Everything Be True
    手机开发网页模板(20140124)
    整站开发初始化
    switch滑动开关
    js 面向对象
    Bootstrap 导航栏
    Bootstrap 标签页
  • 原文地址:https://www.cnblogs.com/liunnis/p/4709437.html
Copyright © 2011-2022 走看看