{"id":1460,"date":"2022-08-01T23:45:48","date_gmt":"2022-08-01T15:45:48","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1460"},"modified":"2022-08-08T23:33:55","modified_gmt":"2022-08-08T15:33:55","slug":"she-ji-mo-shi-javascript-zhong-de-ce-lue-mo-shi","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1460","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f &#8211; JavaScript\u4e2d\u7684\u7b56\u7565\u6a21\u5f0f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/\u7b56\u7565\u6a21\u5f0f.jpeg\" alt=\"\u7b56\u7565\u6a21\u5f0f\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/carlillo\/design-patterns---strategy-pattern-in-javascript-2hg3\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/carlillo\/design-patterns&#8212;strategy-pattern-in-javascript-2hg3<\/a><\/p>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5c06\u4f1a\u8bb2\u89e3 <strong>Strategy Pattern<\/strong> \u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4ee5\u53ca\u4ec0\u4e48\u65f6\u5019\u5e94\u8be5\u4f7f\u7528\u5b83\uff0c\u5728\u5176\u4ed6\u6587\u7ae0\u4e2d\u53eb\u505a <strong>Policy<\/strong>\u3002<\/p>\n<h2><a id=\"%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F%EF%BC%9A%E5%9F%BA%E7%A1%80%E6%A6%82%E5%BF%B5\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b56\u7565\u6a21\u5f0f\uff1a\u57fa\u7840\u6982\u5ff5<\/h2>\n<blockquote>\n<p>\u7b56\u7565\u6a21\u5f0f\u662f\u4e00\u79cd\u884c\u4e3a\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u9009\u62e9\u3002 &#8211;Wikipedia<\/p>\n<\/blockquote>\n<blockquote>\n<p>\u5b9a\u4e49\u4e00\u7cfb\u5217\u7c7b\u578b\u3001\u72ec\u7acb\u5c01\u88c5\u4e14\u53ef\u4e92\u76f8\u66ff\u6362\u7684\u7b97\u6cd5\uff0c\u7b56\u7565\u6a21\u5f0f\u4f7f\u7b97\u6cd5\u4e0d\u4f9d\u8d56\u4f7f\u7528\u5b83\u7684\u5ba2\u6237\u3002 &#8212; Design Patterns: Elements of Reusable Object-Oriented Software<\/p>\n<\/blockquote>\n<p>\u8fd9\u79cd\u6a21\u5f0f\u7684\u4e3b\u8981\u7279\u70b9\u5c31\u662f\u8c03\u7528\u65b9\u6709\u4e00\u7cfb\u5217\u7b97\u6cd5\uff0c\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u4f1a\u9009\u62e9\u4e00\u4e2a\u7279\u5b9a\u7684\u3002\u8fd9\u4e9b\u7b97\u6cd5\u90fd\u662f\u53ef\u4ee5\u4e92\u76f8\u66ff\u6362\u7684\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u4e00\u4e2a\u5178\u578b\u7684\u95ee\u9898\uff1a\u5728\u5e94\u7528\u4e2d\uff0c\u4f60\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u5177\u4f53\u7b97\u6cd5\u3002\u5728\u4ee3\u7801\u4e2d\uff0c\u4f60\u4f7f\u7528 <code>switch<\/code> \u6765\u63a7\u5236\u7f16\u7a0b\u9700\u8981\u7684\u7ed3\u6784\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/16593701349916.jpg\" alt=\"\" \/><\/p>\n<p>\u53ef\u662f\uff0c\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\uff0c\u5b83\u53ef\u4ee5\u53d8\u5f97\u66f4\u52a0\u7075\u6d3b\u3002\u5c31\u50cf\u4e0b\u9762\u7684\u7ed3\u6784\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/16593702207546.jpg\" alt=\"\" \/><\/p>\n<p>\u7b56\u7565\u6a21\u5f0f\u7684 UML \u56fe\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/16593702491526.jpg\" alt=\"\" \/><\/p>\n<p>\u6bcf\u4e2a\u7b56\u7565\u662f\u4f7f\u7528\u4e00\u4e2a\u5177\u4f53\u7684\u5bf9\u8c61\u8868\u793a\u3002\u6240\u4ee5\uff0c\u5ba2\u6237\u7aef\/\u4e0a\u4e0b\u6587\u5305\u542b\u4e00\u4e2a\u5b9e\u73b0\u4e86 <code>Strategy<\/code> \u63a5\u53e3\u7684 <code>Strategy<\/code> \u5bf9\u8c61\u3002\u5207\u6362\u7b56\u7565\u7684\u5173\u952e\u5b58\u5728\u4e8e\u4e0a\u4e0b\u6587\u4e2d\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5207\u6362\u7b56\u7565\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<code>setStrategy<\/code>\u3002<\/p>\n<h2><a id=\"%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F%EF%BC%9A%E4%BD%95%E6%97%B6%E4%BD%BF%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b56\u7565\u6a21\u5f0f\uff1a\u4f55\u65f6\u4f7f\u7528<\/h2>\n<ol>\n<li>\u89e3\u51b3\u7b56\u7565\u6a21\u5f0f\u7684\u95ee\u9898\u662f\uff0c\u5f53\u4f60\u9700\u8981\u4f7f\u7528\u51e0\u79cd\u6709\u4e0d\u540c\u53d8\u5316\u7684\u7b97\u6cd5\u65f6\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5177\u4f53\u7684\u7c7b\u6765\u5b9e\u73b0\u4f60\u7684\u7b97\u6cd5\uff08\u5b83\u53ef\u4ee5\u7531\u4e00\u4e2a\u6216\u51e0\u4e2a\u51fd\u6570\u7ec4\u6210\uff09\u3002<\/li>\n<li>\u53e6\u4e00\u4e2a\u4f60\u53d1\u73b0\u9700\u8981\u7b56\u7565\u6a21\u5f0f\u7684\u6709\u8da3\u65f6\u523b\u662f\u5f53\u6709\u6761\u4ef6\u8bed\u53e5\u7a7f\u63d2\u5728\u7b97\u6cd5\u5468\u56f4\u4e14\u5b83\u4eec\u4e4b\u95f4\u8fd8\u6709\u8054\u7cfb\u65f6\u3002<\/li>\n<li>\u6700\u540e\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528\u8be5\u6a21\u5f0f\u65f6\u662f\u4f60\u7684\u591a\u6570\u7c7b\u6709\u76f8\u5173\u7684\u884c\u4e3a\u3002<\/li>\n<\/ol>\n<h2><a id=\"%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F%EF%BC%9A%E4%BC%98%E7%82%B9\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b56\u7565\u6a21\u5f0f\uff1a\u4f18\u70b9<\/h2>\n<p>\u7b56\u7565\u6a21\u5f0f\u6709\u8bb8\u591a\u4f18\u70b9\uff0c\u53ef\u4ee5\u5f52\u7eb3\u4e3a\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ul>\n<li>\u8fd0\u884c\u65f6\u56e0\u4e3a\u4f7f\u7528\u4e86\u63a5\u53e3\u7684\u591a\u6001\uff0c\u6240\u4ee5\u5f88\u5bb9\u6613\u5207\u6362\u4e0d\u540c\u7684\u7b97\u6cd5\u3002<\/li>\n<li>\u6574\u6d01\u7684\u4ee3\u7801\uff0c\u56e0\u4e3a\u4f60\u907f\u514d\u4e86\u6761\u4ef6\u8bed\u53e5\u4ee3\u7801\uff08\u4e0d\u590d\u6742\uff09\u3002<\/li>\n<li>\u56e0\u4e3a\u4f60\u628a\u5173\u6ce8\u70b9\u5206\u79bb\u5230\u7c7b\u4e2d\uff08\u6bcf\u4e2a\u7b56\u7565\u7684\u7c7b\uff09\u4f7f\u5f97\u4ee3\u7801\u66f4\u6574\u6d01\u3002<\/li>\n<\/ul>\n<h2><a id=\"%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F%EF%BC%9A%E7%94%A8javascript%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b56\u7565\u6a21\u5f0f\uff1a\u7528 JavaScript \u7b80\u5355\u5b9e\u73b0<\/h2>\n<p>\u73b0\u5728\uff0c\u6211\u5c06\u7528 JavaScript \u7ed9\u4f60\u6f14\u793a\u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u7b56\u7565\u6a21\u5f0f\uff0c\u4f60\u80af\u5b9a\u77e5\u9053 JavaScript \u7f3a\u4e4f\u63a5\u53e3\u3002\u6240\u4ee5\uff0c\u4f60\u9700\u8981\u7f16\u5199\u4e00\u4e2a <code>StrategyManager<\/code> \u7c7b\u7528\u4f5c\u63a5\u53e3\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/16599703678712.jpg\" alt=\"\" \/><\/p>\n<p>\u8fd9\u4e2a\u7c7b\u5305\u542b\u79c1\u6709\u7684\u5c5e\u6027 <code>_strategy<\/code> \uff0c\u7528\u5b83\u4ee3\u8868\u5c06\u88ab\u4f7f\u7528\u7684\u7b56\u7565\u3002<code>doAction<\/code> \u65b9\u6cd5\u5c06\u4f1a\u88ab\u6bcf\u4e2a\u5177\u4f53\u7684\u7b56\u7565\u5b9e\u73b0\u3002JavaScript \u4e2d\u7684\u7b56\u7565\u6a21\u5f0f\u4e0e UML \u56fe\u4e0d\u540c\u662f\u56e0\u4e3a\u7f3a\u4e4f\u8bed\u8a00\u7ea7\u522b\u7684 OOP \u7279\u6027\u3002<\/p>\n<p>\u6bcf\u4e2a\u5177\u4f53\u7684\u7b56\u7565\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/16599707031855.jpg\" alt=\"\" \/><\/p>\n<p>\u6ce8\u610f\u5177\u4f53\u7684 <code>doAction<\/code> \u65b9\u6cd5\u662f\u5728\u6bcf\u4e2a\u5177\u4f53\u7684\u7b56\u7565\u4e2d\u5b9e\u73b0\u7684\u3002<\/p>\n<p>\u6700\u540e\uff0c\u7528\u6237\u7aef\/\u4e0a\u4e0b\u6587\u5fc5\u987b\u5305\u542b <code>StrategyManager<\/code> \u6765\u4f7f\u7528\u5177\u4f53\u7684\u7b56\u7565\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/16599708948278.jpg\" alt=\"\" \/><\/p>\n<p>\u7b56\u7565\u6a21\u5f0f\uff1a\u7528 JavaScript \u5b9e\u73b0\u7684\u591a\u4e2a\u7b56\u7565<\/p>\n<p>\u5728\u63a5\u4e0b\u6765\u7684\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u7684 <code>StrategyManager<\/code> \u4f1a\u66f4\u590d\u6742\u4ee5\u53ca\u5305\u542b\u4e00\u7cfb\u5217\u7b97\u6cd5\u3002\u5728\u8fd9\u79cd\u573a\u666f\u4e0b\uff0c\u4f60\u53ef\u4ee5\u7528\u6570\u7ec4 <code>_strategies<\/code> \u6765\u4ee3\u66ff <code>_strategy<\/code> \u5c5e\u6027\u3002<\/p>\n<p>\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>addStrategy<\/code> \u60f3\u5217\u8868\u6dfb\u52a0\u65b0\u7b56\u7565\u3002\u4e00\u4e2a\u7b56\u7565\u7c7b\u5305\u542b 2 \u4e2a\u5c5e\u6027\uff1a1) \u7b56\u7565\u540d\u5b57\uff1b2)  \u7b97\u6cd5\uff08\u53c8\u53eb \u5904\u7406\u5668\uff09\u3002<code>doAction<\/code> \u662f\u7528\u6765\u8c03\u7528\u5177\u4f53\u7b56\u7565\u7b97\u6cd5\u7684\u65b9\u6cd5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/16599712578594.jpg\" alt=\"\" \/><\/p>\n<p>\u6700\u540e\uff0c\u7528\u6237\u7aef\/\u4e0a\u4e0b\u6587\u7684\u4ee3\u7801\u4f7f\u7528\u5177\u4f53\u7b56\u7565\u7684\u4f4d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/08\/16599713816635.jpg\" alt=\"\" \/><\/p>\n<p>\u4ee3\u7801\u7684\u7b2c\u4e00\u90e8\u5206\u662f\u6765\u521b\u5efa\u5177\u4f53\u7684\u7b56\u7565\uff08\u53ef\u4ee5\u901a\u8fc7 <code>Singleton<\/code> \u6a21\u5f0f\u548c <code>Factory<\/code> \u6a21\u5f0f\uff09\uff0c\u63a5\u7740\u88ab\u6dfb\u52a0\u5230\u6211\u4eec\u7684 <code>strategyManager<\/code> \u4e2d\u3002\u63a5\u4e0b\u6765\u662f\u4e0a\u4e0b\u6587\u9009\u62e9\u7b56\u7565\u6765\u4f7f\u7528\uff0c\u5728\u6211\u4eec\u5e94\u7528\u4e2d\u7b56\u7565\u53ef\u4ee5\u901a\u8fc7 <code>GUI<\/code> \u6216 <code>CLI<\/code> \u6765\u9009\u62e9\u3002<\/p>\n<p>\u6700\u540e\uff0c\u4f60\u4f1a\u610f\u8bc6\u5230\u82e5\u9009\u62e9\u4e86\u4e00\u4e2a\u6ca1\u6709\u652f\u6301\u7684\u7b56\u7565\u7cfb\u7edf\u5c06\u4f1a\u62a5\u9519\u3002\u5f53\u4f60\u60f3\u4e3a\u4f60\u7684\u7cfb\u7edf\u63d0\u4f9b\u4e00\u4e2a\u9ad8\u7ea7\u7b97\u6cd5\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/p>\n<h2><a id=\"%E6%80%BB%E7%BB%93\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u603b\u7ed3<\/h2>\n<p>\u7b56\u7565\u6a21\u5f0f\u662f\u4e00\u79cd\u5f53\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u5177\u4f53\u7b97\u6cd5\u65f6\uff0c\u53ef\u4ee5\u4f7f\u4f60\u4ee3\u7801\u907f\u514d\u590d\u6742\u7684\u6a21\u5f0f\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4f60\u77e5\u9053\u4e86\u901a\u8fc7\u7f3a\u4e4f\u63a5\u53e3\u7684 JavaScript \u5b9e\u73b0\u7684\u7b56\u7565\u6a21\u5f0f\u3002\u5728\u4f60\u4f7f\u7528\u7684\u8bed\u8a00\u652f\u6301\u63a5\u53e3\u7684\u573a\u666f\u4e0b\uff0c\u4f60\u53ef\u4ee5\u8ddf\u968f UML \u56fe\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u6700\u91cd\u8981\u7684\u4e0d\u662f\u6211\u6240\u5b9e\u73b0\u7684\u7b56\u7565\u6a21\u5f0f\u65b9\u6cd5\uff0c\u800c\u662f\u4f60\u9700\u8981\u77e5\u9053\u8fd9\u4e2a\u6a21\u5f0f\u89e3\u51b3\u7684\u662f\u4ec0\u4e48\u95ee\u9898\uff0c\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u4f60\u5fc5\u987b\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5f0f\u3002\u56e0\u4e3a\u7f16\u7a0b\u8bed\u8a00\u4e0d\u540c\uff0c\u5b9e\u73b0\u4e5f\u5c31\u4e0d\u540c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/carlillo\/design-patterns&#038;#8212&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[205,124,123],"class_list":["post-1460","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-design-patterns","tag-124","tag-123"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1460"}],"version-history":[{"count":2,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1460\/revisions"}],"predecessor-version":[{"id":1463,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1460\/revisions\/1463"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}