{"id":2065,"date":"2022-02-21T13:21:03","date_gmt":"2022-02-21T05:21:03","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2065"},"modified":"2022-02-21T13:21:03","modified_gmt":"2022-02-21T05:21:03","slug":"%e6%a8%a1%e7%89%88%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=2065","title":{"rendered":"\u6a21\u7248\u6a21\u5f0f"},"content":{"rendered":"\n<p>\u6a21\u7248\u6a21\u5f0f\uff0c\u5c31\u662f\u5b9a\u4e49\u62bd\u8c61\u7c7b\uff0c\u62bd\u53d6\u516c\u5171\u65b9\u6cd5\uff0c\u5b50\u7c7b\u5b9e\u73b0\u6709\u533a\u522b\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u6a21\u677f\u65b9\u6cd5\u90fd\u52a0\u4e0a final \u5173\u952e\u8bcd\u3002<\/p>\n\n\n\n<p><strong>\u4f18\u70b9\uff1a<\/strong>&nbsp;1\u3001\u5c01\u88c5\u4e0d\u53d8\u90e8\u5206\uff0c\u6269\u5c55\u53ef\u53d8\u90e8\u5206\u3002 2\u3001\u63d0\u53d6\u516c\u5171\u4ee3\u7801\uff0c\u4fbf\u4e8e\u7ef4\u62a4\u3002 3\u3001\u884c\u4e3a\u7531\u7236\u7c7b\u63a7\u5236\uff0c\u5b50\u7c7b\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u6bcf\u4e00\u4e2a\u4e0d\u540c\u7684\u5b9e\u73b0\u90fd\u9700\u8981\u4e00\u4e2a\u5b50\u7c7b\u6765\u5b9e\u73b0\uff0c\u5bfc\u81f4\u7c7b\u7684\u4e2a\u6570\u589e\u52a0\uff0c\u4f7f\u5f97\u7cfb\u7edf\u66f4\u52a0\u5e9e\u5927\u3002<\/p>\n\n\n\n<p><strong>\u4f7f\u7528\u573a\u666f\uff1a<\/strong>\u00a01\u3001\u6709\u591a\u4e2a\u5b50\u7c7b\u5171\u6709\u7684\u65b9\u6cd5\uff0c\u4e14\u903b\u8f91\u76f8\u540c\u3002 2\u3001\u91cd\u8981\u7684\u3001\u590d\u6742\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8003\u8651\u4f5c\u4e3a\u6a21\u677f\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u53c2\u8003\uff1a<a href=\"https:\/\/www.runoob.com\/design-pattern\/template-pattern.html\">\u6a21\u677f\u6a21\u5f0f | \u83dc\u9e1f\u6559\u7a0b (runoob.com)<\/a><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6b65\u9aa4 1\n\u521b\u5efa\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u5b83\u7684\u6a21\u677f\u65b9\u6cd5\u88ab\u8bbe\u7f6e\u4e3a final\u3002\n\nGame.java\npublic abstract class Game {\n   abstract void initialize();\n   abstract void startPlay();\n   abstract void endPlay();\n \n   \/\/\u6a21\u677f\n   public final void play(){\n \n      \/\/\u521d\u59cb\u5316\u6e38\u620f\n      initialize();\n \n      \/\/\u5f00\u59cb\u6e38\u620f\n      startPlay();\n \n      \/\/\u7ed3\u675f\u6e38\u620f\n      endPlay();\n   }\n}\n\n\u6b65\u9aa4 2\n\u521b\u5efa\u6269\u5c55\u4e86\u4e0a\u8ff0\u7c7b\u7684\u5b9e\u4f53\u7c7b\u3002\n\nCricket.java\npublic class Cricket extends Game {\n \n   @Override\n   void endPlay() {\n      System.out.println(\"Cricket Game Finished!\");\n   }\n \n   @Override\n   void initialize() {\n      System.out.println(\"Cricket Game Initialized! Start playing.\");\n   }\n \n   @Override\n   void startPlay() {\n      System.out.println(\"Cricket Game Started. Enjoy the game!\");\n   }\n}\nFootball.java\npublic class Football extends Game {\n \n   @Override\n   void endPlay() {\n      System.out.println(\"Football Game Finished!\");\n   }\n \n   @Override\n   void initialize() {\n      System.out.println(\"Football Game Initialized! Start playing.\");\n   }\n \n   @Override\n   void startPlay() {\n      System.out.println(\"Football Game Started. Enjoy the game!\");\n   }\n}\n\n\u6b65\u9aa4 3\n\u4f7f\u7528 Game \u7684\u6a21\u677f\u65b9\u6cd5 play() \u6765\u6f14\u793a\u6e38\u620f\u7684\u5b9a\u4e49\u65b9\u5f0f\u3002\n\nTemplatePatternDemo.java\npublic class TemplatePatternDemo {\n   public static void main(String[] args) {\n \n      Game game = new Cricket();\n      game.play();\n      System.out.println();\n      game = new Football();\n      game.play();      \n   }\n}\n\n\u6b65\u9aa4 4\n\u6267\u884c\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\uff1a\n\nCricket Game Initialized! Start playing.\nCricket Game Started. Enjoy the game!\nCricket Game Finished!\n\nFootball Game Initialized! Start playing.\nFootball Game Started. Enjoy the game!\nFootball Game Finished!<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6a21\u7248\u6a21\u5f0f\uff0c\u5c31\u662f\u5b9a\u4e49\u62bd\u8c61\u7c7b\uff0c\u62bd\u53d6\u516c\u5171\u65b9\u6cd5\uff0c\u5b50\u7c7b\u5b9e\u73b0\u6709\u533a\u522b\u7684\u65b9\u6cd5\u3002 \u4e00\u822c\u6a21\u677f\u65b9\u6cd5\u90fd\u52a0\u4e0a final \u5173\u952e\u8bcd\u3002 \u4f18\u70b9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[131],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2065"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2065"}],"version-history":[{"count":8,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2065\/revisions"}],"predecessor-version":[{"id":2073,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2065\/revisions\/2073"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}