{"id":290,"date":"2021-06-09T21:28:32","date_gmt":"2021-06-09T13:28:32","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=290"},"modified":"2025-10-09T13:56:05","modified_gmt":"2025-10-09T05:56:05","slug":"design-pattern-04-adapter-pattern","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/06\/09\/design-pattern-04-adapter-pattern\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f 04 \u9002\u914d\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\u7c7b\u7684\u63a5\u53e3\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u7528\u6237\u5e0c\u671b\u7684\u63a5\u53e3\u3002Adapter\u6a21\u5f0f\u4f7f\u5f97\u7531\u4e8e\u63a5\u53e3\u4e0d\u517c\u5bb9\u7684\u7c7b\u53ef\u4ee5\u4e00\u8d77\u5de5\u4f5c\u3002<\/p>\n<h2>\u4e8c\u3001UML<\/h2>\n<p><figure style=\"width: 721px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/04\/Adapter.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"721\" height=\"458\" alt=\"\u56fe1\" class=\"size-full\" \/><figcaption class=\"wp-caption-text\">\u56fe1\uff1a\u9002\u914d\u5668\u6a21\u5f0fUML<\/figcaption><\/figure><br \/ >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/04\/Adapter.png\" width=\"721\" height=\"458\" alt=\"\u56fe1\" class=\"size-full\" \/> \u56fe1\uff1a\u9002\u914d\u5668\u6a21\u5f0fUML[\/caption]<br \/><\/noscript>\n&emsp;&emsp;\u5bf9\u4e8e\u7c7b\u9002\u914d\u5668\uff0cC++\u53ef\u4ee5\u4f7f\u7528\u591a\u7ee7\u627f\u5b8c\u6210\uff0c\u5373\u5bf9Target\u4f7f\u7528public\u7ee7\u627f\uff0c\u5bf9Adaptee\u4f7f\u7528private\u7ee7\u627f\uff1b\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\u5219\u53ef\u4ee5\u9009\u62e9\u5c06Adaptee\u4f5c\u4e3aprivate\u6210\u5458\u53d8\u91cf\u4f7f\u7528\u3002<\/p>\n<h2>\u4e09\u3001\u793a\u4f8b<\/h2>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;memory&gt;\n\nclass Target\n{\npublic:\n    virtual ~Target() = default;\n\n    virtual std::string Request() const\n    {\n        return &quot;Target&quot;;\n    }\n};\n\nclass Adaptee\n{\npublic:\n    std::string SpecificRequest() const\n    {\n        return &quot;Adaptee&quot;;\n    }\n};\n\n\/\/ \u7c7b\u9002\u914d\u5668\nclass ClassAdapter : public Target, private Adaptee\n{\npublic:\n    ClassAdapter() : Target(), Adaptee() {}\n\n    std::string Request() const override\n    {\n        return &quot;ClassAdapter + &quot; + this-&gt;SpecificRequest();\n    }\n};\n\n\/\/ \u5bf9\u8c61\u9002\u914d\u5668\nclass ObjectAdapter : public Target\n{\nprivate:\n    Adaptee* adaptee;\npublic:\n    ObjectAdapter(Adaptee* p_adaptee) : Target()\n    {\n        adaptee = p_adaptee;\n    }\n\n    std::string Request() const override\n    {\n        return &quot;ObjectAdapter + &quot; + adaptee-&gt;SpecificRequest();\n    }\n};\n\nvoid Cilent(const Target* p_target)\n{\n    std::cout &lt;&lt; p_target-&gt;Request() &lt;&lt; std::endl;\n}\n\nint main()\n{\n    Target* myTarget = new Target();\n    Adaptee* myAdaptee = new Adaptee();\n    ClassAdapter* classAdapter = new ClassAdapter();\n    ObjectAdapter* objectAdapter = new ObjectAdapter(myAdaptee);\n\n    Cilent(myTarget);\n    Cilent(classAdapter);\n    Cilent(objectAdapter);\n\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u610f\u56fe &emsp;&emsp;\u5c06\u4e00\u4e2a\u7c7b\u7684\u63a5\u53e3\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u7528\u6237\u5e0c\u671b\u7684\u63a5\u53e3\u3002Adapter\u6a21\u5f0f\u4f7f\u5f97\u7531\u4e8e\u63a5\u53e3\u4e0d\u517c\u5bb9\u7684\u7c7b\u53ef\u4ee5\u4e00\u8d77\u5de5\u4f5c\u3002 &#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\/290"}],"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=290"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":455,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/290\/revisions\/455"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}