<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] ## 示例 當你希望在無需修改客戶代碼的前提下于已有類的對象上增加額外行為時,該模式是無可替代的 ## 添加日志行為 <details> <summary>main.php</summary> ``` <?php namespace RefactoringGuru\Proxy\Conceptual; // 服務接口 interface Subject{ public function request(): void; } // 服務 class RealSubject implements Subject{ public function request(): void{ echo "RealSubject: Handling request.\n"; } } // 代理 class Proxy implements Subject{ /** * @var RealSubject */ private $realSubject; public function __construct(RealSubject $realSubject){ $this->realSubject = $realSubject; } public function request(): void{ if($this->checkAccess()){ $this->realSubject->request(); $this->logAccess(); } } private function checkAccess(): bool{ // Some real checks should go here. echo "Proxy: Checking access prior to firing a real request.\n"; return true; } private function logAccess(): void{ echo "Proxy: Logging the time of request.\n"; } } function clientCode(Subject $subject){ // ... $subject->request(); // ... } $realSubject = new RealSubject(); $proxy = new Proxy($realSubject); clientCode($proxy); ``` </details> <br /> 輸出 ``` Proxy: Checking access prior to firing a real request. RealSubject: Handling request. Proxy: Logging the time of request. ``` ### 緩存接口 **代理**類型有無數種:緩存、日志、訪問控制和延遲初始化等 本例演示了代理模式如何通過對結果進行緩存來提高下載器對象的性能 <details> <summary>main.php</summary> ``` <?php namespace RefactoringGuru\Proxy\RealWorld; interface Downloader { public function download(string $url): string; } class SimpleDownloader implements Downloader { public function download(string $url): string { echo "Downloading a file from the Internet.\n"; $result = file_get_contents($url); echo "Downloaded bytes: " . strlen($result) . "\n"; return $result; } } class CachingDownloader implements Downloader { /** * @var SimpleDownloader */ private $downloader; /** * @var string[] */ private $cache = []; public function __construct(SimpleDownloader $downloader) { $this->downloader = $downloader; } public function download(string $url): string { if (!isset($this->cache[$url])) { echo "CacheProxy MISS. "; $result = $this->downloader->download($url); $this->cache[$url] = $result; } else { echo "CacheProxy HIT. Retrieving result from cache.\n"; } return $this->cache[$url]; } } function clientCode(Downloader $subject) { $result = $subject->download("http://example.com/"); // 重復下載請求可緩存的速度增加。 $result = $subject->download("http://example.com/"); } $realSubject = new SimpleDownloader(); $proxy = new CachingDownloader($realSubject); clientCode($proxy); ``` </details> <br /> 輸出 ``` CacheProxy MISS. Downloading a file from the Internet. Downloaded bytes: 1256 CacheProxy HIT. Retrieving result from cache. ```
                  <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>

                              哎呀哎呀视频在线观看