<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                > **設計模式-策略模式** ## 說明 ## **適配器模式(Adapter Pattern):將某個對象的接口適配為另一個對象所期望的接口。屬于結構型設計模式。** ---------- ##模式說明## 【適配器模式中主要角色】 目標(Target)角色:定義客戶端使用的與特定領域相關的接口,這也就是我們所期待得到的 源(Adaptee)角色:需要進行適配的接口 適配器(Adapter)角色:對Adaptee的接口與Target接口進行適配;適配器是本模式的核心,適配器把源接口轉換成目標接口,此角色為具體類。 說明示例:就類似于生活中你家墻上有一個兩口的插座(Adaptee),但你買了一個電風扇(Target)需要三個口的,這個時候你就需要一個插排(Adapter)。 ##類適配器## /** * 適配器模式(Adapter Pattern):將某個對象的接口適配為另一個對象所期望的接口。屬于結構型設計模式。 */ //例子:生活中你家墻上有一個兩口的插座(Adaptee),但你買了一個電風扇(Target)需要三個口的,這個時候你就需要一個插排(Adapter)。 //目標(Target)角色:定義客戶端使用的與特定領域相關的接口, //源(Adaptee)角色:需要進行適配的接口 墻上兩口插座 //適配器(Adapter)角色:對Adaptee的接口與Target接口進行適配;適配器是本模式的核心,適配器把源接口轉換成目標接口,此角色為具體類。 連接兩口插座,提供三口充電 /** * Class Adaptee * 源角色 (墻上插座,提供兩口連電) */ class Adaptee { /** * 兩口充電 */ public function twoCharge() { echo '兩口連上有電'; } } /** * Interface Target * 目標(Target)角色 * 風扇想要的接口 */ interface Target { /** * 兩口連電 連接插座 */ public function twoCharge(); /** * 三口連電 適配風扇 */ public function threeCharge(); } /** * 適配器(Adapter)角色 (插排) * 連接墻上插座,提供三口充電 */ class Adapter extends Adaptee implements Target { public function threeCharge() { echo '支持三口連接'; } } class Client { public static function main() { $adapter = new Adapter(); $adapter->twoCharge(); $adapter->threeCharge(); } } $fan= new Client(); $fan->main(); ##對象適配器模式## <?php /** * 適配器模式(Adapter Pattern):將某個對象的接口適配為另一個對象所期望的接口。屬于結構型設計模式。 */ //例子:生活中你家墻上有一個兩口的插座(Adaptee),但你買了一個電風扇(Target)需要三個口的,這個時候你就需要一個插排(Adapter)。 //目標(Target)角色:定義客戶端使用的與特定領域相關的接口, //源(Adaptee)角色:需要進行適配的接口 墻上兩口插座 //適配器(Adapter)角色:對Adaptee的接口與Target接口進行適配;適配器是本模式的核心,適配器把源接口轉換成目標接口,此角色為具體類。 連接兩口插座,提供三口充電 /** * Class Adaptee * 源角色 (墻上插座,提供兩口連電) */ class Adaptee { /** * 兩口充電 */ public function twoCharge() { echo '兩口連上有電'; } } /** * Interface Target * 目標(Target)角色 * 風扇想要的接口 */ interface Target { /** * 兩口連電 連接插座 */ public function twoCharge(); /** * 三口連電 適配風扇 */ public function threeCharge(); } /** * 適配器(Adapter)角色 (插排) * 連接墻上插座,提供三口充電 */ class Adapter implements Target { private $_adaptee; public function __construct(Adaptee $adaptee) { $this->_adaptee = $adaptee; } /** * 委派調用Adaptee的twoCharge方法 */ public function twoCharge() { $this->_adaptee->twoCharge(); } public function threeCharge() { echo '支持三口連接'; } } class Client { /** * Main program. */ public static function main() { $adaptee = new Adaptee(); $adapter = new Adapter($adaptee); $adapter->twoCharge(); $adapter->threeCharge(); } } $fan= new Client(); $fan->main(); ##總結說明##  類適配器采用“多繼承”的實現方式,帶來了不良的高耦合,所以一般不推薦使用。對象適配器采用“對象組合”的方式,更符合松耦合精神。 ## 結尾 ## <p style="background-image: -webkit-linear-gradient(left, #3498db, #f47920 10%, #d71345 20%, #f7acbc 30%,#ffd400 40%, #3498db 50%, #f47920 60%, #d71345 70%, #f7acbc 80%, #ffd400 90%, #3498db);color: transparent;-webkit-text-fill-color: transparent;-webkit-background-clip: text;text-align:center;"> 心如花木,向陽而生。 </p> [1]: https://blog.zxliu.cn/usr/uploads/2020/11/2092914566.png
                  <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>

                              哎呀哎呀视频在线观看