{"id":203020,"date":"2025-05-22T10:03:02","date_gmt":"2025-05-22T02:03:02","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203020\/"},"modified":"2025-05-22T10:03:02","modified_gmt":"2025-05-22T02:03:02","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e4%b8%ad%e9%ab%98%e9%98%b6%e5%87%bd%e6%95%b0%e5%9c%a8%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8%e5%9c%ba%e6%99%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203020\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u5728\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u5e94\u7528\u573a\u666f\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u5728\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u5e94\u7528\u573a\u666f\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u5728\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u5e94\u7528\u573a\u666f\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730021975671e0a5782d62.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u5728\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u5e94\u7528\u573a\u666f<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4f7f\u7528\u9ad8\u9636\u51fd\u6570\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u6216\u8fd4\u56de\u503c\uff0c\u8fd9\u5728\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n<p><strong>\u7b56\u7565\u6a21\u5f0f<\/strong><\/p>\n<p>\u7b56\u7565\u6a21\u5f0f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b97\u6cd5\u65cf\uff0c\u5b83\u4eec\u4e4b\u95f4\u53ef\u4ee5\u4e92\u6362\uff0c\u4ece\u800c\u8ba9\u7b97\u6cd5\u72ec\u7acb\u4e8e\u4f7f\u7528\u5b83\u4eec\u7684\u5ba2\u6237\u7aef\u3002<\/p>\n<p><strong>\u4f7f\u7528\u9ad8\u9636\u51fd\u6570\uff1a<\/strong><\/p>\n<pre>interface Strategy {\n    int calculate(int a, int b);\n}\n\nStrategy addStrategy = (a, b) -&gt; a + b;\nStrategy subtractStrategy = (a, b) -&gt; a - b;\n\nint result = calculate(addStrategy, 10, 5); \/\/ 15\nint result = calculate(subtractStrategy, 10, 5); \/\/ 5\n\nprivate int calculate(Strategy strategy, int a, int b) {\n    return strategy.calculate(a, b);\n}<\/pre>\n<p><strong>\u9002\u914d\u5668\u6a21\u5f0f<\/strong><\/p>\n<p>\u9002\u914d\u5668\u6a21\u5f0f\u5c06\u4e00\u4e2a\u7c7b\u7684\u63a5\u53e3\u8f6c\u6362\u6210\u53e6\u4e00\u4e2a\u63a5\u53e3\uff0c\u4f7f\u5f97\u539f\u672c\u4e0d\u517c\u5bb9\u7684\u7c7b\u53ef\u4ee5\u534f\u540c\u5de5\u4f5c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u9ad8\u9636\u51fd\u6570\uff1a<\/strong><\/p>\n<pre>class LegacyClass {\n    public String getOldName() {\n        return \"Old Name\";\n    }\n}\n\ninterface NewInterface {\n    String getNewName();\n}\n\nclass Adapter implements NewInterface {\n    private LegacyClass legacyClass;\n\n    Adapter(LegacyClass legacyClass) {\n        this.legacyClass = legacyClass;\n    }\n\n    @Override\n    public String getNewName() {\n        return legacyClass.getOldName();\n    }\n}\n\nNewInterface newInterface = new Adapter(new LegacyClass());\nString newName = newInterface.getNewName(); \/\/ \"Old Name\"<\/pre>\n<p><strong>\u88c5\u9970\u5668\u6a21\u5f0f<\/strong><\/p>\n<p>\u88c5\u9970\u5668\u6a21\u5f0f\u52a8\u6001\u5730\u7ed9\u4e00\u4e2a\u5bf9\u8c61\u6dfb\u52a0\u65b0\u7684\u884c\u4e3a\uff0c\u8fd9\u5c31\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u5bf9\u8c61\u672c\u8eab\u7684\u60c5\u51b5\u4e0b\u6269\u5c55\u5176\u529f\u80fd\u3002<\/p>\n<p><strong>\u4f7f\u7528\u9ad8\u9636\u51fd\u6570\uff1a<\/strong><\/p>\n<pre>class Decorator implements Shape {\n    private Shape shape;\n\n    Decorator(Shape shape) {\n        this.shape = shape;\n    }\n\n    @Override\n    public void draw() {\n        shape.draw();\n    }\n}\n\nclass ColoredShape extends Decorator {\n    private String color;\n\n    ColoredShape(Shape shape, String color) {\n        super(shape);\n        this.color = color;\n    }\n\n    @Override\n    public void draw() {\n        super.draw();\n        System.out.println(\"Color: \" + color);\n    }\n}\n\nShape redCircle = new ColoredShape(new Circle(), \"Red\");\nredCircle.draw(); \/\/ \"Draw a Circle in Red color\"<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u5728\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u5e94\u7528\u573a\u666f\uff1f\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9ad8\u9636\u51fd\u6570\u5728\u8bbe&#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-203020","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203020","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=203020"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203020\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}