{"id":328,"date":"2021-06-12T11:27:25","date_gmt":"2021-06-12T03:27:25","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=328"},"modified":"2025-10-09T13:56:04","modified_gmt":"2025-10-09T05:56:04","slug":"design-pattern-08-strategy","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/06\/12\/design-pattern-08-strategy\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f 08 \u7b56\u7565\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;\u5b9a\u4e49\u4e00\u7cfb\u5217\u7684\u7b97\u6cd5\uff0c\u628a\u4ed6\u4eec\u4e00\u4e2a\u4e2a\u5c01\u88c5\u8d77\u6765\uff0c\u5e76\u4e14\u4f7f\u5b83\u4eec\u53ef\u4e92\u76f8\u66ff\u6362\u3002<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;\u4eba\u8bdd\uff1a\u6211\u6253\u5f00\u4e00\u4e2a\u5bfc\u822a\u5730\u56fe\uff0c\u5bfc\u822a\u529f\u80fd\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u4e0d\u540c\u6a21\u5f0f\uff08\u9a7e\u9a76\u3001\u9a91\u884c\u3001\u6b65\u884c\uff09\u3002<\/p>\n<h2>\u4e8c\u3001UML<\/h2>\n<figure style=\"width: 641px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Design%20Pattern\/08\/Strategy.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"641\" height=\"250\" 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\/08\/Strategy.png\" width=\"641\" height=\"250\" alt=\"\u56fe1\" class=\"size-full\" \/><\/noscript> \u56fe1\uff1a\u7b56\u7565\u6a21\u5f0fUML<\/figcaption><\/figure>\n<h2>\u4e09\u3001\u5b9e\u4f8b<\/h2>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u5047\u8bbe\u73b0\u5728\u6211\u4eec\u6709\u4e00\u4e2a\u5bfc\u822a\u7cfb\u7edf\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u7528\u6237\u7684\u9009\u62e9\u4f7f\u7528\u4e0d\u540c\u7684\u5bfc\u822a\u6a21\u5f0f\u3002\u6211\u4eec\u9009\u62e9User\u4f5c\u4e3aContext\uff0c\u5c06Navigator\u4f5c\u4e3aStrategy\u3002<\/p>\n<pre><code class=\"language-cpp\">#include &lt;iostream&gt;\n\nclass Navigator\n{\npublic:\n    virtual void show() const = 0;\n};\n\nclass User\n{\nprivate:\n    Navigator* m_navi;\npublic:\n    User(Navigator* p_navi = nullptr)\n    {\n        m_navi = p_navi;\n    }\n\n    void setNavi(Navigator* p_navi)\n    {\n        delete this-&gt;m_navi;\n        this-&gt;m_navi = p_navi;\n    }\n\n    void showNaviMode()\n    {\n        this-&gt;m_navi-&gt;show();\n    }\n};\n\nclass WalkNavi : public Navigator\n{\npublic:\n    void show() const override\n    {\n        std::cout &lt;&lt; &quot;Walk Mode&quot; &lt;&lt; std::endl;\n    }\n};\n\nclass CycNavi : public Navigator\n{\npublic:\n    void show() const override\n    {\n        std::cout &lt;&lt; &quot;Cycling Mode&quot; &lt;&lt; std::endl;\n    }\n};\n\nclass DriveNavi : public Navigator\n{\npublic:\n    void show() const override\n    {\n        std::cout &lt;&lt; &quot;Drive Mode&quot; &lt;&lt; std::endl;\n    }\n};\n\nvoid Client(User* user)\n{\n    user-&gt;showNaviMode();\n}\n\nint main()\n{\n    User* user = new User(new WalkNavi());\n    Client(user);   \/\/ \u6b65\u884c\u6a21\u5f0f\n    user-&gt;setNavi(new CycNavi());\n    Client(user);   \/\/ \u9a91\u884c\u6a21\u5f0f\n    user-&gt;setNavi(new DriveNavi());\n    Client(user);   \/\/ \u9a7e\u9a76\u6a21\u5f0f\n\n    delete user;\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u610f\u56fe &nbsp;&nbsp;&nbsp;&nbsp;\u5b9a\u4e49\u4e00\u7cfb\u5217\u7684\u7b97\u6cd5\uff0c\u628a\u4ed6\u4eec\u4e00\u4e2a\u4e2a\u5c01\u88c5\u8d77\u6765\uff0c\u5e76\u4e14\u4f7f\u5b83\u4eec\u53ef\u4e92\u76f8\u66ff\u6362\u3002 &#038;nbs &#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\/328"}],"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=328"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/328\/revisions"}],"predecessor-version":[{"id":463,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/328\/revisions\/463"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}