{"id":200567,"date":"2020-09-22T09:48:34","date_gmt":"2020-09-22T01:48:34","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=200567"},"modified":"2020-09-11T09:49:59","modified_gmt":"2020-09-11T01:49:59","slug":"interpreter-pattern","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/interpreter-pattern.html","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f\u4e4b\u89e3\u91ca\u5668\u6a21\u5f0f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u89e3\u91ca\u5668\u6a21\u5f0f\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u8bed\u8a00\uff0c\u5b9a\u4e49\u5b83\u7684\u6587\u6cd5\u7684\u4e00\u79cd\u8868\u793a\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u89e3\u91ca\u5668\uff0c\u8fd9\u4e2a\u89e3\u91ca\u5668\u4f7f\u7528\u8be5\u8868\u793a\u6765\u89e3\u91ca\u8bed\u8a00\u4e2d\u7684\u53e5\u5b50\u3002\u8fd9\u548c\u89e3\u91ca\u578b\u7f16\u7a0b\u8bed\u8a00\u7684\u89e3\u91ca\u5668\u6709\u70b9\u7c7b\u4f3c\uff0c\u8981\u6839\u636e\u4e00\u6bb5\u8f93\u5165\u8f6c\u6362\u6210\u4e00\u6bb5\u8f93\u51fa\uff0c\u5c06\u4e0d\u6613\u8bfb\u7684\u6587\u672c\u8f6c\u6362\u4e3a\u6613\u8bfb\u7684\u6587\u672c\uff0c\u5c06\u673a\u5668\u4e0d\u80fd\u8bc6\u522b\u7684\u8f93\u5165\u8f6c\u6210\u4e8c\u8fdb\u5236\u673a\u5668\u53ef\u8bfb\u7684\u8f93\u51fa\u3002\u5f53\u6709\u4e00\u4e2a\u8bed\u8a00\u9700\u8981\u89e3\u91ca\u6267\u884c\uff0c\u5e76\u4e14\u4f60\u53ef\u4ee5\u5c06\u8be5\u8bed\u8a00\u4e2d\u7684\u53e5\u5b50\u8868\u793a\u4e3a\u4e00\u4e2a\u62bd\u8c61\u8bed\u6cd5\u6811\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u89e3\u91ca\u5668\u6a21\u5f0f\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
Prototype<\/strong><\/div>\n

Context: \u89e3\u91ca\u5668\u4e0a\u4e0b\u6587
\nAbstractExpression: \u89e3\u91ca\u8868\u8fbe\u5f0f\u62bd\u8c61\uff0c\u5b9a\u4e49\u89e3\u91ca\u64cd\u4f5c
\nConcreteExpression: \u89e3\u91ca\u8868\u8fbe\u5f0f\u5b9e\u73b0\u7c7b\uff0c\u5b9e\u73b0\u5177\u4f53\u7684\u89e3\u91ca\u903b\u8f91<\/p>\n

