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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ## 示例 適配器模式在 PHP 代碼中很常見。基于一些遺留代碼的系統常常會使用該模式。在這種情況下,適配器讓遺留代碼與現代的類得以相互合作 ### 概念示例 <details> <summary>main.php</summary> ``` <?php class Target { public function request(): string { return "Target: The default target's behavior."; } } // 適配行為 class Adaptee { public function specificRequest(): string { return ".eetpadA eht fo roivaheb laicepS"; } } // 添加適配器,來適配 target class Adapter extends Target { private $adaptee; public function __construct(Adaptee $adaptee) { $this->adaptee = $adaptee; } public function request(): string { return strrev($this->adaptee->specificRequest()); } } function clientCode(Target $target) { echo $target->request(); } $adaptee = new Adaptee(); $adapter = new Adapter($adaptee); clientCode($adapter); ``` </details> <br /> 輸出 ``` Special behavior of the Adaptee. ``` ### 改造郵箱發送接口 適配器允許你使用第三方或遺留系統的類, 即使它們與你的代碼不兼容 你可以創建一系列特殊的封裝器,來讓應用所發出的調用與第三方類所要求的接口與格式適配,而無需重寫應用的通知接口以使其支持每一個第三方服務(如釘釘、微信、短信或其他任何服務) <details> <summary>main.php</summary> ``` <?php interface Notification { public function send(string $title, string $message); } // 假設原先為發郵箱的接口 class EmailNotification implements Notification { private $adminEmail; public function __construct(string $adminEmail) { $this->adminEmail = $adminEmail; } public function send(string $title, string $message): void { mail($this->adminEmail, $title, $message); echo "send email :'$title' to '{$this->adminEmail}' that says '$message'."; } } class SlackApi { private $login; private $apiKey; public function __construct(string $login, string $apiKey) { $this->login = $login; $this->apiKey = $apiKey; } public function logIn(): void { echo "logIn :'{$this->login}'.\n"; } public function sendMessage(string $chatId, string $message): void { echo "sendMessage :'$chatId' chat: '$message'.\n"; } } class SlackNotification implements Notification { private $slack; private $chatId; public function __construct(SlackApi $slack, string $chatId) { $this->slack = $slack; $this->chatId = $chatId; } // 對發郵箱接口進行適配 public function send(string $title, string $message): void { // strip_tags 可以去掉原先郵箱發送時候攜帶的 html標簽 $slackMessage = "#" . $title . "# " . strip_tags($message); $this->slack->logIn(); $this->slack->sendMessage($this->chatId, $slackMessage); } } // 假設為老代碼的調用通知接口 function clientCode(Notification $notification) { // ... echo $notification->send("發送 title","<p>發送內容</p>"); // ... } $notification = new EmailNotification("developers@example.com"); clientCode($notification); echo "\n\n"; $slackApi = new SlackApi("example.com", "XXXXXXXX"); $notification = new SlackNotification($slackApi, "Example.com Developers"); clientCode($notification); ``` </details> <br /> 輸出 ``` Sent email with title '發送 title' to 'developers@example.com' that says '<p>發送內容</p>'. logIn :'example.com'. sendMessage :'Example.com Developers' chat: '#發送 title# 發送內容'. ```
                  <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>

                              哎呀哎呀视频在线观看