{"id":312,"date":"2021-06-12T11:04:41","date_gmt":"2021-06-12T03:04:41","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=312"},"modified":"2025-10-09T13:56:05","modified_gmt":"2025-10-09T05:56:05","slug":"design-pattern-05-facade-pattern","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/06\/12\/design-pattern-05-facade-pattern\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f 05 \u5916\u89c2\u6a21\u5f0f"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h2>\u4e00\u3001\u610f\u56fe<\/h2>\n<p>&emsp;&emsp;\u4e3a\u5b50\u7cfb\u7edf\u4e2d\u7684\u4e00\u7ec4\u63a5\u53e3\u63d0\u4f9b\u4e00\u4e2a\u4e00\u81f4\u7684\u754c\u9762\u3002<br \/>\n&emsp;&emsp;\u4eba\u8bdd\uff1a\u5916\u90e8\u7c7b\u4e0d\u518d\u76f4\u63a5\u4e0e\u7cfb\u7edf\u4e2d\u7684\u5177\u4f53\u5b50\u7c7b\u4ea4\u4e92\uff0c\u800c\u662f\u7edf\u4e00\u4ea4\u7531\u4e00\u4e2a\u4e2d\u95f4\u4eba\u4ee3\u7406\u5b8c\u6210\u3002<\/p>\n<h2>\u4e8c\u3001UML<\/h2>\n<figure style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/05\/structure.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"560\" height=\"380\" alt=\"\u56fe1\" class=\"size-full\" \/ ><figcaption class=\"wp-caption-text\"><noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/05\/structure.png\" width=\"560\" height=\"380\" alt=\"\u56fe1\" class=\"size-full\" \/><\/noscript> \u56fe1\uff1a\u5916\u89c2\u6a21\u5f0fUML<\/figcaption><\/figure>\n<h2>\u4e09\u3001\u5b9e\u4f8b<\/h2>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;memory&gt;\nclass Subsystem1 {\npublic:\n    std::string Operation1() const {\n        return &quot;Subsystem1: Ready!\\n&quot;;\n    }\n    \/\/ ...\n    std::string OperationN() const {\n        return &quot;Subsystem1: Go!\\n&quot;;\n    }\n};\n\nclass Subsystem2 {\npublic:\n    std::string Operation1() const {\n        return &quot;Subsystem2: Get ready!\\n&quot;;\n    }\n    \/\/ ...\n    std::string OperationZ() const {\n        return &quot;Subsystem2: Fire!\\n&quot;;\n    }\n};\n\nclass Facade {\nprotected:\n    Subsystem1* subsystem1_;\n    Subsystem2* subsystem2_;\n\npublic:\n\n    Facade(\n        Subsystem1* subsystem1 = nullptr,\n        Subsystem2* subsystem2 = nullptr)\n    {\n        this-&gt;subsystem1_ = this-&gt;subsystem1_ == subsystem1 ? subsystem1 : new Subsystem1;\n        this-&gt;subsystem2_ = this-&gt;subsystem2_ == subsystem2 ? subsystem2 : new Subsystem2;\n    }\n    ~Facade() {\n        delete subsystem1_;\n        delete subsystem2_;\n    }\n\n    std::string Operation() {\n        std::string result = &quot;Facade initializes subsystems:\\n&quot;;\n        result += this-&gt;subsystem1_-&gt;Operation1();\n        result += this-&gt;subsystem2_-&gt;Operation1();\n        result += &quot;Facade orders subsystems to perform the action:\\n&quot;;\n        result += this-&gt;subsystem1_-&gt;OperationN();\n        result += this-&gt;subsystem2_-&gt;OperationZ();\n        return result;\n    }\n};\n\nvoid ClientCode(Facade* facade) {\n    \/\/ ...\n    std::cout &lt;&lt; facade-&gt;Operation();\n    \/\/ ...\n}\n\nint main() {\n    Subsystem1* subsystem1 = new Subsystem1;\n    Subsystem2* subsystem2 = new Subsystem2;\n    Facade* facade = new Facade(subsystem1, subsystem2);\n    ClientCode(facade);\n\n    delete facade;\n\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u610f\u56fe &emsp;&emsp;\u4e3a\u5b50\u7cfb\u7edf\u4e2d\u7684\u4e00\u7ec4\u63a5\u53e3\u63d0\u4f9b\u4e00\u4e2a\u4e00\u81f4\u7684\u754c\u9762\u3002 &emsp;&emsp;\u4eba\u8bdd\uff1a\u5916\u90e8\u7c7b\u4e0d\u518d\u76f4\u63a5\u4e0e\u7cfb\u7edf\u4e2d\u7684 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/312"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=312"}],"version-history":[{"count":2,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/312\/revisions"}],"predecessor-version":[{"id":460,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/312\/revisions\/460"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}