{"id":4360,"date":"2025-08-28T10:41:21","date_gmt":"2025-08-28T02:41:21","guid":{"rendered":"https:\/\/www.caiqinyi.cn\/?p=4360"},"modified":"2025-08-28T10:41:21","modified_gmt":"2025-08-28T02:41:21","slug":"ue5_design_pattern_interpreter_pattern","status":"publish","type":"post","link":"https:\/\/www.caiqinyi.cn\/index.php\/2025\/08\/28\/ue5_design_pattern_interpreter_pattern\/","title":{"rendered":"[UE5 \u8bbe\u8ba1\u6a21\u5f0f] \u89e3\u91ca\u5668\u6a21\u5f0fInterpreter Pattern"},"content":{"rendered":"<p><script type=\"text\/javascript\" async src=\"https:\/\/www.caiqinyi.cn\/wp-content\/MathJax\/MathJax.js?config=TeX-AMS_CHTML\">\n<\/script><br \/>\n<script type=\"text\/x-mathjax-config\">\n    MathJax.Hub.Config({\n        tex2jax: {inlineMath: [['$','$']]},\n        TeX: {equationNumbers: {autoNumber: [\"AMS\"], useLabelIds: true}},\n        \"HTML-CSS\": {linebreaks: {automatic: true}},\n        SVG: {linebreaks: {automatic: true}}\n    });\n<\/script><\/p>\n<p><strong>\u53c2\u8003\u6750\u6599<\/strong><br \/>\n1. <a href=\"https:\/\/www.cnblogs.com\/shiroe\/p\/14909707.html\">\u3010UE4 \u8bbe\u8ba1\u6a21\u5f0f\u3011\u7b56\u7565\u6a21\u5f0f Strategy Pattern<\/a><br \/>\n2. <a href=\"https:\/\/www.runoob.com\/design-pattern\/interpreter-pattern.html\">\u89e3\u91ca\u5668\u6a21\u5f0f<\/a><br \/>\n3. <a href=\"https:\/\/gpp.tkchu.me\/bytecode.html\">\u5b57\u8282\u7801<\/a><\/p>\n<p><strong>1. \u6982\u8ff0<\/strong><\/p>\n<p><strong>1.1 \u63cf\u8ff0<\/strong><\/p>\n<p>$\\cdot$ \u89e3\u91ca\u5668\u6a21\u5f0f(Interpreter Pattern) \u63d0\u4f9b\u4e86\u8bc4\u4f30\u8bed\u8a00\u7684\u8bed\u6cd5\u6216\u8868\u8fbe\u5f0f\u7684\u65b9\u5f0f, \u5b83\u5c5e\u4e8e\u884c\u4e3a\u578b\u6a21\u5f0f.<br \/>\n$\\\\$ \u89e3\u91ca\u5668\u6a21\u5f0f\u7ed9\u5b9a\u4e00\u4e2a\u8bed\u8a00, \u5b9a\u4e49\u5b83\u7684\u6587\u6cd5\u7684\u4e00\u79cd\u8868\u793a, \u5e76\u5b9a\u4e49\u4e00\u4e2a\u89e3\u91ca\u5668, \u8fd9\u4e2a\u89e3\u91ca\u5668\u4f7f\u7528\u8be5\u8868\u793a\u6765\u89e3\u91ca\u8bed\u8a00\u4e2d\u7684\u53e5\u5b50.<br \/>\n$\\\\$ \u8fd9\u79cd\u6a21\u5f0f\u88ab\u7528\u5728SQL\u89e3\u6790, \u7b26\u53f7\u5904\u7406\u5f15\u64ce\u7b49.<\/p>\n<p><!--more--><\/p>\n<p><strong>1.2 \u5957\u8def<\/strong><\/p>\n<p>$\\cdot$ <strong>\u5b9e\u73b0\u65b9\u5f0f:<\/strong><br \/>\n$\\\\$ 1) \u5b9a\u4e49\u6587\u6cd5: \u660e\u786e\u8bed\u8a00\u7684\u7ec8\u7ed3\u7b26\u548c\u975e\u7ec8\u7ed3\u7b26.<br \/>\n$\\\\$ 2) \u6784\u5efa\u8bed\u6cd5\u6811: \u6839\u636e\u8bed\u8a00\u7684\u53e5\u5b50\u6784\u5efa\u5bf9\u5e94\u7684\u8bed\u6cd5\u6811\u7ed3\u6784.<br \/>\n$\\\\$ 3) \u521b\u5efa\u73af\u5883\u7c7b: \u5305\u542b\u89e3\u91ca\u8fc7\u7a0b\u4e2d\u6240\u9700\u7684\u5168\u5c40\u4fe1\u606f, \u901a\u5e38\u662f\u4e00\u4e2aHashMap.<\/p>\n<p>$\\cdot$ <strong>\u5173\u952e\u4ee3\u7801:<\/strong><br \/>\n$\\\\$ 1) \u7ec8\u7ed3\u7b26\u4e0e\u975e\u7ec8\u7ed3\u7b26: \u5b9a\u4e49\u8bed\u8a00\u7684\u6587\u6cd5\u7ed3\u6784.<br \/>\n$\\\\$ 2) \u73af\u5883\u7c7b: \u5b58\u50a8\u89e3\u91ca\u8fc7\u7a0b\u4e2d\u9700\u8981\u7684\u5916\u90e8\u73af\u5883\u4fe1\u606f.<\/p>\n<p>$\\cdot$ <strong>\u7ed3\u6784:<\/strong><br \/>\n$\\\\$ \u89e3\u91ca\u5668\u6a21\u5f0f\u5305\u542b\u4ee5\u4e0b\u51e0\u4e2a\u4e3b\u8981\u89d2\u8272:<br \/>\n$\\\\$ 1) \u62bd\u8c61\u8868\u8fbe\u5f0f(Abstract Expression): \u5b9a\u4e49\u4e86\u89e3\u91ca\u5668\u7684\u62bd\u8c61\u63a5\u53e3, \u58f0\u660e\u4e86\u89e3\u91ca\u64cd\u4f5c\u7684\u65b9\u6cd5, \u901a\u5e38\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b\u6216\u63a5\u53e3.<br \/>\n$\\\\$ 2) \u7ec8\u7ed3\u7b26\u8868\u8fbe\u5f0f(Terminal Expression): \u5b9e\u73b0\u4e86\u62bd\u8c61\u8868\u8fbe\u5f0f\u63a5\u53e3\u7684\u7ec8\u7ed3\u7b26\u8868\u8fbe\u5f0f\u7c7b, \u7528\u4e8e\u8868\u793a\u8bed\u8a00\u4e2d\u7684\u7ec8\u7ed3\u7b26(\u5982\u53d8\u91cf, \u5e38\u91cf\u7b49), \u5e76\u5b9e\u73b0\u4e86\u5bf9\u5e94\u7684\u89e3\u91ca\u64cd\u4f5c.<br \/>\n$\\\\$ 3) \u975e\u7ec8\u7ed3\u7b26\u8868\u8fbe\u5f0f(Non-terminal Expression): \u5b9e\u73b0\u4e86\u62bd\u8c61\u8868\u8fbe\u5f0f\u63a5\u53e3\u7684\u975e\u7ec8\u7ed3\u7b26\u8868\u8fbe\u5f0f\u7c7b, \u7528\u4e8e\u8868\u793a\u8bed\u8a00\u4e2d\u7684\u975e\u7ec8\u7ed3\u7b26(\u5982\u53e5\u5b50, \u8868\u8fbe\u5f0f\u7b49), \u5e76\u5b9e\u73b0\u4e86\u5bf9\u5e94\u7684\u89e3\u91ca\u64cd\u4f5c.<br \/>\n$\\\\$ 4) \u4e0a\u4e0b\u6587(Context): \u5305\u542b\u89e3\u91ca\u5668\u4e4b\u5916\u7684\u4e00\u4e9b\u5168\u5c40\u4fe1\u606f, \u5728\u89e3\u91ca\u8fc7\u7a0b\u4e2d\u63d0\u4f9b\u7ed9\u89e3\u91ca\u5668\u4f7f\u7528, \u901a\u5e38\u7528\u4e8e\u5b58\u50a8\u53d8\u91cf\u7684\u503c, \u4fdd\u5b58\u89e3\u91ca\u5668\u7684\u72b6\u6001\u7b49.<br \/>\n$\\\\$ 5) \u5ba2\u6237\u7aef(Client): \u521b\u5efa\u5e76\u914d\u7f6e\u5177\u4f53\u7684\u89e3\u91ca\u5668\u5bf9\u8c61, \u5e76\u5c06\u9700\u8981\u89e3\u91ca\u7684\u8868\u8fbe\u5f0f\u4f20\u9012\u7ed9\u89e3\u91ca\u5668\u8fdb\u884c\u89e3\u91ca.<\/p>\n<p><strong>1.3 \u4f7f\u7528\u573a\u666f<\/strong><\/p>\n<p>$\\cdot$ \u5f53\u67d0\u4e00\u7279\u5b9a\u7c7b\u578b\u7684\u95ee\u9898\u9891\u7e41\u51fa\u73b0, \u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u4e00\u79cd\u7b80\u5355\u7684\u8bed\u8a00\u6765\u8868\u8fbe\u8fd9\u4e9b\u95ee\u9898\u7684\u5b9e\u4f8b\u65f6.<\/p>\n<p><strong>1.4 \u4f18\u7f3a\u70b9<\/strong><\/p>\n<p>$\\cdot$ <strong>\u4f18\u70b9:<\/strong><br \/>\n$\\\\$ 1) \u53ef\u6269\u5c55\u6027\u597d: \u5bb9\u6613\u6dfb\u52a0\u65b0\u7684\u89e3\u91ca\u8868\u8fbe\u5f0f\u7684\u65b9\u5f0f.<br \/>\n$\\\\$ 2) \u7075\u6d3b\u6027: \u53ef\u4ee5\u6839\u636e\u9700\u8981\u8f7b\u677e\u6269\u5c55\u6216\u4fee\u6539\u6587\u6cd5.<br \/>\n$\\\\$ 3) \u6613\u4e8e\u5b9e\u73b0\u7b80\u5355\u6587\u6cd5: \u5bf9\u4e8e\u7b80\u5355\u7684\u8bed\u8a00, \u5b9e\u73b0\u8d77\u6765\u76f8\u5bf9\u5bb9\u6613.<\/p>\n<p>$\\cdot$ <strong>\u7f3a\u70b9:<\/strong><br \/>\n$\\\\$ 1) \u4f7f\u7528\u573a\u666f\u6709\u9650: \u53ea\u9002\u7528\u4e8e\u9002\u5408\u4f7f\u7528\u89e3\u91ca\u7684\u7b80\u5355\u6587\u6cd5.<br \/>\n$\\\\$ 2) \u7ef4\u62a4\u56f0\u96be: \u5bf9\u4e8e\u590d\u6742\u7684\u6587\u6cd5, \u7ef4\u62a4\u548c\u6269\u5c55\u53d8\u5f97\u56f0\u96be.<br \/>\n$\\\\$ 3) \u7c7b\u81a8\u80c0: \u53ef\u80fd\u4f1a\u4ea7\u751f\u5f88\u591a\u7c7b, \u6bcf\u4e2a\u6587\u6cd5\u89c4\u5219\u5bf9\u5e94\u4e00\u4e2a\u7c7b.<br \/>\n$\\\\$ 4) \u9012\u5f52\u8c03\u7528: \u89e3\u91ca\u5668\u6a21\u5f0f\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u8c03\u7528, \u8fd9\u53ef\u80fd\u96be\u4ee5\u7406\u89e3\u548c\u8ddf\u8e2a.<\/p>\n<p><strong>2. UE5\u5b9e\u8df5<\/strong><\/p>\n<p>\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u63a5\u53e3FExpression\u548c\u5b9e\u73b0\u4e86FExpression\u63a5\u53e3\u7684\u5b9e\u4f53\u7c7b. \u5b9a\u4e49\u4f5c\u4e3a\u4e0a\u4e0b\u6587\u4e2d\u4e3b\u8981\u89e3\u91ca\u5668\u7684FTerminalExpression\u7c7b. \u5176\u4ed6\u7684\u7c7bFOrExpression, FAndExpression\u7528\u4e8e\u521b\u5efa\u7ec4\u5408\u5f0f\u8868\u8fbe\u5f0f, \u6700\u540e\u6211\u4eec\u6f14\u793a\u4f7f\u7528FExpression\u7c7b\u521b\u5efa\u89c4\u5219\u548c\u6f14\u793a\u8868\u8fbe\u5f0f\u7684\u89e3\u6790.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.caiqinyi.cn\/wp-content\/uploads\/2025\/08\/interpreter_pattern_uml_diagram.jpg\" alt=\"\" width=\"536\" height=\"488\" class=\"aligncenter size-full wp-image-4362\" srcset=\"https:\/\/www.caiqinyi.cn\/wp-content\/uploads\/2025\/08\/interpreter_pattern_uml_diagram.jpg 536w, https:\/\/www.caiqinyi.cn\/wp-content\/uploads\/2025\/08\/interpreter_pattern_uml_diagram-300x273.jpg 300w\" sizes=\"(max-width: 536px) 100vw, 536px\" \/><\/p>\n<p><strong>2.1 \u6b65\u9aa41<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u8868\u8fbe\u5f0f\u63a5\u53e3.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n\r\n#pragma once\r\n\r\n#include \"CoreMinimal.h\"\r\n\r\n\r\nclass FExpression \r\n{\r\npublic:\r\n\tvirtual bool Interpret(const FString& Context)\r\n\t{\r\n\t\treturn false;\r\n\t}\r\n};\r\n\r\n<\/pre>\n<p><strong>2.2 \u6b65\u9aa42<\/strong><\/p>\n<p>\u521b\u5efa\u5b9e\u73b0\u4e86\u4e0a\u8ff0\u63a5\u53e3\u7684\u5b9e\u4f53\u7c7b.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n\r\n#pragma once\r\n\r\n#include \"Expression.h\"\r\n\r\n\r\nclass FTerminalExpression final : public FExpression\r\n{\r\npublic:\r\n\tFTerminalExpression(const FString& InData) : Data(InData)\r\n\t{\r\n\t}\r\n\r\n\tvirtual bool Interpret(const FString& Context) override\r\n\t{\r\n\t\treturn Context.Contains(Data);\r\n\t}\r\n\r\nprivate:\r\n\tFString Data;\r\n};\r\n\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n\r\n#pragma once\r\n\r\n#include \"Expression.h\"\r\n\r\n\r\nclass FOrExpression final : public FExpression\r\n{\r\npublic:\r\n\tFOrExpression(const FExpression& InExpression1, const FExpression& InExpression2) : Expression1(InExpression1), Expression2(InExpression2)\r\n\t{\r\n\t}\r\n\r\n\tvirtual bool Interpret(const FString& Context) override\r\n\t{\r\n\t\treturn Expression1.Contains(Data) || Expression2.Contains(Data);\r\n\t}\r\n\r\nprivate:\r\n\tFExpression Expression1;\r\n\r\n\tFExpression Expression2;\r\n};\r\n\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n\r\n#pragma once\r\n\r\n#include \"Expression.h\"\r\n\r\n\r\nclass FAndExpression final : public FExpression\r\n{\r\npublic:\r\n\tFAndExpression(const FExpression& InExpression1, const FExpression& InExpression2) : Expression1(InExpression1), Expression2(InExpression2)\r\n\t{\r\n\t}\r\n\r\n\tvirtual bool Interpret(const FString& Context) override\r\n\t{\r\n\t\treturn Expression1.Contains(Data) && Expression2.Contains(Data);\r\n\t}\r\n\r\nprivate:\r\n\tFExpression Expression1;\r\n\r\n\tFExpression Expression2;\r\n};\r\n\r\n<\/pre>\n<p><strong>2.3 \u6b65\u9aa43<\/strong><\/p>\n<p>\u4f7f\u7528FExpression\u7c7b\u6765\u521b\u5efa\u89c4\u5219, \u5e76\u89e3\u6790\u5b83\u4eec.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n\r\n#include \"TerminalExpression.h\"\r\n#include \"OrExpression.h\"\r\n#include \"AndExpression.h\"\r\n\r\n#define BOOL_TO_STRING(Bool) Bool ? TEXT(\"true\") : TEXT(\"false\")\r\n\r\n\r\n\/\/ \u89c4\u5219: Robert\u548cJohn\u662f\u7537\u6027\r\nstatic FExpression GetMaleExpression()\r\n{\r\n\tFExpression Robert = FTerminalExpression(TEXT(\"Robert\"));\r\n\tFExpression John = FTerminalExpression(TEXT(\"John\"));\r\n\treturn FOrExpression(Robert, John);\r\n}\r\n\r\n\/\/ \u89c4\u5219: Julie\u662f\u4e00\u4e2a\u5df2\u5a5a\u7684\u5973\u6027\r\nstatic FExpression GetMarriedWomanExpression()\r\n{\r\n\tFExpression Julie = FTerminalExpression(TEXT(\"Julie\"));\r\n\tFExpression Married = FTerminalExpression(TEXT(\"Married\"));\r\n\treturn FOrExpression(Julie, Married);\r\n}\r\n\r\nint main()\r\n{\r\n\tFExpression IsMale = MoveTemp(GetMaleExpression());\r\n\tFExpression IsMarried = MoveTemp(GetMarriedWomanExpression());\r\n\r\n\tUE_LOG(LogTemp, Log, TEXT(\"John is male? %s\"), BOOL_TO_STRING(IsMale.Interpret(\"John\"));\r\n\tUE_LOG(LogTemp, Log, TEXT(\"Julie is a married women? %s\"), BOOL_TO_STRING(IsMarried.Interpret(\"Married Julie\"));\r\n\r\n\treturn 0;\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\u6750\u6599 1. \u3010UE4 \u8bbe\u8ba1\u6a21\u5f0f\u3011\u7b56\u7565\u6a21\u5f0f Strategy Pattern 2. \u89e3\u91ca\u5668\u6a21\u5f0f 3. \u5b57\u8282\u7801 &hellip; <a href=\"https:\/\/www.caiqinyi.cn\/index.php\/2025\/08\/28\/ue5_design_pattern_interpreter_pattern\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">[UE5 \u8bbe\u8ba1\u6a21\u5f0f] \u89e3\u91ca\u5668\u6a21\u5f0fInterpreter Pattern<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,32],"tags":[],"_links":{"self":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/4360"}],"collection":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/comments?post=4360"}],"version-history":[{"count":6,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/4360\/revisions"}],"predecessor-version":[{"id":4369,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/4360\/revisions\/4369"}],"wp:attachment":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}