<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) ## 工廠方法模式(Factory Method) **概述:** 定義一個用于創建對象的接口,讓子類決定實例化哪一個類。 **簡述:** 工廠模式是另一種非常常用的模式,正如其名字所示:確實是對象實例的生產工廠。某些意義上,工廠模式提供了通用的方法有助于我們去獲取對象,而不需要關心其具體的內在的實現。 **適用性:** 1. 當一個類不知道它所必須創建的對象的類的時候。 2. 當一個類希望由它的子類來指定它所創建的對象的時候。 3. 當類將創建對象的職責委托給多個幫助子類中的某一個,并且你希望將哪一個幫助子類是代理者這一信息局部化的時候。 **UML圖** ![](https://img.kancloud.cn/24/fe/24feb8707eeb8874dcec972d631923ce_598x401.jpg) **簡單實例** ``` // 工廠接口 interface FactoryInterface { /** * @Desc: 創建操作系統 * @param string $type 系統類型 * @return mixed */ public function createSystem($type); } // 工廠 class Factory implements FactoryInterface { // 實現工廠方法 public function createSystem($type) { switch ($type) { case 'mac': return new MacSystem(); case 'windows': return new WindowSystem(); case 'linux': return new LinuxSystem(); } } } // 操作系統接口 interface SystemInterface { /** * @Desc: 開機 * @return mixed */ public function boot(); /** * @Desc: 關機 * @return mixed */ public function shutDown(); } // Windows操作系統 class WindowSystem implements SystemInterface { public function boot() { echo 'Windows 10 開機'; } public function shutDown() { echo 'Windows 10 關機'; } } // Linux操作系統 class LinuxSystem implements SystemInterface { public function boot() { echo 'Linux 開機'; } public function shutDown() { echo 'Linux 關機'; } } ``` **使用** ``` // 創建工廠實例 $factory = new Factory(); // 通過工廠實例創建windows系統 $windows = $factory->createSystem('windows'); // Windows 開機 echo $windows->boot()."<br/>"; // Linux 開機 $linux = $factory->createSystem('linux'); echo $linux->boot()."<br/>"; echo $linux->shutDown(); ``` > 輸出內容: Windows 10 開機 Linux 開機 Linux 關機
                  <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>

                              哎呀哎呀视频在线观看