<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ![](https://source.unsplash.com/random/1680x200) ## 裝飾器模式(Decorator Pattern) **概念** 允許向一個已有的對象添加新的功能或部分內容,同時又不改變其結構。屬于結構型模式,它是作為現有的類的一個包裝。 **為什么需要裝飾器模式** 1. 我們要對一個已有的對象添加新功能,又不想修改它原來的結構。 2. 使用子類繼承的方法去實現添加新功能,會不可避免地出現子類過多,繼承鏈很長的情況。而且不少書籍都規勸我們竭力保持一個對象的父與子關系不超過3個。 3. 裝飾器模式,可以提供對對象內容快速非侵入式地修改。 **UML圖** ![](https://img.kancloud.cn/f7/fa/f7fa9a0f8b59b34592506ce49c9f2f0c_643x546.jpg) **簡單實例** 原本的對象和裝飾共同的接口:奶茶接口 ``` // 奶茶接口 interface TeaInterface { /** * @Desc: 名稱 * @return mixed */ public function name(); /** * @Desc: 價格 * @return mixed */ public function price(); } ``` 原本的對象:小麥奶茶 ``` /** * 小麥奶茶 * Class Wheat * @package app\common\repositories\decorator */ class Wheat extends TeaAbstract { public function name() { return '小麥奶茶'; } public function price() { return 12; } } ``` 原本的對象:蜂蜜柚子茶 ``` /** * 蜂蜜奶茶 * Class Honey * @package app\common\repositories\decorator */ class Honey extends TeaAbstract { public function name() { return '蜂蜜柚子茶'; } public function price() { return 10; } } ``` 裝飾抽象類:DecoratorAbstract ``` abstract class DecoratorAbstract implements TeaInterface { // 奶茶實例 public $tea; public function __construct(TeaInterface $tea) { $this->tea = $tea; } } ``` 具體裝飾類:加糖類 ``` class SugarDecorator extends DecoratorAbstract { public function name() { return $this->tea->name() . '加糖'; } public function price() { return $this->tea->price() + 1; } } ``` 具體裝飾類:加奶類 ``` class MilkDecorator extends DecoratorAbstract { public function name() { return $this->tea->name() . '加奶'; } public function price() { return $this->tea->price() + 5; } } ``` 工作人員制作奶茶 ``` // 蜂蜜柚子茶 $tem = new Honey(); echo $tem->name().' | '.$tem->price()."<br/>"; // 蜂蜜柚子茶 + 糖 $suger = new SugarDecorator($tem); echo $suger->name().' | '.$suger->price()."<br/>"; // 小麥奶茶 $tem = new Wheat(); echo $tem->name().' | '.$tem->price()."<br/>"; // 小麥奶茶 + 奶 $suger = new MilkDecorator($tem); echo $suger->name().' | '.$suger->price()."<br/>"; ``` 成品輸出 ``` 蜂蜜柚子茶 | 10 蜂蜜柚子茶加糖 | 11 小麥奶茶 | 12 小麥奶茶加奶 | 17 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看