• 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
  • 相关阅读:
    严格模式
    es6模块与 commonJS规范的区别
    Javascript内置对象、原生对象、宿主对象关系
    实现继承的几种方式
    创建对象的一些方式
    null的小扩展
    getElementById的缩略
    你真的知道为什么不推荐使用@import?
    换行与不换行
    transition与animation
  • 原文地址:https://www.cnblogs.com/liunnis/p/4709437.html
Copyright © 2020-2023  润新知