<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國際加速解決方案。 廣告
                (一)為什么需要適配器模式 1,某個操作數據庫的有兩套不同的數據庫操作方法,我們通過適配器統一成一個接口。例如,我們待會把mysql和mysqli統一成一個接口。 2,我們有多套數據庫對應了多種數據庫操作,例如MySQL,SqlServer,Oralce,Redis都有對應的操作函數,或操作類。PDO把這些都統一成一個接口。 3,系統的增加一些新功能,創建了一個新的接口,但是老的接口并不想廢棄。可以使用適配器模式,對用戶隱藏這兩個接口,提供用戶所希望的接口。 ``` <?php //MySQL待操作適配類 class MySQLAdaptee implements Target { protected $conn; //用于存放數據庫連接句柄 //實現連接方法 public function connect($host, $user, $passwd, $dbname) { $conn = mysql_connect($host, $user, $passwd); mysql_select_db($dbname, $conn); $this->conn = $conn; } //查詢方法 public function query($sql) { $res = mysql_query($sql, $this->conn); return $res; } //關閉方法 public function close() { mysql_close($this->conn); } } //MySQLi操作待適配類 class MySQLiAdaptee { protected $conn; public function connect($host, $user, $passwd, $dbname) { $conn = mysqli_connect($host, $user, $passwd, $dbname); $this->conn = $conn; } public function query($sql) { return mysqli_query($this->conn, $sql); } public function close() { mysqli_close($this->conn); } } //用戶所期待的接口 Interface Target{ public function connect($host, $user, $passwd, $dbname); public function query($sql); public function close(); } //用戶期待適配類 Class DataBase implements Target { protected $db ; //存放MySQLiAdapter對象或MySQLAdapter對象 public function __construct($type){ $type = $type."Adapter" ; $this->db = new $type ; } public function connect($host, $user, $passwd, $dbname){ $this->db->connect($host, $user, $passwd, $dbname); } public function query($sql){ return $this->db->query($sql); } public function close(){ $this->db->close(); } } //用戶調用同一個接口,使用MySQL和mysqli這兩套不同示例。 $db1 = new DataBase('MySQL'); $db1->connect('127.0.0.1','root','1234','myDB');die; $db1->query('select * from test'); $db1->close(); $db2 = new DataBase('MySQLi'); $db2->connect('127.0.0.1','root','1234','myDB'); $db2->query('select * from test'); $db2->close(); ``` 場景描述 比如現在有一個支付工具Paypal,通過調用$Paypal->sendPayment($amount)就可以進行支付,但是過了一段時間,這個調用方法名稱改變了,或者是改用其它支付工具,顯然將每個調用到這個方法的代碼都改動一次是不太現實的,這個時候就可以考慮使用適配器模式進行封裝; ``` <?php class Paypal { public function sendPayMent($amount) { echo "pay by Paypal:".$amount."<br>"; } } class Skrill { public function doPayment($amount) { echo "pay bySkrill:".$amount."<br>"; } } class Adapter { private $adapter; public function __construct($adaptee) { $this->adapter = $adaptee; } public function send($amount) { if ($this->adapter == 'Paypal') { $send = new Paypal(); $send->sendPayMent($amount); } if ($this->adapter == 'Skrill') { $send = new Skrill(); $send->doPayment($amount); } } } $paypal = new Adapter('Paypal'); $paypal->send(50); ```
                  <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>

                              哎呀哎呀视频在线观看