Sample<\/strong><\/div>\n
\r\npublic class Context\r\n{\r\n    public string Input { get; set; }\r\n    public string Output { get; set; }\r\n}\r\n\r\npublic abstract class AbstractExpression\r\n{\r\n    public abstract void Interpret(Context context);\r\n}\r\npublic class TerminalExpression : AbstractExpression\r\n{\r\n    public override void Interpret(Context context)\r\n    {\r\n        Console.WriteLine(\"TerminalExpressionInterpreter\");\r\n    }\r\n}\r\npublic class NoneTerminalExpression : AbstractExpression\r\n{\r\n    public override void Interpret(Context context)\r\n    {\r\n        Console.WriteLine(\"NonTerminalExpressionInterpreter\");\r\n    }\r\n}\r\n\r\n\r\nvar context = new Context();\r\nICollection expressions = new List<\/abstractexpression>();\r\n\r\nexpressions.Add(new TerminalExpression());\r\nexpressions.Add(new TerminalExpression());\r\nexpressions.Add(new NoneTerminalExpression());\r\n\r\nforeach (var expression in expressions)\r\n{\r\n    expression.Interpret(context);\r\n}\r\n<\/abstractexpression><\/pre>\n
More<\/strong><\/div>\n

\u89e3\u91ca\u5668\u6a21\u5f0f\u4e3a\u67d0\u4e2a\u8bed\u8a00\u5b9a\u4e49\u5b83\u7684\u8bed\u6cd5\uff08\u6216\u8005\u53eb\u6587\u6cd5\uff09\u8868\u793a\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u89e3\u91ca\u5668\u7528\u6765\u5904\u7406\u8fd9\u4e2a\u8bed\u6cd5\u3002<\/p>\n

\u8981\u60f3\u4e86\u89e3\u201c\u8bed\u8a00\u201d\u8981\u8868\u8fbe\u7684\u4fe1\u606f\uff0c\u6211\u4eec\u5c31\u5fc5\u987b\u5b9a\u4e49\u76f8\u5e94\u7684\u8bed\u6cd5\u89c4\u5219\u3002\u8fd9\u6837\uff0c\u4e66\u5199\u8005\u5c31\u53ef\u4ee5\u6839\u636e\u8bed\u6cd5\u89c4\u5219\u6765\u4e66\u5199\u201c\u53e5\u5b50\u201d\uff08\u4e13\u4e1a\u70b9\u7684\u53eb\u6cd5\u5e94\u8be5\u662f\u201c\u8868\u8fbe\u5f0f\u201d\uff09\uff0c\u9605\u8bfb\u8005\u6839\u636e\u8bed\u6cd5\u89c4\u5219\u6765\u9605\u8bfb\u201c\u53e5\u5b50\u201d\uff0c\u8fd9\u6837\u624d\u80fd\u505a\u5230\u4fe1\u606f\u7684\u6b63\u786e\u4f20\u9012\u3002\u800c\u6211\u4eec\u8981\u8bb2\u7684\u89e3\u91ca\u5668\u6a21\u5f0f\uff0c\u5176\u5b9e\u5c31\u662f\u7528\u6765\u5b9e\u73b0\u6839\u636e\u8bed\u6cd5\u89c4\u5219\u89e3\u8bfb\u201c\u53e5\u5b50\u201d\u7684\u89e3\u91ca\u5668\u3002<\/p>\n

\u89e3\u91ca\u5668\u6a21\u5f0f\u7684\u4ee3\u7801\u5b9e\u73b0\u6bd4\u8f83\u7075\u6d3b\uff0c\u6ca1\u6709\u56fa\u5b9a\u7684\u6a21\u677f\u3002<\/p>\n

\u5e94\u7528\u8bbe\u8ba1\u6a21\u5f0f\u4e3b\u8981\u662f\u5e94\u5bf9\u4ee3\u7801\u7684\u590d\u6742\u6027\uff0c\u89e3\u91ca\u5668\u6a21\u5f0f\u4e5f\u4e0d\u4f8b\u5916\u3002\u5b83\u7684\u4ee3\u7801\u5b9e\u73b0\u7684\u6838\u5fc3\u601d\u60f3\uff0c\u5c31\u662f\u5c06\u8bed\u6cd5\u89e3\u6790\u7684\u5de5\u4f5c\u62c6\u5206\u5230\u5404\u4e2a\u5c0f\u7c7b\u4e2d\uff0c\u4ee5\u6b64\u6765\u907f\u514d\u5927\u800c\u5168\u7684\u89e3\u6790\u7c7b\u3002<\/p>\n

\u4e00\u822c\u7684\u505a\u6cd5\u662f\uff0c\u5c06\u8bed\u6cd5\u89c4\u5219\u62c6\u5206\u4e00\u4e9b\u5c0f\u7684\u72ec\u7acb\u7684\u5355\u5143\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a\u5355\u5143\u8fdb\u884c\u89e3\u6790\uff0c\u6700\u7ec8\u5408\u5e76\u4e3a\u5bf9\u6574\u4e2a\u8bed\u6cd5\u89c4\u5219\u7684\u89e3\u6790\u3002<\/p>\n

\u89e3\u91ca\u5668\u6a21\u5f0f\u53ef\u80fd\u5c0f\u4f17\uff0c\u53ea\u5728\u4e00\u4e9b\u7279\u5b9a\u7684\u9886\u57df\u6216\u60c5\u5883\u4e0b\u4f1a\u88ab\u7528\u5230\uff0c\u6bd4\u5982\u7f16\u8bd1\u5668\u3001\u89c4\u5219\u5f15\u64ce\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Context: \u89e3\u91ca\u5668\u4e0a\u4e0b\u6587 AbstractExpression: \u89e3\u91ca\u8868\u8fbe\u5f0f\u62bd\u8c61\uff0c\u5b9a\u4e49\u89e3\u91ca\u64cd\u4f5c Conc […]<\/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-200567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/200567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=200567"}],"version-history":[{"count":6,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/200567\/revisions"}],"predecessor-version":[{"id":200699,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/200567\/revisions\/200699"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/120934"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=200567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=200567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=200567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}