{"id":325,"date":"2021-06-12T11:23:28","date_gmt":"2021-06-12T03:23:28","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=325"},"modified":"2025-10-09T13:56:04","modified_gmt":"2025-10-09T05:56:04","slug":"design-pattern-07-decorator","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/06\/12\/design-pattern-07-decorator\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f 07 \u88c5\u9970\u6a21\u5f0f"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h2>\u4e00\u3001\u610f\u56fe<\/h2>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u52a8\u6001\u5730\u7ed9\u4e00\u4e2a\u5bf9\u8c61\u6dfb\u52a0\u4e00\u4e9b\u989d\u5916\u7684\u804c\u8d23\u3002\u5c31\u589e\u52a0\u529f\u80fd\u6765\u8bf4\uff0cDecorator\u6bd4\u751f\u6210\u5b50\u7c7b\u66f4\u52a0\u7075\u6d3b\u3002<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\u4eba\u8bdd\uff1a\u6211\u4eec\u5e0c\u671b\u5bf9\u57fa\u7c7b\u521b\u5efa\u7684\u90e8\u5206\u5bf9\u8c61\u6dfb\u52a0\u67d0\u4e9b\u529f\u80fd\uff0c\u8fd9\u65f6\u5019\u4f7f\u7528\u88c5\u9970\u6a21\u5f0f\u6bd4\u521b\u5efa\u591a\u4e2a\u5b50\u7c7b\u66f4\u65b9\u4fbf\u3002<\/p>\n<h2>\u4e8c\u3001\u5b9e\u9645\u95ee\u9898<\/h2>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u73b0\u5728\u6211\u4eec\u6709\u4e00\u4e2a\u57fa\u7c7b\u4ea7\u54c1\uff1a\u5976\u8336\u3002\u73b0\u5728\u6211\u4eec\u6709\u591a\u79cd\u6dfb\u52a0\u7269\uff1a\u73cd\u73e0\u3001\u6930\u679c\u3001\u5e03\u4e01\u3001\u7ea2\u8c46\u7b49\u3002<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\u4e3a\u4e86\u5b9e\u73b0\u591a\u79cd\u6dfb\u52a0\u7269\u7684\u81ea\u7531\u7ec4\u5408\uff0c\u6211\u4eec\u73b0\u5728\u53c8\u4e24\u79cd\u65b9\u5f0f\uff1a\uff081\uff09\u4f7f\u7528\u7ee7\u627f\u7684\u65b9\u5f0f\uff0c\u7ee7\u627f\u51fa\u591a\u79cd\u4e0d\u540c\u7684\u7ec4\u5408\u5b50\u7c7b\uff1b\uff082\uff09\u5c06\u6dfb\u52a0\u7269\u4f5c\u4e3a\u5976\u8336\u7684\u6210\u5458\uff0c\u4f7f\u7528\u7ec4\u5408\u7684\u65b9\u5f0f\u5b8c\u6210\u3002\u4f7f\u7528\u7ee7\u627f\u65b9\u5f0f\u7684\u7f3a\u70b9\u5f88\u660e\u663e\uff1a\u4f1a\u6709\u5f88\u591a\u5b50\u7c7b\u3002\u4f7f\u7528\u7ec4\u5408\u65b9\u5f0f\u6765\u5e26\u7684\u6700\u5927\u95ee\u9898\u662f\uff1a\u5c06\u4f1a\u4ea7\u751f\u5f88\u957f\u7684\u6784\u9020\u51fd\u6570\u53c2\u6570\u3002<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\u4e0b\u9762\u6211\u4eec\u5f15\u5165\u88c5\u9970\u6a21\u5f0f\u3002<\/p>\n<h2>\u4e09\u3001UML<\/h2>\n<figure style=\"width: 851px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/07\/Decorator_UML.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"851\" height=\"436\" 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\/07\/Decorator_UML.png\" width=\"851\" height=\"436\" alt=\"\u56fe1\" class=\"size-full\" \/><\/noscript> \u56fe1\uff1a\u88c5\u9970\u6a21\u5f0fUML<\/figcaption><\/figure>\n<h2>\u56db\u3001\u5b9e\u4f8b<\/h2>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u4e0b\u9762\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u5b9e\u4f8b\uff1a\u73b0\u5728\u6211\u4eec\u6709\u4e00\u4e2a\u4ea7\u54c1\u2014\u2014\u5976\u8336\uff0c\u4ee5\u53ca\u4e24\u4e2a\u6dfb\u52a0\u7269\uff1a\u73cd\u73e0\u548c\u6930\u679c\u3002\u73b0\u5728\u6211\u4eec\u5e0c\u671b\u80fd\u81ea\u5b9a\u4e49\u7684\u4e3a\u4e0d\u540c\u5976\u8336\u5bf9\u8c61\u6dfb\u52a0\u6930\u679c\u548c\u73cd\u73e0\uff0c\u540c\u65f6\u6211\u4eec\u5e0c\u671b\u80fd\u67e5\u770b\u6dfb\u52a0\u7269\u4ee5\u53ca\u6dfb\u52a0\u540e\u7684\u603b\u4ef7\u683c\u3002<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\u6211\u4eec\u9009\u62e9\u4e00\u4e2a\u62bd\u8c61\u4ea7\u54c1Imilktea\u4f5c\u4e3a\u57fa\u7c7b\uff0c\u6d3e\u751f\u51fa\u5177\u4f53\u4ea7\u54c1\u7c7b\uff1aMilktea\u3001\u88c5\u9970\u7c7bDecorator\uff0c\u540c\u65f6\u88c5\u9970\u7c7b\u6d3e\u751f\u51fa\u4e24\u4e2a\u5177\u4f53\u7684\u88c5\u9970\u7269\uff1a\u73cd\u73e0Bubble\u548c\u6930\u679cCoconut\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n\n\/* Visual Component *\/\nclass Imilktea\n{\nprotected:\n    int m_price;\npublic:\n    Imilktea(const int&amp; p_price = 10)\n    {\n        m_price = p_price;\n    }\n\n    virtual void show() const = 0;\n\n    virtual int price() const = 0;\n};\n\nclass Milktea : public Imilktea\n{\npublic:\n    Milktea(const int&amp; p_price = 10) : Imilktea(p_price) {}\n\n    void show() const override\n    {\n        std::cout &lt;&lt; &quot;Milktea&quot; &lt;&lt; std::endl;\n    }\n\n    int price() const override\n    {\n        return m_price;\n    }\n};\n\nclass Decorator : public Imilktea\n{\nprotected:\n    Imilktea* m_Component;\npublic:\n    Decorator(Imilktea* p, const int&amp; p_price) : Imilktea(p_price)\n    {\n        m_Component = p;\n    }\n\n    void show() const override\n    {\n        this-&gt;m_Component-&gt;show();\n    }\n\n    int price() const override\n    {\n        return this-&gt;m_Component-&gt;price();\n    }\n};\n\n\/* Concrete Decorator : \u73cd\u73e0 *\/\nclass Bubble : public Decorator\n{\nprivate:\n    int m_DecPrice;     \/\/ \u6dfb\u52a0\u7269\u5355\u4ef7\npublic:\n    Bubble(Imilktea* p, const int&amp; p_DecPrice) : Decorator(p, p-&gt;price())\n    {\n        m_DecPrice = p_DecPrice;\n    }\n\n    void show() const override\n    {\n        std::cout &lt;&lt; &quot;Bubble&quot; &lt;&lt; std::endl;\n        this-&gt;m_Component-&gt;show();\n    }\n\n    int price() const override\n    {\n        return m_DecPrice + this-&gt;m_Component-&gt;price();\n    }\n};\n\n\/* Concrete Decorator : \u6930\u679c *\/\nclass Coconut : public Decorator\n{\nprivate:\nprivate:\n    int m_DecPrice;     \/\/ \u6dfb\u52a0\u7269\u5355\u4ef7\npublic:\n    Coconut(Imilktea* p, const int&amp; p_DecPrice) : Decorator(p, p-&gt;price())\n    {\n        m_DecPrice = p_DecPrice;\n    }\n\n    void show() const override\n    {\n        std::cout &lt;&lt; &quot;Coconut&quot; &lt;&lt; std::endl;\n        this-&gt;m_Component-&gt;show();\n    }\n\n    int price() const override\n    {\n        return m_DecPrice + this-&gt;m_Component-&gt;price();\n    }\n};\n\nvoid Client(Imilktea* milktea)\n{\n    std::cout &lt;&lt; &quot;\u5976\u8336\u7684\u6210\u5206\uff1a&quot; &lt;&lt; std::endl;\n    milktea-&gt;show();\n    std::cout &lt;&lt; &quot;\u5976\u8336\u7684\u4ef7\u683c\uff1a&quot; &lt;&lt; milktea-&gt;price() &lt;&lt; std::endl;\n\n}\n\nint main()\n{\n    \/* \u9010\u5c42\u88c5\u9970 *\/\n    Imilktea* baseMilktea = new Milktea(10);    \/\/ \u57fa\u672c\u5976\u8336\n    Imilktea* mtAddBubble = new Bubble(baseMilktea, 5);     \/\/ \u52a0\u73cd\u73e0\n    Client(mtAddBubble);\n    Imilktea* mtAddBubbleAddCoconut = new Coconut(mtAddBubble, 3);  \/\/ \u52a0\u73cd\u73e0\u3001\u6930\u679c\n    Client(mtAddBubbleAddCoconut);\n    Imilktea* mtAddDoubleBubble = new Bubble(mtAddBubble, 5);       \/\/ \u52a0\u4e24\u5206\u73cd\u73e0\n    Client(mtAddDoubleBubble);\n\n    delete baseMilktea, mtAddBubble, mtAddBubbleAddCoconut, mtAddDoubleBubble;\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u610f\u56fe &nbsp;&nbsp;&nbsp;&nbsp;\u52a8\u6001\u5730\u7ed9\u4e00\u4e2a\u5bf9\u8c61\u6dfb\u52a0\u4e00\u4e9b\u989d\u5916\u7684\u804c\u8d23\u3002\u5c31\u589e\u52a0\u529f\u80fd\u6765\u8bf4\uff0cDecorator\u6bd4 &#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\/325"}],"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=325"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/325\/revisions"}],"predecessor-version":[{"id":462,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/325\/revisions\/462"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}