{"id":202812,"date":"2025-05-22T10:43:36","date_gmt":"2025-05-22T02:43:36","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202812\/"},"modified":"2025-05-22T10:43:36","modified_gmt":"2025-05-22T02:43:36","slug":"java%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87%e7%ad%96%e7%95%a5%e6%a8%a1%e5%bc%8f%e6%8f%90%e9%ab%98%e4%bb%a3%e7%a0%81%e5%8f%af%e5%a4%8d%e7%94%a8%e6%80%a7%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202812\/","title":{"rendered":"Java\u51fd\u6570\u5982\u4f55\u901a\u8fc7\u7b56\u7565\u6a21\u5f0f\u63d0\u9ad8\u4ee3\u7801\u53ef\u590d\u7528\u6027\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5982\u4f55\u901a\u8fc7\u7b56\u7565\u6a21\u5f0f\u63d0\u9ad8\u4ee3\u7801\u53ef\u590d\u7528\u6027\uff1f<\/h1>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aJava\u51fd\u6570\u5982\u4f55\u901a\u8fc7\u7b56\u7565\u6a21\u5f0f\u63d0\u9ad8\u4ee3\u7801\u53ef\u590d\u7528\u6027\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u63d0\u9ad8 Java \u51fd\u6570\u53ef\u590d\u7528\u6027\uff1a\u5b9a\u4e49\u7b56\u7565\u63a5\u53e3\uff0c\u58f0\u660e\u7b56\u7565\u884c\u4e3a\u3002\u4e3a\u6bcf\u4e2a\u7b97\u6cd5\u521b\u5efa\u5177\u4f53\u7b56\u7565\u7c7b\uff0c\u5b9e\u73b0\u7b56\u7565\u63a5\u53e3\u3002\u521b\u5efa\u4e0a\u4e0b\u6587\u7c7b\u7ba1\u7406\u7b56\u7565\uff0c\u5e76\u6839\u636e\u9700\u8981\u5e94\u7528\u5b83\u4eec\u3002\u4e0a\u4e0b\u6587\u7c7b\u901a\u8fc7\u8c03\u7528\u7b56\u7565\u65b9\u6cd5\u6267\u884c\u7b97\u6cd5\u3002\u901a\u8fc7\u4f20\u9012\u4e0d\u540c\u7b56\u7565\uff0c\u53ef\u4ee5\u52a8\u6001\u66f4\u6539\u51fd\u6570\u7684\u884c\u4e3a\uff0c\u63d0\u9ad8\u4ee3\u7801\u53ef\u590d\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729944765671cdcbda9a9a.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u63d0\u9ad8 Java \u51fd\u6570\u53ef\u590d\u7528\u6027<\/strong><\/p>\n<p>\u7b56\u7565\u6a21\u5f0f\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u5141\u8bb8\u4f60\u5c06\u4e00\u7ec4\u76f8\u5173\u7684\u7b97\u6cd5\u5c01\u88c5\u6210\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u5b83\u4eec\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u60c5\u51b5\u4e0b\u4ea4\u6362\u4f7f\u7528\u3002\u8fd9\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8f7b\u677e\u5730\u521b\u5efa\u548c\u66ff\u6362\u7b97\u6cd5\u3002<\/p>\n<p><strong>\u5b9e\u73b0<\/strong><\/p>\n<p>\u5728 Java \u4e2d\u5b9e\u73b0\u7b56\u7565\u6a21\u5f0f\u5f88\u7b80\u5355\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3\u6765\u58f0\u660e\u7b56\u7565\u7684\u884c\u4e3a\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>interface Strategy {\n    int doSomething(int a, int b);\n}<\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u4f60\u9700\u8981\u4e3a\u6bcf\u4e2a\u7b97\u6cd5\u521b\u5efa\u4e00\u4e2a\u5177\u4f53\u7684\u7b56\u7565\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u5b9e\u73b0 <code>Strategy<\/code> \u63a5\u53e3\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>class AddStrategy implements Strategy {\n    @Override\n    public int doSomething(int a, int b) {\n        return a + b;\n    }\n}\n\nclass SubtractStrategy implements Strategy {\n    @Override\n    public int doSomething(int a, int b) {\n        return a - b;\n    }\n}<\/pre>\n<p>\u7136\u540e\uff0c\u4f60\u9700\u8981\u4e00\u4e2a\u7c7b\u6765\u7ba1\u7406\u8fd9\u4e9b\u7b56\u7565\u5e76\u6839\u636e\u9700\u8981\u5e94\u7528\u5b83\u4eec\u3002\u8fd9\u4e2a\u7c7b\u901a\u5e38\u79f0\u4e3a <strong>\u4e0a\u4e0b\u6587<\/strong>\u3002\u4e0a\u4e0b\u6587\u7c7b\u6709\u4e00\u4e2a\u5b57\u6bb5\u6765\u5b58\u50a8\u5f53\u524d\u7684\u7b56\u7565\uff0c\u4ee5\u53ca\u4e00\u4e2a\u65b9\u6cd5\u6765\u8c03\u7528\u7b56\u7565\u5e76\u6267\u884c\u7b97\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>class Context {\n    private Strategy strategy;\n\n    public void setStrategy(Strategy strategy) {\n        this.strategy = strategy;\n    }\n\n    public int executeStrategy(int a, int b) {\n        return strategy.doSomething(a, b);\n    }\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u7684\u7b80\u5355\u793a\u4f8b\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u51fd\u6570 <code>calculate<\/code>\uff0c\u5b83\u6839\u636e\u4f20\u9012\u7ed9\u5b83\u7684\u7b56\u7565\u6267\u884c\u4e0d\u540c\u7684\u8ba1\u7b97\u3002<\/p>\n<pre>public class Calculate {\n    private Strategy strategy;\n\n    public void setStrategy(Strategy strategy) {\n        this.strategy = strategy;\n    }\n\n    public int calculate(int a, int b) {\n        return strategy.doSomething(a, b);\n    }\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f20\u9012\u4e0d\u540c\u7684\u7b56\u7565\u6765\u52a8\u6001\u66f4\u6539\u51fd\u6570\u7684\u884c\u4e3a\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>Calculate calculate = new Calculate();\ncalculate.setStrategy(new AddStrategy());\nint result = calculate.calculate(10, 5); \/\/ result = 15\n\ncalculate.setStrategy(new SubtractStrategy());\nresult = calculate.calculate(10, 5); \/\/ result = 5<\/pre>\n<p>\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u884c\u4e3a\uff0c\u800c\u65e0\u9700\u4fee\u6539\u51fd\u6570\u672c\u8eab\u7684\u4ee3\u7801\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u590d\u7528\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aJava\u51fd\u6570\u5982\u4f55\u901a\u8fc7\u7b56\u7565\u6a21\u5f0f\u63d0\u9ad8\u4ee3\u7801\u53ef\u590d\u7528\u6027\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7b56\u7565\u6a21\u5f0f,\u4ee3\u7801\u53ef\u590d\u7528\u6027\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5982\u4f55\u901a\u8fc7\u7b56\u7565\u6a21\u5f0f\u63d0\u9ad8&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202812","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202812"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202812\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}