{"id":316,"date":"2021-06-12T11:20:40","date_gmt":"2021-06-12T03:20:40","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=316"},"modified":"2025-10-09T13:56:04","modified_gmt":"2025-10-09T05:56:04","slug":"design-pattern-06-builder","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/06\/12\/design-pattern-06-builder\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f 06 \u751f\u6210\u5668\u6a21\u5f0f"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h2>\u4e00\u3001\u610f\u56fe<\/h2>\n<p>&emsp;&emsp;\u5c06\u4e00\u4e2a\u590d\u6742\u5bf9\u8c61\u7684\u6784\u5efa\u4e0e\u5b83\u7684\u8868\u793a\u5206\u79bb\uff0c\u4f7f\u5f97\u540c\u6837\u7684\u6784\u5efa\u8fc7\u7a0b\u53ef\u4ee5\u521b\u5efa\u4e0d\u540c\u7684\u8868\u793a\u3002<br \/>\n&emsp;&emsp;\u4eba\u8bdd\uff1a\u5de5\u5382\u6d41\u6c34\u7ebf\u751f\u6210\u4ea7\u54c1\u3002<br \/>\n<figure style=\"width: 640px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/builder-zh.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"640\" height=\"400\" alt=\"\u56fe1\" class=\"size-full\" \/><figcaption class=\"wp-caption-text\">\u56fe1\uff1abuilder\u793a\u610f\u56fe<\/figcaption><\/figure><\/p >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/builder-zh.png\" width=\"640\" height=\"400\" alt=\"\u56fe1\" class=\"size-full\" \/> \u56fe1\uff1abuilder\u793a\u610f\u56fe[\/caption]<\/p><\/noscript>\n<h2>\u4e8c\u3001\u95ee\u9898<\/h2>\n<p>&emsp;&emsp;\u4e0b\u9762\u6211\u4eec\u6765\u8003\u8651\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u623f\u5c4b\u5bf9\u8c61\u3002\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u62bd\u8c61\u51fa\u623f\u5c4b\u7684\u57fa\u672c\u7279\u5f81\uff0c\u7136\u540e\u6309\u7528\u6237\u4e0d\u540c\u7684\u9700\u6c42\u6dfb\u52a0\u4e0d\u540c\u7684\u5c5e\u6027\uff0c\u4ece\u800c\u4ea7\u751f\u4e0d\u540c\u7684\u5b50\u7c7b\u3002<br \/>\n<figure style=\"width: 600px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/problem1.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"600\" height=\"350\" alt=\"\u56fe2\" class=\"size-full\" \/><figcaption class=\"wp-caption-text\">\u56fe2\uff1a\u4f7f\u7528\u7ee7\u627f\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u623f\u5c4b<\/figcaption><\/figure><br \/ >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/problem1.png\" width=\"600\" height=\"350\" alt=\"\u56fe2\" class=\"size-full\" \/> \u56fe2\uff1a\u4f7f\u7528\u7ee7\u627f\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u623f\u5c4b[\/caption]<br \/><\/noscript>\n&emsp;&emsp;\u4f46\u662f\u8fd9\u4e48\u505a\u5c06\u4f1a\u4ea7\u751f\u76f8\u5f53\u591a\u7684\u5b50\u7c7b\u3002\u53e6\u4e00\u4e2a\u89e3\u51b3\u65b9\u6cd5\u662f\u5728\u57fa\u7c7b\u4e2d\u5199\u4e00\u4e2a\u5de8\u5927\u7684\u6784\u9020\u51fd\u6570\uff0c\u5728\u521b\u5efa\u65f6\u6307\u5b9a\u9700\u8981\u54ea\u4e9b\u5143\u7d20\u3002<br \/>\n<figure style=\"width: 600px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/problem2.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"600\" height=\"350\" alt=\"\u56fe3\" class=\"size-full\" \/><figcaption class=\"wp-caption-text\">\u56fe3\uff1a\u4f7f\u7528\u6784\u9020\u51fd\u6570\u53c2\u6570\u5316\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u623f\u5c4b<\/figcaption><\/figure><br \/ >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/problem2.png\" width=\"600\" height=\"350\" alt=\"\u56fe3\" class=\"size-full\" \/> \u56fe3\uff1a\u4f7f\u7528\u6784\u9020\u51fd\u6570\u53c2\u6570\u5316\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u623f\u5c4b[\/caption]<br \/><\/noscript>\n&emsp;&emsp;\u4f46\u662f\u8fd9\u4e00\u505a\u6cd5\u4f7f\u5f97\u6211\u4eec\u7684\u6784\u9020\u51fd\u6570\u5f88\u4e0d\u7b80\u6d01\uff0c\u540c\u65f6\u6709\u7684\u53c2\u6570\u5f88\u5c11\u4f1a\u88ab\u7528\u5230\u3002\u4f8b\u5982\uff0c\u53ea\u6709\u5f88\u5c11\u7684\u623f\u5c4b\u624d\u4f1a\u6709\u6e38\u6cf3\u6c60\u3002<\/p>\n<h2>\u4e09\u3001\u89e3\u51b3\u65b9\u6848<\/h2>\n<h3>3.1 \u751f\u6210\u5668\u6a21\u5f0f<\/h3>\n<p>&emsp;&emsp;\u751f\u6210\u5668\u6a21\u5f0f\u5efa\u8bae\u5c06\u6784\u9020\u5bf9\u8c61\u7684\u4ee3\u7801\u4ece\u4ea7\u54c1\u4e2d\u62bd\u79bb\uff0c\u5e76\u5355\u72ec\u653e\u5728\u4e00\u4e2a\u540d\u4e3a\u751f\u6210\u5668\u7684\u5bf9\u8c61\u5f53\u4e2d\u3002\u8be5\u6a21\u5f0f\u5c06\u6784\u9020\u5bf9\u8c61\u8fc7\u7a0b\u5212\u5206\u4e3a\u4e00\u7ec4\u6b65\u9aa4\uff0c\u5982 buildWalls\u521b\u5efa\u5899\u58c1 \u548c buildDoor\u521b\u5efa\u623f\u95e8 \u7b49\u3002\u6bcf\u6b21\u521b\u5efa\u5bf9\u8c61\u65f6\uff0c\u90fd\u9700\u8981\u901a\u8fc7\u751f\u6210\u5668\u6267\u884c\u4e00\u4e9b\u5217\u6b65\u9aa4\u3002\u91cd\u70b9\u5728\u4e8e\u65e0\u9700\u8c03\u7528\u5168\u90e8\u7684\u6b65\u9aa4\uff0c\u800c\u53ea\u9700\u8c03\u7528\u521b\u5efa\u7279\u5b9a\u5bf9\u8c61\u914d\u7f6e\u6240\u9700\u7684\u90a3\u4e9b\u6b65\u9aa4\u5373\u53ef\u3002<br \/>\n<figure style=\"width: 410px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/solution1.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"410\" height=\"280\" alt=\"\u56fe4\" class=\"size-full\" \/><figcaption class=\"wp-caption-text\">\u56fe4\uff1a\u751f\u6210\u5668\u5206\u79bb\u521b\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u6b65\u9aa4<\/figcaption><\/figure><br \/ >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/solution1.png\" width=\"410\" height=\"280\" alt=\"\u56fe4\" class=\"size-full\" \/> \u56fe4\uff1a\u751f\u6210\u5668\u5206\u79bb\u521b\u5efa\u590d\u6742\u5bf9\u8c61\u7684\u6b65\u9aa4[\/caption]<br \/><\/noscript>\n&emsp;&emsp;\u5f53\u4f60\u9700\u8981\u521b\u5efa\u4e0d\u540c\u5f62\u5f0f\u7684\u4ea7\u54c1\u65f6\uff0c\u5176\u4e2d\u7684\u4e00\u4e9b\u6784\u9020\u6b65\u9aa4\u53ef\u80fd\u9700\u8981\u4e0d\u540c\u7684\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u6728\u5c4b\u7684\u623f\u95e8\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u6728\u5934\u5236\u9020\uff0c\u800c\u57ce\u5821\u7684\u623f\u95e8\u5219\u5fc5\u987b\u4f7f\u7528\u77f3\u5934\u5236\u9020\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u4e0d\u540c\u7684\u751f\u6210\u5668\uff0c\u7528\u4e0d\u540c\u65b9\u5f0f\u5b9e\u73b0\u4e00\u7ec4\u76f8\u540c\u7684\u521b\u5efa\u6b65\u9aa4\u3002\u7136\u540e\u4f60\u5c31\u53ef\u4ee5\u5728\u521b\u5efa\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u751f\u6210\u5668\uff08\u4f8b\u5982\u6309\u987a\u5e8f\u8c03\u7528\u591a\u4e2a\u6784\u9020\u6b65\u9aa4\uff09\u6765\u751f\u6210\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u3002<br \/>\n<figure style=\"width: 600px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/builder-comic-1-zh.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"600\" height=\"300\" alt=\"\u56fe5\" class=\"size-full\" \/><figcaption class=\"wp-caption-text\">\u56fe5\uff1a\u4e0d\u540c\u751f\u6210\u5668\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u6267\u884c\u76f8\u540c\u7684\u4efb\u52a1<\/figcaption><\/figure><br \/ >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/builder-comic-1-zh.png\" width=\"600\" height=\"300\" alt=\"\u56fe5\" class=\"size-full\" \/> \u56fe5\uff1a\u4e0d\u540c\u751f\u6210\u5668\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u6267\u884c\u76f8\u540c\u7684\u4efb\u52a1[\/caption]<br \/><\/noscript>\n&emsp;&emsp;\u4f8b\u5982\uff0c\u5047\u8bbe\u7b2c\u4e00\u4e2a\u5efa\u9020\u8005\u4f7f\u7528\u6728\u5934\u548c\u73bb\u7483\u5236\u9020\u623f\u5c4b\uff0c\u7b2c\u4e8c\u4e2a\u5efa\u9020\u8005\u4f7f\u7528\u77f3\u5934\u548c\u94a2\u94c1\uff0c\u800c\u7b2c\u4e09\u4e2a\u5efa\u9020\u8005\u4f7f\u7528\u9ec4\u91d1\u548c\u94bb\u77f3\u3002\u5728\u8c03\u7528\u540c\u4e00\u7ec4\u6b65\u9aa4\u540e\uff0c\u7b2c\u4e00\u4e2a\u5efa\u9020\u8005\u4f1a\u7ed9\u4f60\u4e00\u680b\u666e\u901a\u623f\u5c4b\uff0c\u7b2c\u4e8c\u4e2a\u4f1a\u7ed9\u4f60\u4e00\u5ea7\u5c0f\u57ce\u5821\uff0c\u800c\u7b2c\u4e09\u4e2a\u5219\u4f1a\u7ed9\u4f60\u4e00\u5ea7\u5bab\u6bbf\u3002\u4f46\u662f\uff0c\u53ea\u6709\u5728\u8c03\u7528\u6784\u9020\u6b65\u9aa4\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u901a\u7528\u63a5\u53e3\u4e0e\u5efa\u9020\u8005\u8fdb\u884c\u4ea4\u4e92\u65f6\uff0c\u8fd9\u6837\u7684\u8c03\u7528\u624d\u80fd\u8fd4\u56de\u9700\u8981\u7684\u623f\u5c4b\u3002<\/p>\n<h3>3.2 \u4e3b\u7ba1<\/h3>\n<p>&emsp;&emsp;\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5c06\u7528\u4e8e\u521b\u5efa\u4ea7\u54c1\u7684\u4e00\u7cfb\u5217\u751f\u6210\u5668\u6b65\u9aa4\u8c03\u7528\u62bd\u53d6\u6210\u4e3a\u5355\u72ec\u7684\u4e3b\u7ba1\u7c7b\u3002\u4e3b\u7ba1\u7c7b\u53ef\u5b9a\u4e49\u521b\u5efa\u6b65\u9aa4\u7684\u6267\u884c\u987a\u5e8f\uff0c\u800c\u751f\u6210\u5668\u5219\u63d0\u4f9b\u8fd9\u4e9b\u6b65\u9aa4\u7684\u5b9e\u73b0\u3002<br \/>\n<figure style=\"width: 343px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/builder-comic-2-zh.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"343\" height=\"300\" alt=\"\u56fe6\" class=\"size-full\" \/><figcaption class=\"wp-caption-text\">\u56fe6\uff1a\u4e3b\u7ba1\u77e5\u9053\u9700\u8981\u54ea\u4e9b\u521b\u5efa\u6b65\u9aa4\u624d\u80fd\u83b7\u5f97\u53ef\u6b63\u5e38\u4f7f\u7528\u7684\u4ea7\u54c1<\/figcaption><\/figure><br \/ >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/builder-comic-2-zh.png\" width=\"343\" height=\"300\" alt=\"\u56fe6\" class=\"size-full\" \/> \u56fe6\uff1a\u4e3b\u7ba1\u77e5\u9053\u9700\u8981\u54ea\u4e9b\u521b\u5efa\u6b65\u9aa4\u624d\u80fd\u83b7\u5f97\u53ef\u6b63\u5e38\u4f7f\u7528\u7684\u4ea7\u54c1[\/caption]<br \/><\/noscript>\n&emsp;&emsp;\u4e25\u683c\u6765\u8bf4\uff0c\u4f60\u7684\u7a0b\u5e8f\u4e2d\u5e76\u4e0d\u4e00\u5b9a\u9700\u8981\u4e3b\u7ba1\u7c7b\u3002\u5ba2\u6237\u7aef\u4ee3\u7801\u53ef\u76f4\u63a5\u4ee5\u7279\u5b9a\u987a\u5e8f\u8c03\u7528\u521b\u5efa\u6b65\u9aa4\u3002\u4e0d\u8fc7\uff0c\u4e3b\u7ba1\u7c7b\u4e2d\u975e\u5e38\u9002\u5408\u653e\u5165\u5404\u79cd\u4f8b\u884c\u6784\u9020\u6d41\u7a0b\uff0c\u4ee5\u4fbf\u5728\u7a0b\u5e8f\u4e2d\u53cd\u590d\u4f7f\u7528\u3002\u6b64\u5916\uff0c\u5bf9\u4e8e\u5ba2\u6237\u7aef\u4ee3\u7801\u6765\u8bf4\uff0c\u4e3b\u7ba1\u7c7b\u5b8c\u5168\u9690\u85cf\u4e86\u4ea7\u54c1\u6784\u9020\u7ec6\u8282\u3002\u5ba2\u6237\u7aef\u53ea\u9700\u8981\u5c06\u4e00\u4e2a\u751f\u6210\u5668\u4e0e\u4e3b\u7ba1\u7c7b\u5173\u8054\uff0c\u7136\u540e\u4f7f\u7528\u4e3b\u7ba1\u7c7b\u6765\u6784\u9020\u4ea7\u54c1\uff0c\u5c31\u80fd\u4ece\u751f\u6210\u5668\u5904\u83b7\u5f97\u6784\u9020\u7ed3\u679c\u4e86\u3002<br \/>\n&emsp;&emsp;\u4e3b\u7ba1\u53ea\u8d1f\u8d23\u751f\u6210\u7684\u6b65\u9aa4\uff0c\u5b8c\u6210\u540e\u4ecd\u7136\u4ece\u751f\u6210\u5668\u4e2d\u83b7\u5f97\u4ea7\u54c1<\/p>\n<h2>\u56db\u3001UML<\/h2>\n<figure style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/structure-1.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"460\" height=\"580\" alt=\"\u56fe7\" class=\"size-full\" \/ ><figcaption class=\"wp-caption-text\"><noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/06\/structure-1.png\" width=\"460\" height=\"580\" alt=\"\u56fe7\" class=\"size-full\" \/><\/noscript> \u56fe7\uff1a\u751f\u6210\u5668\u7ed3\u6784<\/figcaption><\/figure>\n<h2>\u4e94\u3001\u5b9e\u4f8b<\/h2>\n<p>&emsp;&emsp;\u4e0b\u9762\u6211\u4eec\u6765\u4fee\u4e2a\u623f\u5c4b\uff0c\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u6f14\u793a\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;vector&gt;\n\n\/\/ \u4ea7\u54c1\uff08\u623f\u5c4b\uff09: \u901a\u8fc7Builder\u88c5\u914d\u540e\u751f\u4ea7\u7684\u96f6\u4ef6\nclass House\n{\npublic:\n    \/\/ \u5b58\u653e\u5177\u4f53\u4ea7\u54c1\u7684parts\n    std::vector&lt;std::string&gt; m_parts;\n\n    void showPartsList()\n    {\n        for (auto i : m_parts)\n        {\n            std::cout &lt;&lt; i &lt;&lt; &#039; &#039;;\n        }\n        std::cout &lt;&lt; std::endl;\n    }\n};\n\nclass Builder\n{\npublic:\n    virtual ~Builder() {}\n\n    virtual void ProducePartA() const = 0;\n\n    virtual void ProducePartB() const = 0;\n\n    virtual void ProducePartC() const = 0;\n};\n\nclass HouseBuilder : public Builder\n{\nprivate:\n    House* m_house;\npublic:\n    HouseBuilder() : Builder()\n    {\n        this-&gt;Reset();\n        \/\/ \u65b0\u521b\u5efa\u7684Builder\u7684\u4ea7\u54c1\u5fc5\u987b\u662f\u7a7a\u7684\n    }\n\n    ~HouseBuilder()\n    {\n        delete m_house;\n    }\n\n    \/\/ \u91cd\u8bbe\u4ea7\u54c1\n    void Reset()\n    {\n        this-&gt;m_house = new House();\n    }\n\n    void ProducePartA() const override\n    {\n        this-&gt;m_house-&gt;m_parts.push_back(&quot;Door&quot;);\n    }\n\n    void ProducePartB() const override\n    {\n        this-&gt;m_house-&gt;m_parts.push_back(&quot;Floor&quot;);\n    }\n\n    void ProducePartC() const override\n    {\n        this-&gt;m_house-&gt;m_parts.push_back(&quot;Walls&quot;);\n    }\n\n    \/\/ \u83b7\u5f97\u4ea7\u54c1\n    House* getHouse()\n    {\n        House* re = this-&gt;m_house;\n        this-&gt;Reset();\n        return re;\n    }\n};\n\n\/\/ \u4e3b\u7ba1\nclass Director\n{\nprivate:\n    Builder* m_Builder;\npublic:\n    \/\/ \u4e3b\u7ba1\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u7684Builder\uff0c\u4f46\u9700\u8981\u5ba2\u6237\u7aef\u6307\u5b9a\n    void setBuilder(Builder* p_Builder)\n    {\n        this-&gt;m_Builder = p_Builder;\n    }\n\n    void BuildNormalHouse()\n    {\n        this-&gt;m_Builder-&gt;ProducePartA();\n        this-&gt;m_Builder-&gt;ProducePartB();\n        this-&gt;m_Builder-&gt;ProducePartC();\n    }\n\n    void BuildAbnormalHouse()\n    {\n        this-&gt;m_Builder-&gt;ProducePartB();\n    }\n};\n\n\/\/ \u5ba2\u6237\u7aef\u4ee3\u7801\nvoid Client(Director&amp; p_director)\n{\n    \/* ===== Section 0 : \u751f\u4ea7\u4e00\u4e2a\u6b63\u5e38\u7684\u623f\u5c4b ===== *\/\n    std::cout &lt;&lt; &quot;Build A Normal House&quot; &lt;&lt; std::endl;\n\n    \/\/ \u7ed9\u4e3b\u7ba1\u6307\u5b9a\u751f\u6210\u5668\n    HouseBuilder* houseBuilder = new HouseBuilder();\n    p_director.setBuilder(houseBuilder);\n\n    \/\/ \u4e3b\u7ba1\u751f\u4ea7\u540e\u53d6\u56de\u4ea7\u54c1\n    p_director.BuildNormalHouse();\n    House* normalHouse = houseBuilder-&gt;getHouse();\n\n    normalHouse-&gt;showPartsList();\n    delete normalHouse;\n\n    \/* ===== Section 1 : \u751f\u4ea7\u4e00\u4e2a\u4e0d\u6b63\u5e38\u7684\u623f\u5c4b ===== *\/\n    std::cout &lt;&lt; &quot;Build An Abnormal House&quot; &lt;&lt; std::endl;\n\n    \/\/ \u4e3b\u7ba1\u751f\u4ea7\u540e\u53d6\u56de\u4ea7\u54c1\n    p_director.BuildAbnormalHouse();\n    House* abnormalHouse = houseBuilder-&gt;getHouse();\n\n    abnormalHouse-&gt;showPartsList();\n    delete abnormalHouse;\n\n    delete houseBuilder;\n    return;\n}\n\nint main()\n{\n    Director* director = new Director();\n    Client(*director);\n\n    delete director;\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u610f\u56fe &emsp;&emsp;\u5c06\u4e00\u4e2a\u590d\u6742\u5bf9\u8c61\u7684\u6784\u5efa\u4e0e\u5b83\u7684\u8868\u793a\u5206\u79bb\uff0c\u4f7f\u5f97\u540c\u6837\u7684\u6784\u5efa\u8fc7\u7a0b\u53ef\u4ee5\u521b\u5efa\u4e0d\u540c\u7684\u8868\u793a\u3002 &emsp;&#038;ems &#8230","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\/316"}],"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=316"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/316\/revisions"}],"predecessor-version":[{"id":461,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/316\/revisions\/461"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}