{"id":282,"date":"2021-06-09T21:08:34","date_gmt":"2021-06-09T13:08:34","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=282"},"modified":"2025-10-09T13:56:05","modified_gmt":"2025-10-09T05:56:05","slug":"design-pattern-03-abstract-factory-pattern","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/06\/09\/design-pattern-03-abstract-factory-pattern\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f 03 \u62bd\u8c61\u5de5\u5382\u6a21\u5f0f"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h2>\u4e00\u3001\u610f\u56fe<\/h2>\n<p>&emsp;&emsp;\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u4e00\u521b\u5efa\u4e00\u7cfb\u5217\u76f8\u5173\u6216\u76f8\u4e92\u4f9d\u8d56\u7684\u5bf9\u8c61\uff0c\u800c\u65e0\u9700\u6307\u5b9a\u5b83\u4eec\u5177\u4f53\u7684\u7c7b\u3002<\/p>\n<h2>\u4e8c\u3001UML<\/h2>\n<figure style=\"width: 991px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/03\/Abstract-Factory.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"991\" height=\"442\" 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\/03\/Abstract-Factory.png\" width=\"991\" height=\"442\" alt=\"\u56fe1\" class=\"size-full\" \/><\/noscript> \u56fe1\uff1a\u62bd\u8c61\u5de5\u5382\u6a21\u5f0fUML<\/figcaption><\/figure>\n<h2>\u4e09\u3001\u95ee\u9898<\/h2>\n<p>&emsp;&emsp;\u5047\u8bbe\u6211\u4eec\u5f00\u53d1\u4e00\u4e9b\u5217\u4ea7\u54c1\uff0c\u4f8b\u5982\uff1a\u6905\u5b50Chair\u3001\u6c99\u53d1Sofa\u548c\u5496\u5561\u684cCoffeTable\u3002\u73b0\u5728\u6211\u4eec\u6709\u591a\u79cd\u4ea7\u54c1\u7684\u53d8\u4f53\uff1a\u5982\u73b0\u4ee3Modern\u3001\u7ef4\u591a\u5229\u4e9aVectorian\u3001\u88c5\u9970\u98ce\u827a\u672fArtDeco\u7b49\u98ce\u683c\u6765\u751f\u6210\u4e0e\u6905\u5b50\u3001\u6c99\u53d1\u548c\u5496\u5561\u684c\u3002<br \/>\n<figure style=\"width: 430px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/03\/proble_Abastract_Factory.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"430\" height=\"300\" alt=\"\u56fe2\" class=\"size-full\" \/><figcaption class=\"wp-caption-text\">\u56fe2\uff1a\u95ee\u9898\u6a21\u578b<\/figcaption><\/figure><br \/ >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/03\/proble_Abastract_Factory.png\" width=\"430\" height=\"300\" alt=\"\u56fe2\" class=\"size-full\" \/> \u56fe2\uff1a\u95ee\u9898\u6a21\u578b[\/caption]<br \/><\/noscript>\n&emsp;&emsp;\u4e3a\u4e86\u786e\u4fdd\u4ea7\u54c1\u751f\u6210\u7684\u98ce\u683c\u4e00\u81f4\uff0c\u540c\u65f6\u51cf\u5c11\u589e\u6dfb\u65b0\u4ea7\u54c1\u6216\u65b0\u98ce\u683c\u65f6\u7684\u4ee3\u7801\u4fee\u6539\u91cf\uff0c\u6211\u4eec\u5f15\u5165\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\u3002<\/p>\n<h2>\u56db\u3001\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>&emsp;&emsp;\u9996\u5148\uff0c\u62bd\u8c61\u5de5\u5382\u6a21\u5f0f\u5efa\u8bae\u4e3a\u7cfb\u5217\u4e2d\u7684\u6bcf\u4ef6\u4ea7\u54c1\u660e\u786e\u58f0\u660e\u63a5\u53e3\uff08\u4f8b\u5982\u6905\u5b50\u3001 \u6c99\u53d1\u6216\u5496\u5561\u684c\uff09\u3002\u7136\u540e\uff0c\u786e\u4fdd\u6240\u6709\u4ea7\u54c1\u53d8\u4f53\u90fd\u7ee7\u627f\u8fd9\u4e9b\u63a5\u53e3\u3002\u4f8b\u5982\uff0c\u6240\u6709\u98ce\u683c\u7684\u6905\u5b50\u90fd\u5b9e\u73b0\u6905\u5b50\u63a5\u53e3\uff1b\u6240\u6709\u98ce\u683c\u7684\u5496\u5561\u684c\u90fd\u5b9e\u73b0\u5496\u5561\u684c\u63a5\u53e3\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<br \/>\n<figure style=\"width: 420px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/03\/AF_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=\"420\" height=\"280\" alt=\"\u56fe3\" class=\"size-full\" \/><figcaption class=\"wp-caption-text\">\u56fe3\uff1a\u521b\u5efa\u63a5\u53e3<\/figcaption><\/figure><br \/ >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/03\/AF_solution1.png\" width=\"420\" height=\"280\" alt=\"\u56fe3\" class=\"size-full\" \/> \u56fe3\uff1a\u521b\u5efa\u63a5\u53e3[\/caption]<br \/><\/noscript>\n&emsp;&emsp;\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u58f0\u660e\u62bd\u8c61\u5de5\u5382\u2014\u2014\u5305\u542b\u7cfb\u5217\u4e2d\u6240\u6709\u4ea7\u54c1\u6784\u9020\u65b9\u6cd5\u7684\u63a5\u53e3\u3002\u4f8b\u5982 createChair\u521b\u5efa\u6905\u5b50 \u3001createSofa\u521b\u5efa\u6c99\u53d1\u548ccreateCoffeeTable\u521b\u5efa\u5496\u5561\u684c\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5fc5\u987b\u8fd4\u56de\u62bd\u8c61\u4ea7\u54c1\u7c7b\u578b\uff0c\u5373\u6211\u4eec\u4e4b\u524d\u62bd\u53d6\u7684\u90a3\u4e9b\u63a5\u53e3\uff1a\u6905\u5b50\uff0c\u6c99\u53d1\u548c\u5496\u5561\u684c\u7b49\u7b49\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\/03\/AF_solution2.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=\"320\" alt=\"\u56fe4\" class=\"size-full\" \/><figcaption class=\"wp-caption-text\">\u56fe4\uff1a\u62bd\u8c61\u5de5\u5382<\/figcaption><\/figure><br \/ >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/03\/AF_solution2.png\" width=\"640\" height=\"320\" alt=\"\u56fe4\" class=\"size-full\" \/> \u56fe4\uff1a\u62bd\u8c61\u5de5\u5382[\/caption]<br \/><\/noscript>\n&emsp;&emsp;\u90a3\u4e48\u8be5\u5982\u4f55\u5904\u7406\u4ea7\u54c1\u53d8\u4f53\u5462\uff1f\u5bf9\u4e8e\u7cfb\u5217\u4ea7\u54c1\u7684\u6bcf\u4e2a\u53d8\u4f53\uff0c\u6211\u4eec\u90fd\u5c06\u57fa\u4e8e\u62bd\u8c61\u5de5\u5382\u63a5\u53e3\u521b\u5efa\u4e0d\u540c\u7684\u5de5\u5382\u7c7b\u3002\u6bcf\u4e2a\u5de5\u5382\u7c7b\u90fd\u53ea\u80fd\u8fd4\u56de\u7279\u5b9a\u7c7b\u522b\u7684\u4ea7\u54c1\uff0c\u4f8b\u5982\uff0c\u73b0\u4ee3\u5bb6\u5177\u5de5\u5382ModernFurnitureFactory\u53ea\u80fd\u521b\u5efa\u73b0\u4ee3\u6905\u5b50ModernChair\u3001\u73b0\u4ee3\u6c99\u53d1ModernSofa\u548c\u73b0\u4ee3\u5496\u5561\u684cModernCoffeeTable\u5bf9\u8c61\u3002<br \/>\n&emsp;&emsp;\u5ba2\u6237\u7aef\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u76f8\u5e94\u7684\u62bd\u8c61\u63a5\u53e3\u8c03\u7528\u5de5\u5382\u548c\u4ea7\u54c1\u7c7b\u3002 \u4f60\u65e0\u9700\u4fee\u6539\u5b9e\u9645\u5ba2\u6237\u7aef\u4ee3\u7801\uff0c \u5c31\u80fd\u66f4\u6539\u4f20\u9012\u7ed9\u5ba2\u6237\u7aef\u7684\u5de5\u5382\u7c7b\uff0c \u4e5f\u80fd\u66f4\u6539\u5ba2\u6237\u7aef\u4ee3\u7801\u63a5\u6536\u7684\u4ea7\u54c1\u53d8\u4f53\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\/03\/AF_abstract-factory-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=\"600\" height=\"300\" alt=\"\u56fe5\" class=\"size-full\" \/><figcaption class=\"wp-caption-text\">\u56fe5<\/figcaption><\/figure><br \/ >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/03\/AF_abstract-factory-comic-2-zh.png\" width=\"600\" height=\"300\" alt=\"\u56fe5\" class=\"size-full\" \/> \u56fe5[\/caption]<br \/><\/noscript>\n&emsp;&emsp;\u5047\u8bbe\u5ba2\u6237\u7aef\u60f3\u8981\u5de5\u5382\u521b\u5efa\u4e00\u628a\u6905\u5b50\u3002\u5ba2\u6237\u7aef\u65e0\u9700\u4e86\u89e3\u5de5\u5382\u7c7b\uff0c\u4e5f\u4e0d\u7528\u7ba1\u5de5\u5382\u7c7b\u521b\u5efa\u51fa\u7684\u6905\u5b50\u7c7b\u578b\u3002\u65e0\u8bba\u662f\u73b0\u4ee3\u98ce\u683c\uff0c\u8fd8\u662f\u7ef4\u591a\u5229\u4e9a\u98ce\u683c\u7684\u6905\u5b50\uff0c\u5bf9\u4e8e\u5ba2\u6237\u7aef\u6765\u8bf4\u6ca1\u6709\u5206\u522b\uff0c\u5b83\u53ea\u9700\u8c03\u7528\u62bd\u8c61\u6905\u5b50\u63a5\u53e3\u5c31\u53ef\u4ee5\u4e86\u3002 \u8fd9\u6837\u4e00\u6765\uff0c\u5ba2\u6237\u7aef\u53ea\u9700\u77e5\u9053\u6905\u5b50\u4ee5\u67d0\u79cd\u65b9\u5f0f\u5b9e\u73b0\u4e86sitOn\u5750\u4e0b\u65b9\u6cd5\u5c31\u8db3\u591f\u4e86\u3002\u6b64\u5916\uff0c\u65e0\u8bba\u5de5\u5382\u8fd4\u56de\u7684\u662f\u4f55\u79cd\u6905\u5b50\u53d8\u4f53\uff0c\u5b83\u90fd\u4f1a\u548c\u7531\u540c\u4e00\u5de5\u5382\u5bf9\u8c61\u521b\u5efa\u7684\u6c99\u53d1\u6216\u5496\u5561\u684c\u98ce\u683c\u4e00\u81f4\u3002<br \/>\n&emsp;&emsp;\u6700\u540e\u4e00\u70b9\u8bf4\u660e\uff1a \u5982\u679c\u5ba2\u6237\u7aef\u4ec5\u63a5\u89e6\u62bd\u8c61\u63a5\u53e3\uff0c \u90a3\u4e48\u8c01\u6765\u521b\u5efa\u5b9e\u9645\u7684\u5de5\u5382\u5bf9\u8c61\u5462\uff1f \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c \u5e94\u7528\u7a0b\u5e8f\u4f1a\u5728\u521d\u59cb\u5316\u9636\u6bb5\u521b\u5efa\u5177\u4f53\u5de5\u5382\u5bf9\u8c61\u3002 \u800c\u5728\u6b64\u4e4b\u524d\uff0c \u5e94\u7528\u7a0b\u5e8f\u5fc5\u987b\u6839\u636e\u914d\u7f6e\u6587\u4ef6\u6216\u73af\u5883\u8bbe\u5b9a\u9009\u62e9\u5de5\u5382\u7c7b\u522b\u3002<\/p>\n<h2>\u4e94\u3001\u5b9e\u4f8b<\/h2>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;memory&gt;\n\n\/* ==============================*\/\n\/* ===== Product 00 : Chair =====*\/\n\/* ==============================*\/\nclass AbstractChair\n{\npublic:\n    virtual ~AbstractChair() {}\n\n    virtual std::string GetChairName() const = 0;\n};\n\nclass ArtDecoChair : public AbstractChair\n{\npublic:\n    std::string GetChairName() const override\n    {\n        return &quot;Art Deco Chair&quot;;\n    }\n};\n\nclass VictorianChair : public AbstractChair\n{\npublic:\n    std::string GetChairName() const override\n    {\n        return &quot;Victorian Chair&quot;;\n    }\n};\n\nclass ModernChair : public AbstractChair\n{\npublic:\n    std::string GetChairName() const override\n    {\n        return &quot;Modern Chair&quot;;\n    }\n};\n\n\/* =============================*\/\n\/* ===== Product 02 : Sofa =====*\/\n\/* =============================*\/\nclass AbstractSofa\n{\npublic:\n    virtual ~AbstractSofa() {}\n\n    virtual std::string GetSofaName() const = 0;\n};\n\nclass ArtDecoSofa : public AbstractSofa\n{\n    std::string GetSofaName() const override\n    {\n        return &quot;Art Deco Sofa&quot;;\n    }\n};\n\nclass VictorianSofa : public AbstractSofa\n{\n    std::string GetSofaName() const override\n    {\n        return &quot;Victorian Sofa&quot;;\n    }\n};\n\nclass ModernSofa : public AbstractSofa\n{\n    std::string GetSofaName() const override\n    {\n        return &quot;Modern Sofa&quot;;\n    }\n};\n\n\/* ===================*\/\n\/* ===== Factory =====*\/\n\/* ===================*\/\nclass AbstractFactory\n{\npublic:\n    virtual AbstractChair* CreateChair() const = 0;\n    virtual AbstractSofa* CreateSofa() const = 0;\n};\n\nclass ArtDecoFactory : public AbstractFactory\n{\npublic:\n    AbstractChair* CreateChair() const override\n    {\n        return new ArtDecoChair();\n    }\n\n    AbstractSofa* CreateSofa() const override\n    {\n        return new ArtDecoSofa();\n    }\n};\n\nclass VictorianFactory : public AbstractFactory\n{\npublic:\n    AbstractChair* CreateChair() const override\n    {\n        return new VictorianChair();\n    }\n\n    AbstractSofa* CreateSofa() const override\n    {\n        return new VictorianSofa();\n    }\n};\n\nclass ModernFactory : public AbstractFactory\n{\n    AbstractChair* CreateChair() const override\n    {\n        return new ModernChair();\n    }\n\n    AbstractSofa* CreateSofa() const override\n    {\n        return new ModernSofa();\n    }\n};\n\nvoid Cilent_ShowProduct(const AbstractFactory&amp; p_factory)\n{\n    const AbstractChair* chair = p_factory.CreateChair();\n    const AbstractSofa* sofa = p_factory.CreateSofa();\n\n    std::cout &lt;&lt; chair-&gt;GetChairName() &lt;&lt; std::endl;\n    std::cout &lt;&lt; sofa-&gt;GetSofaName() &lt;&lt; std::endl;\n\n    delete chair;\n    delete sofa;\n}\n\nint main()\n{\n    std::cout &lt;&lt; &quot;now is Art Deco Factory&quot; &lt;&lt; std::endl;\n    const ArtDecoFactory* artdecoFactory = new ArtDecoFactory();\n    Cilent_ShowProduct(*artdecoFactory);\n\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u610f\u56fe &emsp;&emsp;\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u4e00\u521b\u5efa\u4e00\u7cfb\u5217\u76f8\u5173\u6216\u76f8\u4e92\u4f9d\u8d56\u7684\u5bf9\u8c61\uff0c\u800c\u65e0\u9700\u6307\u5b9a\u5b83\u4eec\u5177\u4f53\u7684\u7c7b\u3002 \u4e8c\u3001UML \u4e09\u3001\u95ee\u9898 &#038; &#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\/282"}],"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=282"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/282\/revisions"}],"predecessor-version":[{"id":454,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/282\/revisions\/454"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}