{"id":200553,"date":"2020-09-20T09:59:42","date_gmt":"2020-09-20T01:59:42","guid":{"rendered":"https:\/\/gulass.cn\/?p=200553"},"modified":"2020-09-08T11:01:34","modified_gmt":"2020-09-08T03:01:34","slug":"strategy-pattern","status":"publish","type":"post","link":"https:\/\/gulass.cn\/strategy-pattern.html","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f\u4e4b\u7b56\u7565\u6a21\u5f0f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u7b56\u7565\u6a21\u5f0f\uff08Strategy\uff09\uff1a\u5b83\u5b9a\u4e49\u4e86\u7b97\u6cd5\u5bb6\u65cf\uff0c\u5206\u522b\u5c01\u88c5\u8d77\u6765\uff0c\u8ba9\u5b83\u4eec\u4e4b\u95f4\u53ef\u4ee5\u4e92\u76f8\u66ff\u6362\uff0c\u6b64\u6a21\u5f0f\u8ba9\u7b97\u6cd5\u7684\u53d8\u5316\u4e0d\u4f1a\u5f71\u54cd\u5230\u4f7f\u7528\u7b97\u6cd5\u7684 Context\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u7b56\u7565\u6a21\u5f0f\u662f\u4e00\u79cd\u5b9a\u4e49\u4e00\u7cfb\u5217\u7b97\u6cd5\u7684\u65b9\u6cd5\uff0c\u4ece\u6982\u5ff5\u4e0a\u6765\u770b\uff0c\u6240\u6709\u8fd9\u4e9b\u7b97\u6cd5\u5b8c\u5168\u7684\u90fd\u662f\u76f8\u540c\u7684\u5de5\u4f5c\uff0c\u53ea\u662f\u5b9e\u73b0\u4e0d\u540c\uff0c\u5b83\u53ef\u4ee5\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u8c03\u7528\u6240\u6709\u7684\u7b97\u6cd5\uff0c\u51cf\u5c11\u4e86\u5404\u79cd\u7b97\u6cd5\u7c7b\u4e0e\u4f7f\u7528\u7c7b\u4e4b\u95f4\u7684\u8026\u5408\u3002<\/p>\n

\u4f7f\u7528\u573a\u666f<\/strong><\/div>\n

\u7b56\u7565\u6a21\u5f0f\u5c31\u662f\u7528\u6765\u5c01\u88c5\u7b97\u6cd5\u7684\uff0c\u4f46\u5728\u5b9e\u8df5\u4e2d\u4e5f\u53ef\u4ee5\u7528\u5b83\u6765\u5c01\u88c5\u51e0\u4e4e\u4efb\u4f55\u7c7b\u578b\u7684\u89c4\u5219\uff0c\u53ea\u8981\u5728\u5206\u6790\u8fc7\u7a0b\u4e2d\u542c\u5230\u9700\u8981\u5728\u4e0d\u540c\u65f6\u95f4\u5e94\u7528\u4e0d\u540c\u7684\u4e1a\u52a1\u89c4\u5219\uff0c\u5c31\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u5904\u7406\u8fd9\u79cd\u53d8\u5316\u7684\u53ef\u80fd\u6027\u3002<\/p>\n

\u4f18\u70b9<\/strong><\/div>\n

\u51cf\u5c11\u4e86\u5177\u4f53\u7684\u7b97\u6cd5\u548c\u4f7f\u7528\u7b97\u6cd5\u7c7b\u4e4b\u95f4\u7684\u8026\u5408
\n\u7b56\u7565\u6a21\u5f0f\u7684 Strategy \u7c7b\u5c42\u4e3a Context \u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u7684\u53ef\u4f9b\u91cd\u7528\u7684\u7b97\u6cd5\u6216\u884c\u4e3a\uff0c\u7ee7\u627f\u6709\u52a9\u4e8e\u6790\u53d6\u8fd9\u4e9b\u7b97\u6cd5\u4e2d\u7684\u516c\u5171\u529f\u80fd
\n\u7b80\u5316\u4e86\u5355\u5143\u6d4b\u8bd5\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u7b97\u6cd5\u90fd\u6709\u81ea\u5df1\u7684\u7c7b\uff0c\u53ef\u4ee5\u901a\u8fc7\u81ea\u5df1\u7684\u63a5\u53e3\u5355\u72ec\u6d4b\u8bd5\u3002<\/p>\n

\u4e3e\u4f8b<\/strong><\/div>\n
\r\npublic class Context\r\n{\r\n    private readonly Strategy _strategy;\r\n\r\n    public Context(Strategy strategy) => _strategy = strategy;\r\n\r\n    public void Implement()\r\n    {\r\n        _strategy.AlgorithmImplement();\r\n    }\r\n}\r\n\r\npublic abstract class Strategy\r\n{\r\n    public abstract void AlgorithmImplement();\r\n}\r\n\r\npublic class ConcreteStrategyA : Strategy\r\n{\r\n    public override void AlgorithmImplement()\r\n    {\r\n        Console.WriteLine(\"\u7b97\u6cd5A\u5b9e\u73b0\");\r\n    }\r\n}\r\n\r\npublic class ConcreteStrategyB : Strategy\r\n{\r\n    public override void AlgorithmImplement()\r\n    {\r\n        Console.WriteLine(\"\u7b97\u6cd5B\u5b9e\u73b0\");\r\n    }\r\n}\r\n\r\npublic class ConcreteStrategyC : Strategy\r\n{\r\n    public override void AlgorithmImplement()\r\n    {\r\n        Console.WriteLine(\"\u7b97\u6cd5C\u5b9e\u73b0\");\r\n    }\r\n}\r\n<\/pre>\n
More<\/strong><\/div>\n
\r\n\u5de5\u5382\u6a21\u5f0f\u662f\u89e3\u8026\u5bf9\u8c61\u7684\u521b\u5efa\u548c\u4f7f\u7528\uff0c\u89c2\u5bdf\u8005\u6a21\u5f0f\u662f\u89e3\u8026\u89c2\u5bdf\u8005\u548c\u88ab\u89c2\u5bdf\u8005\u3002\u7b56\u7565\u6a21\u5f0f\u8ddf\u4e24\u8005\u7c7b\u4f3c\uff0c\u4e5f\u80fd\u8d77\u5230\u89e3\u8026\u7684\u4f5c\u7528\uff0c\u4e0d\u8fc7\uff0c\u5b83\u89e3\u8026\u7684\u662f\u7b56\u7565\u7684\u5b9a\u4e49\u3001\u521b\u5efa\u3001\u4f7f\u7528\u8fd9\u4e09\u90e8\u5206\u3002\r\n\r\n\u7b56\u7565\u6a21\u5f0f\u7528\u6765\u89e3\u8026\u7b56\u7565\u7684\u5b9a\u4e49\u3001\u521b\u5efa\u3001\u4f7f\u7528\u3002\u5b9e\u9645\u4e0a\uff0c\u4e00\u4e2a\u5b8c\u6574\u7684\u7b56\u7565\u6a21\u5f0f\u5c31\u662f\u7531\u8fd9\u4e09\u4e2a\u90e8\u5206\u7ec4\u6210\u7684\u3002\r\n\r\n\u7b56\u7565\u7c7b\u7684\u5b9a\u4e49\u6bd4\u8f83\u7b80\u5355\uff0c\u5305\u542b\u4e00\u4e2a\u7b56\u7565\u63a5\u53e3\u548c\u4e00\u7ec4\u5b9e\u73b0\u8fd9\u4e2a\u63a5\u53e3\u7684\u7b56\u7565\u7c7b\u3002\r\n\u7b56\u7565\u7684\u521b\u5efa\u7531\u5de5\u5382\u7c7b\u6765\u5b8c\u6210\uff0c\u5c01\u88c5\u7b56\u7565\u521b\u5efa\u7684\u7ec6\u8282\u3002\r\n\u7b56\u7565\u6a21\u5f0f\u5305\u542b\u4e00\u7ec4\u7b56\u7565\u53ef\u9009\uff0c\u5ba2\u6237\u7aef\u4ee3\u7801\u5982\u4f55\u9009\u62e9\u4f7f\u7528\u54ea\u4e2a\u7b56\u7565\uff0c\u6709\u4e24\u79cd\u786e\u5b9a\u65b9\u6cd5\uff1a\u7f16\u8bd1\u65f6\u9759\u6001\u786e\u5b9a\u548c\u8fd0\u884c\u65f6\u52a8\u6001\u786e\u5b9a\u3002\u5176\u4e2d\uff0c\u201c\u8fd0\u884c\u65f6\u52a8\u6001\u786e\u5b9a\u201d\u624d\u662f\u7b56\u7565\u6a21\u5f0f\u6700\u5178\u578b\u7684\u5e94\u7528\u573a\u666f\r\n\r\n\u5982\u679c if-else \u5206\u652f\u5224\u65ad\u4e0d\u590d\u6742\u3001\u4ee3\u7801\u4e0d\u591a\uff0c\u8fd9\u5e76\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\uff0c\u6bd5\u7adf if-else \u5206\u652f\u5224\u65ad\u51e0\u4e4e\u662f\u6240\u6709\u7f16\u7a0b\u8bed\u8a00\u90fd\u4f1a\u63d0\u4f9b\u7684\u8bed\u6cd5\uff0c\u5b58\u5728\u5373\u6709\u7406\u7531\u3002\u9075\u5faa KISS \u539f\u5219\uff0c\u600e\u4e48\u7b80\u5355\u600e\u4e48\u6765\uff0c\u5c31\u662f\u6700\u597d\u7684\u8bbe\u8ba1\u3002\u975e\u5f97\u7528\u7b56\u7565\u6a21\u5f0f\uff0c\u641e\u51fa n \u591a\u7c7b\uff0c\u53cd\u5012\u662f\u4e00\u79cd\u8fc7\u5ea6\u8bbe\u8ba1\u3002\r\n\r\n\u7b56\u7565\u7684\u521b\u5efa\u4e5f\u53ef\u4ee5\u7ed3\u5408\u4f9d\u8d56\u6ce8\u5165\u6765\u521b\u5efa\u6216\u83b7\u53d6\uff0c\u7075\u6d3b\u4f7f\u7528\u3002<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u7b56\u7565\u6a21\u5f0f\u662f\u4e00\u79cd\u5b9a\u4e49\u4e00\u7cfb\u5217\u7b97\u6cd5\u7684\u65b9\u6cd5\uff0c\u4ece\u6982\u5ff5\u4e0a\u6765\u770b\uff0c\u6240\u6709\u8fd9\u4e9b\u7b97\u6cd5\u5b8c\u5168\u7684\u90fd\u662f\u76f8\u540c\u7684\u5de5\u4f5c\uff0c\u53ea\u662f\u5b9e\u73b0\u4e0d\u540c\uff0c\u5b83\u53ef\u4ee5\u4ee5\u76f8\u540c […]<\/p>\n","protected":false},"author":1903,"featured_media":120934,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-200553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/200553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=200553"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/200553\/revisions"}],"predecessor-version":[{"id":200612,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/200553\/revisions\/200612"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/120934"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=200553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=200553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=200553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}