国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP設計模式裝飾器模式實例

2024-05-04 21:49:57
字體:
來源:轉載
供稿:網友

php面向對象的設計模式中有很多種模式了,今天我們為各位介紹的是裝飾器模式的一個學習筆記了,有需要了解php裝飾器模式的朋友可以和小編來看看。

我們在使用面向對象的日常開發過程中,或許會碰見需要對某個方法或者某個對象,添加新的行為。然而常見的做法是,寫一個子類繼承需要改寫的類,然后去重新實現類的方法。

但是裝飾器模式(Decorator),可以動態地添加修改類的功能,在使用裝飾器模式,僅需在運行時添加一個裝飾器對象既可實現,相對與生成子類更加的靈活。

在我們需要改寫一個類的時候通常的做法是采用繼承的方式來重新方法,如下代碼:

  1. /* 
  2.  * 比如我們需要改寫一串字符串的樣式,采用繼承的寫法。 
  3.  */ 
  4. class Canvas { 
  5.     function draw($width = 20, $height = 10) { 
  6.         for($i = 0; $i < $height$i++) { 
  7.             for($j = 0; $j < $width$j++) { 
  8.                 echo '*'
  9.             } 
  10.             echo '<br/>'
  11.          } //Vevb.com 
  12.      } 
  13. class Canvas2 extends Canvas { 
  14.     function draw($width = 20, $height = 10) { 
  15.         echo "<div style='color: red;'>"
  16.         parent::draw($width$height); 
  17.         echo "</div>"
  18.     } 
  19. $Canvas2 = new Canvas2(); 
  20. $Canvas2->draw(); 

對于上面的這種寫法,假如我們需要多增加一個一種樣式就需要多一個繼承。接下來使用裝飾器模式(Decorator)就會方便很多。

  1. /* 
  2.  
  3.  * 首先聲明一個裝飾器的接口 
  4.  
  5.  */ 
  6.  
  7. interface DrawDecorator { 
  8.  
  9.     function beforeDraw(); 
  10.  
  11.     function afterDraw(); 
  12.  

接下來再分別添加兩個裝飾類,來繼承接口,實現接口中的方法:

  1. /* 
  2.  * 顏色裝飾 
  3.  */ 
  4. class ColorDrawDecorator implements DrawDecorator { 
  5.     protected $color
  6.     function __construct($color = 'red') { 
  7.         $this->color = $color
  8.     } 
  9.     function beforeDraw() { 
  10.         echo "<div style='color: {$this->color};'>"
  11.     } 
  12.     function afterDraw() { 
  13.         echo "</div>"
  14.     } 
  15. /* 
  16.  * 字體大小裝飾 
  17.  */ 
  18. class SizeDrawDecorator implements DrawDecorator { 
  19.     protected $size
  20.     function __construct($size = '14px') { 
  21.         $this->size = $size
  22.     } 
  23.     function beforeDraw() { 
  24.         echo "<div style='font-size: {$this->size};'>"
  25.     } 
  26.     function afterDraw() { 
  27.         echo "</div>"
  28.     } 

接下來就是使用我們前面所創建的裝飾類:

  1. /* 
  2.  * 創建一個畫布類 
  3.  */ 
  4. class Canvas { 
  5.     protected $decorators = array(); //用來存放裝飾的數組 
  6.     function draw($width = 20, $height = 10) { 
  7.         $this->beforeDraw(); 
  8.         for($i = 0; $i < $height$i++) { 
  9.             for($j = 0; $j < $width$j++) 
  10.             { 
  11.                 echo '*'
  12.             } 
  13.             echo '<br/>'
  14.         } 
  15.         $this->afterDraw(); 
  16.     } 
  17.     //添加裝飾器的方法 
  18.     function addDecorator(DrawDecorator $decorator) { 
  19.         $this->decorators[] = $decorator
  20.     } 
  21.     function beforeDraw() { 
  22.         foreach($this->decorators as $decorator) { 
  23.             $decorator->beforeDraw(); 
  24.         } 
  25.     } 
  26.     function afterDraw() { 
  27.         $decorators = array_reverse($this->decorators); 
  28.         foreach($decorators as $decorator) { 
  29.             $decorator->afterDraw(); 
  30.         } 
  31.     } 
  32. $Canvas = new Canvas(); 
  33. $Canvas->addDecorator(new ColorDrawDecorator('red')); 
  34. $Canvas->addDecorator(new SizeDrawDecorator('9px')); 
  35. $Canvas->draw(20, 10);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临泽县| 卫辉市| 五指山市| 汉阴县| 彰武县| 辛集市| 柘荣县| 广汉市| 仙居县| 汝城县| 元氏县| 尖扎县| 长宁区| 东城区| 全州县| 土默特右旗| 泰兴市| 蒲江县| 广灵县| 长岛县| 金坛市| 柘城县| 伊金霍洛旗| 水城县| 蚌埠市| 唐河县| 辰溪县| 香格里拉县| 广德县| 胶南市| 永嘉县| 同心县| 固阳县| 集安市| 碌曲县| 长乐市| 青冈县| 海安县| 海安县| 道真| 泉州市|