{"id":4352,"date":"2025-08-26T09:52:19","date_gmt":"2025-08-26T01:52:19","guid":{"rendered":"https:\/\/www.caiqinyi.cn\/?p=4352"},"modified":"2025-08-28T10:13:55","modified_gmt":"2025-08-28T02:13:55","slug":"ue5_design_pattern_template_pattern","status":"publish","type":"post","link":"https:\/\/www.caiqinyi.cn\/index.php\/2025\/08\/26\/ue5_design_pattern_template_pattern\/","title":{"rendered":"[UE5 \u8bbe\u8ba1\u6a21\u5f0f] \u6a21\u677f\u6a21\u5f0fTemplate 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\/template-pattern.html\">\u6a21\u677f\u6a21\u5f0f<\/a><\/p>\n<p><strong>1. \u6982\u8ff0<\/strong><\/p>\n<p><strong>1.1 \u63cf\u8ff0<\/strong><\/p>\n<p>$\\cdot$ \u5728\u6a21\u677f\u6a21\u5f0f(Template Pattern) \u4e2d, \u4e00\u4e2a\u62bd\u8c61\u7c7b\u516c\u5f00\u5b9a\u4e49\u4e86\u6267\u884c\u5b83\u7684\u65b9\u6cd5\u7684\u65b9\u5f0f\/ \u6a21\u677f. \u5b83\u7684\u5b50\u7c7b\u53ef\u4ee5\u6309\u9700\u8981\u91cd\u5199\u65b9\u6cd5\u5b9e\u73b0, \u4f46\u8c03\u7528\u5c06\u4ee5\u62bd\u8c61\u7c7b\u4e2d\u5b9a\u4e49\u7684\u65b9\u5f0f\u8fdb\u884c. \u8fd9\u79cd\u7c7b\u578b\u7684\u8bbe\u8ba1\u6a21\u5f0f\u5c5e\u4e8e\u884c\u4e3a\u578b\u6a21\u5f0f.<\/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\u62bd\u8c61\u7236\u7c7b: \u5305\u542b\u6a21\u677f\u65b9\u6cd5\u548c\u4e00\u4e9b\u62bd\u8c61\u65b9\u6cd5\u6216\u5177\u4f53\u65b9\u6cd5.<br \/>\n$\\\\$ 2) \u5b9e\u73b0\u5b50\u7c7b: \u7ee7\u627f\u62bd\u8c61\u7236\u7c7b\u5e76\u5b9e\u73b0\u62bd\u8c61\u65b9\u6cd5, \u4e0d\u6539\u53d8\u7b97\u6cd5\u7ed3\u6784.<\/p>\n<p>$\\cdot$ <strong>\u5173\u952e\u4ee3\u7801:<\/strong><br \/>\n$\\\\$ 1) \u6a21\u677f\u65b9\u6cd5: \u5728\u62bd\u8c61\u7236\u7c7b\u4e2d\u5b9a\u4e49, \u8c03\u7528\u62bd\u8c61\u65b9\u6cd5\u548c\u5177\u4f53\u65b9\u6cd5.<br \/>\n$\\\\$ 2) \u62bd\u8c61\u65b9\u6cd5: \u7531\u5b50\u7c7b\u5b9e\u73b0, \u4ee3\u8868\u7b97\u6cd5\u7684\u53ef\u53d8\u90e8\u5206.<br \/>\n$\\\\$ 3) \u5177\u4f53\u65b9\u6cd5: \u5728\u62bd\u8c61\u7236\u7c7b\u4e2d\u5b9e\u73b0, \u4ee3\u8868\u7b97\u6cd5\u7684\u4e0d\u53d8\u90e8\u5206.<\/p>\n<p>$\\cdot$ <strong>\u7ed3\u6784:<\/strong><br \/>\n$\\\\$ \u5305\u542b\u7684\u51e0\u4e2a\u4e3b\u8981\u89d2\u8272:<br \/>\n$\\\\$ 1) \u62bd\u8c61\u7236\u7c7b(Abstract Class): \u5b9a\u4e49\u4e86\u6a21\u677f\u65b9\u6cd5\u548c\u4e00\u4e9b\u62bd\u8c61\u65b9\u6cd5\u6216\u5177\u4f53\u65b9\u6cd5.<br \/>\n$\\\\$ 2) \u5177\u4f53\u5b50\u7c7b(Concrete Classes): \u7ee7\u627f\u81ea\u62bd\u8c61\u7236\u7c7b, \u5e76\u5b9e\u73b0\u62bd\u8c61\u65b9\u6cd5.<br \/>\n$\\\\$ 3) \u94a9\u5b50\u65b9\u6cd5(Hook Method)(\u53ef\u9009): \u5728\u62bd\u8c61\u7236\u7c7b\u4e2d\u5b9a\u4e49, \u53ef\u4ee5\u88ab\u5b50\u7c7b\u91cd\u5199, \u4ee5\u5f71\u54cd\u6a21\u677f\u65b9\u6cd5\u7684\u884c\u4e3a.<br \/>\n$\\\\$ 4) \u5ba2\u6237\u7aef(Client)(\u53ef\u9009): \u4f7f\u7528\u62bd\u8c61\u7236\u7c7b\u548c\u5177\u4f53\u5b50\u7c7b, \u65e0\u9700\u5173\u5fc3\u6a21\u677f\u65b9\u6cd5\u7684\u7ec6\u8282.<\/p>\n<p><strong>1.3 \u4f7f\u7528\u573a\u666f<\/strong><\/p>\n<p>$\\cdot$ \u5f53\u5b58\u5728\u4e00\u4e9b\u901a\u7528\u7684\u65b9\u6cd5, \u53ef\u4ee5\u5728\u591a\u4e2a\u5b50\u7c7b\u4e2d\u5171\u7528\u65f6.<\/p>\n<p><strong>1.4 \u4f18\u7f3a\u70b9<\/strong><\/p>\n<p>$\\cdot$ <strong>\u4f18\u70b9:<\/strong><br \/>\n$\\\\$ 1) \u5c01\u88c5\u4e0d\u53d8\u90e8\u5206: \u7b97\u6cd5\u7684\u4e0d\u53d8\u90e8\u5206\u88ab\u5c01\u88c5\u5728\u7236\u7c7b\u4e2d.<br \/>\n$\\\\$ 2) \u6269\u5c55\u53ef\u53d8\u90e8\u5206: \u5b50\u7c7b\u53ef\u4ee5\u6269\u5c55\u6216\u4fee\u6539\u7b97\u6cd5\u7684\u53ef\u53d8\u90e8\u5206.<br \/>\n$\\\\$ 3) \u63d0\u53d6\u516c\u5171\u4ee3\u7801: \u51cf\u5c11\u4ee3\u7801\u91cd\u590d, \u4fbf\u4e8e\u7ef4\u62a4.<\/p>\n<p>$\\cdot$ <strong>\u7f3a\u70b9:<\/strong><br \/>\n$\\\\$ 1) \u7c7b\u6570\u76ee\u589e\u52a0: \u6bcf\u4e2a\u4e0d\u540c\u7684\u5b9e\u73b0\u90fd\u9700\u8981\u4e00\u4e2a\u5b50\u7c7b, \u53ef\u80fd\u5bfc\u81f4\u7cfb\u7edf\u5e9e\u5927.<\/p>\n<p><strong>2. UE5\u5b9e\u8df5<\/strong><\/p>\n<p>\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u5b9a\u4e49\u64cd\u4f5c\u7684FGame\u62bd\u8c61\u7c7b, \u5176\u4e2d, \u6a21\u677f\u65b9\u6cd5\u8bbe\u7f6e\u4e3afinal, \u8fd9\u6837\u5b83\u5c31\u4e0d\u4f1a\u88ab\u91cd\u5199. FCricket\u548cFFootball\u662f\u6269\u5c55\u4e86FGame\u7684\u5b9e\u4f53\u7c7b, \u5b83\u4eec\u91cd\u5199\u4e86\u62bd\u8c61\u7c7b\u7684\u65b9\u6cd5.<br \/>\n$\\\\$ \u6211\u4eec\u4f7f\u7528FGame\u6765\u6f14\u793a\u6a21\u677f\u6a21\u5f0f\u7684\u7528\u6cd5.<\/p>\n<p><strong>2.1 \u6b65\u9aa41<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u62bd\u8c61\u7c7b, \u5b83\u7684\u6a21\u677f\u65b9\u6cd5\u88ab\u8bbe\u7f6e\u4e3afinal.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n\r\n#pragma once\r\n\r\n\r\nclass FGame \r\n{\r\n\tvirtual void Initialize() = 0;\r\n\r\n\tvirtual void StartPlay() = 0;\r\n\r\n\tvirtual void EndPlay() = 0;\r\n\r\n\t\/\/ \u6a21\u677f\r\n\tvirtual void Play() final\r\n\t{\r\n\t\t\/\/ \u521d\u59cb\u5316\u6e38\u620f\r\n\t\tInitialize();\r\n\r\n\t\t\/\/ \u5f00\u59cb\u6e38\u620f\r\n\t\tStartPlay();\r\n\r\n\t\t\/\/ \u7ed3\u675f\u6e38\u620f\r\n\t\tEndPlay();\r\n\t}\r\n};\r\n\r\n<\/pre>\n<p><strong>2.2 \u6b65\u9aa42<\/strong><\/p>\n<p>\u521b\u5efa\u6269\u5c55\u4e86\u4e0a\u8ff0\u7c7b\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 \"Game.h\"\r\n#include \"CoreMinimal.h\"\r\n\r\n\r\nclass FCricket final : public FGame\r\n{\r\n\tvirtual void EndPlay() override\r\n\t{\r\n\t\tUE_LOG(LogTemp, Log, TEXT(\"Cricket Game Finished!\"));\r\n\t}\r\n\r\n\tvirtual void Initialize() override\r\n\t{\r\n\t\tUE_LOG(LogTemp, Log, TEXT(\"Cricket Game Initialized! Start playing.\"));\r\n\t}\r\n\r\n\tvirtual void StartPlay() override\r\n\t{\r\n\t\tUE_LOG(LogTemp, Log, TEXT(\"Cricket Game Started. Enjoy the game!\"));\r\n\t}\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 \"Game.h\"\r\n#include \"CoreMinimal.h\"\r\n\r\n\r\nclass FFootball final : public FGame\r\n{\r\n\tvirtual void EndPlay() override\r\n\t{\r\n\t\tUE_LOG(LogTemp, Log, TEXT(\"Football Game Finished!\"));\r\n\t}\r\n\r\n\tvirtual void Initialize() override\r\n\t{\r\n\t\tUE_LOG(LogTemp, Log, TEXT(\"Football Game Initialized! Start playing.\"));\r\n\t}\r\n\r\n\tvirtual void StartPlay() override\r\n\t{\r\n\t\tUE_LOG(LogTemp, Log, TEXT(\"Football Game Started. Enjoy the game!\"));\r\n\t}\r\n};\r\n\r\n<\/pre>\n<p><strong>2.3 \u6b65\u9aa43<\/strong><\/p>\n<p>\u4f7f\u7528FGame\u7684\u6a21\u677f\u65b9\u6cd5Play()\u6765\u6f14\u793a\u6e38\u620f\u7684\u5b9a\u4e49\u65b9\u5f0f.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"C++\" data-enlighter-theme=\"monokai\">\r\n\r\n#include \"Circket.h\"\r\n#incluce \"Football.h\"\r\n\r\n\r\nint main()\r\n{\r\n\tFGame* GameInstance = new FCricket();\r\n\tGameInstance->Play();\r\n\tdelete GameInstance;\r\n\r\n\tGameInstance = new FFootball();\r\n\tGameInstance->Play();\r\n\tdelete GameInstance;\r\n\r\n\tGameInstance = nullptr;\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. \u6a21\u677f\u6a21\u5f0f 1. \u6982\u8ff0 1 &hellip; <a href=\"https:\/\/www.caiqinyi.cn\/index.php\/2025\/08\/26\/ue5_design_pattern_template_pattern\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">[UE5 \u8bbe\u8ba1\u6a21\u5f0f] \u6a21\u677f\u6a21\u5f0fTemplate 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\/4352"}],"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=4352"}],"version-history":[{"count":8,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/4352\/revisions"}],"predecessor-version":[{"id":4366,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/posts\/4352\/revisions\/4366"}],"wp:attachment":[{"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.caiqinyi.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}