{"id":202430,"date":"2025-05-13T12:00:49","date_gmt":"2025-05-13T04:00:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202430\/"},"modified":"2025-05-13T12:00:49","modified_gmt":"2025-05-13T04:00:49","slug":"php-%e5%87%bd%e6%95%b0%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e5%ba%94%e7%94%a8%e4%b8%8e%e5%8f%af%e6%89%a9%e5%b1%95%e6%80%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202430\/","title":{"rendered":"PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e0e\u53ef\u6269\u5c55\u6027"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e0e\u53ef\u6269\u5c55\u6027<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aPHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e0e\u53ef\u6269\u5c55\u6027\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241015\/1728980458670e25ea25927.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u7684\u5e94\u7528\u4e0e\u53ef\u6269\u5c55\u6027<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><br \/>\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u662f\u7f16\u5199\u6a21\u5757\u5316\u3001\u53ef\u91cd\u7528\u548c\u53ef\u6269\u5c55\u4ee3\u7801\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u5728 PHP \u4e2d\u5e94\u7528\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u6269\u5c55\u6027\u3002<\/p>\n<p><strong>\u7ecf\u5178\u8bbe\u8ba1\u6a21\u5f0f<\/strong><\/p>\n<p><strong>1. \u5355\u4f8b\u6a21\u5f0f<\/strong><br \/>\u9650\u5236\u7c7b\u5b9e\u4f8b\u5316\u7684\u6570\u91cf\uff0c\u786e\u4fdd\u53ea\u80fd\u521b\u5efa\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/p>\n<pre>class Singleton\n{\n    private static $instance = null;\n    private function __construct() {}\n    public static function getInstance()\n    {\n        if (self::$instance === null) {\n            self::$instance = new Singleton();\n        }\n        return self::$instance;\n    }\n}<\/pre>\n<p><strong>2. \u5de5\u5382\u65b9\u6cd5\u6a21\u5f0f<\/strong><br \/>\u901a\u8fc7\u5b50\u7c7b\u5316\u521b\u5efa\u5bf9\u8c61\u7684\u63a5\u53e3\uff0c\u5141\u8bb8\u5728\u4e0d\u66f4\u6539\u5ba2\u6237\u7aef\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<\/p>\n<pre>interface Factory\n{\n    public function createProduct();\n}\n\nclass ConcreteFactory1 implements Factory\n{\n    public function createProduct()\n    {\n        return new Product1();\n    }\n}<\/pre>\n<p><strong>3. \u7b56\u7565\u6a21\u5f0f<\/strong><br \/>\u5b9a\u4e49\u4e00\u7cfb\u5217\u7b97\u6cd5\uff0c\u5c01\u88c5\u5b83\u4eec\u5e76\u4f7f\u5b83\u4eec\u53ef\u4e92\u6362\u3002\u5ba2\u6237\u7aef\u53ef\u4ee5\u72ec\u7acb\u4e8e\u5177\u4f53\u7b97\u6cd5\u8fdb\u884c\u914d\u7f6e\u3002<\/p>\n<pre>interface Strategy\n{\n    public function calculate(int $a, int $b);\n}\n\nclass SumStrategy implements Strategy\n{\n    public function calculate(int $a, int $b)\n    {\n        return $a + $b;\n    }\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p><strong>\u5efa\u7acb\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u7c7b<\/strong><br \/>\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u786e\u4fdd\u53ea\u6709\u5355\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u5668\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u65b9\u6cd5\u5c06\u4fe1\u606f\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<pre>class Logger\n{\n    private static $instance = null;\n    private function __construct() {}\n    private function log($message)\n    {\n        \/\/ \u5199\u5165\u65e5\u5fd7\u6587\u4ef6...\n    }\n    public static function getInstance()\n    {\n        if (self::$instance === null) {\n            self::$instance = new Logger();\n        }\n        return self::$instance;\n    }<\/pre>\n<p><strong>\u521b\u5efa\u53ef\u6269\u5c55\u7684\u51fd\u6570\u5e93<\/strong><br \/>\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u5b9a\u4e49\u7b97\u6cd5\u65cf\uff0c\u4ee5\u4fbf\u6839\u636e\u9700\u8981\u8f7b\u677e\u6dfb\u52a0\u65b0\u7684\u7b97\u6cd5\u3002<\/p>\n<pre>class Math\n{\n    private $strategy;\n    public function setStrategy(Strategy $strategy)\n    {\n        $this-&gt;strategy = $strategy;\n    }\n    public function calculate(int $a, int $b)\n    {\n        return $this-&gt;strategy-&gt;calculate($a, $b);\n    }\n}<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><br \/>\u5e94\u7528\u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8 PHP \u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u901a\u8fc7\u5229\u7528\u8fd9\u4e9b\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7f16\u5199\u6a21\u5757\u5316\u3001\u53ef\u91cd\u7528\u3001\u6613\u4e8e\u6269\u5c55\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aPHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e0e\u53ef\u6269\u5c55\u6027\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u8bbe\u8ba1\u6a21\u5f0f\u5e94\u7528\u4e0e\u53ef\u6269\u5c55&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202430","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202430"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202430\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}