{"id":1763,"date":"2022-01-12T18:39:38","date_gmt":"2022-01-12T10:39:38","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=1763"},"modified":"2022-01-12T18:39:38","modified_gmt":"2022-01-12T10:39:38","slug":"%e5%bb%ba%e9%80%a0%e8%80%85%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=1763","title":{"rendered":"\u5efa\u9020\u8005\u6a21\u5f0f"},"content":{"rendered":"\n<p><strong>\u5148\u770b\u7ed3\u8bba<\/strong>\uff1a<strong>\u5f53\u4e00\u4e2a\u7c7b\u7684\u6784\u9020\u51fd\u6570\u53c2\u6570\u4e2a\u6570\u8d85\u8fc74\u4e2a\uff0c\u800c\u4e14\u8fd9\u4e9b\u53c2\u6570\u6709\u4e9b\u662f\u53ef\u9009\u7684\u53c2\u6570\uff0c\u8003\u8651\u4f7f\u7528\u6784\u9020\u8005\u6a21\u5f0f\u3002<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u51b3\u7684\u95ee\u9898<\/h2>\n\n\n\n<p>\u5f53\u4e00\u4e2a\u7c7b\u7684\u6784\u9020\u51fd\u6570\u53c2\u6570\u8d85\u8fc74\u4e2a\uff0c\u800c\u4e14\u8fd9\u4e9b\u53c2\u6570\u6709\u4e9b\u662f\u53ef\u9009\u7684\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u6709\u4e24\u79cd\u529e\u6cd5\u6765\u6784\u5efa\u5b83\u7684\u5bf9\u8c61\u3002 \u4f8b\u5982\u6211\u4eec\u73b0\u5728\u6709\u5982\u4e0b\u4e00\u4e2a\u7c7b\u8ba1\u7b97\u673a\u7c7b<code>Computer<\/code>\uff0c\u5176\u4e2dcpu\u4e0eram\u662f\u5fc5\u586b\u53c2\u6570\uff0c\u800c\u5176\u4ed63\u4e2a\u662f\u53ef\u9009\u53c2\u6570\uff0c\u90a3\u4e48\u6211\u4eec\u5982\u4f55\u6784\u9020\u8fd9\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u5462,\u901a\u5e38\u6709\u4e24\u79cd\u5e38\u7528\u7684\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Computer {\n    private String cpu;\/\/\u5fc5\u987b\n    private String ram;\/\/\u5fc5\u987b\n    private int usbCount;\/\/\u53ef\u9009\n    private String keyboard;\/\/\u53ef\u9009\n    private String display;\/\/\u53ef\u9009\n}<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e00\uff1a\u6298\u53e0\u6784\u9020\u51fd\u6570\u6a21\u5f0f\uff08telescoping constructor pattern \uff09\uff0c\u8fd9\u4e2a\u6211\u4eec\u7ecf\u5e38\u7528,\u5982\u4e0b\u4ee3\u7801\u6240\u793a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Computer {\n     ...\n    public Computer(String cpu, String ram) {\n        this(cpu, ram, 0);\n    }\n    public Computer(String cpu, String ram, int usbCount) {\n        this(cpu, ram, usbCount, \"\u7f57\u6280\u952e\u76d8\");\n    }\n    public Computer(String cpu, String ram, int usbCount, String keyboard) {\n        this(cpu, ram, usbCount, keyboard, \"\u4e09\u661f\u663e\u793a\u5668\");\n    }\n    public Computer(String cpu, String ram, int usbCount, String keyboard, String display) {\n        this.cpu = cpu;\n        this.ram = ram;\n        this.usbCount = usbCount;\n        this.keyboard = keyboard;\n        this.display = display;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e8c\u79cd\uff1aJavabean \u6a21\u5f0f\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Computer {\n        ...\n\n    public String getCpu() {\n        return cpu;\n    }\n    public void setCpu(String cpu) {\n        this.cpu = cpu;\n    }\n    public String getRam() {\n        return ram;\n    }\n    public void setRam(String ram) {\n        this.ram = ram;\n    }\n    public int getUsbCount() {\n        return usbCount;\n    }\n...\n}<\/code><\/pre>\n\n\n\n<p>\u90a3\u4e48\u8fd9\u4e24\u79cd\u65b9\u5f0f\u6709\u4ec0\u4e48\u5f0a\u7aef\u5462\uff1f<\/p>\n\n\n\n<p>\u7b2c\u4e00\u79cd\u4e3b\u8981\u662f\u4f7f\u7528\u53ca\u9605\u8bfb\u4e0d\u65b9\u4fbf\u3002\u4f60\u53ef\u4ee5\u60f3\u8c61\u4e00\u4e0b\uff0c\u5f53\u4f60\u8981\u8c03\u7528\u4e00\u4e2a\u7c7b\u7684\u6784\u9020\u51fd\u6570\u65f6\uff0c\u4f60\u9996\u5148\u8981\u51b3\u5b9a\u4f7f\u7528\u54ea\u4e00\u4e2a\uff0c\u7136\u540e\u91cc\u9762\u53c8\u662f\u4e00\u5806\u53c2\u6570\uff0c\u5982\u679c\u8fd9\u4e9b\u53c2\u6570\u7684\u7c7b\u578b\u5f88\u591a\u53c8\u90fd\u4e00\u6837\uff0c\u4f60\u8fd8\u8981\u641e\u6e05\u695a\u8fd9\u4e9b\u53c2\u6570\u7684\u542b\u4e49\uff0c\u5f88\u5bb9\u6613\u5c31\u4f20\u6df7\u4e86\u3002\u3002\u3002\u90a3\u9178\u723d\u8c01\u7528\u8c01\u77e5\u9053\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u79cd\u65b9\u5f0f\u5728\u6784\u5efa\u8fc7\u7a0b\u4e2d\u5bf9\u8c61\u7684\u72b6\u6001\u5bb9\u6613\u53d1\u751f\u53d8\u5316\uff0c\u9020\u6210\u9519\u8bef\u3002\u56e0\u4e3a\u90a3\u4e2a\u7c7b\u4e2d\u7684\u5c5e\u6027\u662f\u5206\u6b65\u8bbe\u7f6e\u7684\uff0c\u6240\u4ee5\u5c31\u5bb9\u6613\u51fa\u9519\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e24\u4e2a\u75db\u70b9\uff0cbuilder\u6a21\u5f0f\u5c31\u6a2a\u7a7a\u51fa\u4e16\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u5b9e\u73b0<\/h2>\n\n\n\n<ol><li>\u5728Computer \u4e2d\u521b\u5efa\u4e00\u4e2a\u9759\u6001\u5185\u90e8\u7c7b Builder\uff0c\u7136\u540e\u5c06Computer \u4e2d\u7684\u53c2\u6570\u90fd\u590d\u5236\u5230Builder\u7c7b\u4e2d\u3002<\/li><li>\u5728Computer\u4e2d\u521b\u5efa\u4e00\u4e2aprivate\u7684\u6784\u9020\u51fd\u6570\uff0c\u53c2\u6570\u4e3aBuilder\u7c7b\u578b<\/li><li>\u5728Builder\u4e2d\u521b\u5efa\u4e00\u4e2a<code>public<\/code>\u7684\u6784\u9020\u51fd\u6570\uff0c\u53c2\u6570\u4e3aComputer\u4e2d\u5fc5\u586b\u7684\u90a3\u4e9b\u53c2\u6570\uff0ccpu \u548cram\u3002<\/li><li>\u5728Builder\u4e2d\u521b\u5efa\u8bbe\u7f6e\u51fd\u6570\uff0c\u5bf9Computer\u4e2d\u90a3\u4e9b\u53ef\u9009\u53c2\u6570\u8fdb\u884c\u8d4b\u503c\uff0c\u8fd4\u56de\u503c\u4e3aBuilder\u7c7b\u578b\u7684\u5b9e\u4f8b<\/li><li>\u5728Builder\u4e2d\u521b\u5efa\u4e00\u4e2a<code>build()<\/code>\u65b9\u6cd5\uff0c\u5728\u5176\u4e2d\u6784\u5efaComputer\u7684\u5b9e\u4f8b\u5e76\u8fd4\u56de<\/li><\/ol>\n\n\n\n<p>\u4e0b\u9762\u4ee3\u7801\u5c31\u662f\u6700\u7ec8\u7684\u6837\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Computer {\n    private final String cpu;\/\/\u5fc5\u987b\n    private final String ram;\/\/\u5fc5\u987b\n    private final int usbCount;\/\/\u53ef\u9009\n    private final String keyboard;\/\/\u53ef\u9009\n    private final String display;\/\/\u53ef\u9009\n\n    private Computer(Builder builder){\n        this.cpu=builder.cpu;\n        this.ram=builder.ram;\n        this.usbCount=builder.usbCount;\n        this.keyboard=builder.keyboard;\n        this.display=builder.display;\n    }\n    public static class Builder{\n        private String cpu;\/\/\u5fc5\u987b\n        private String ram;\/\/\u5fc5\u987b\n        private int usbCount;\/\/\u53ef\u9009\n        private String keyboard;\/\/\u53ef\u9009\n        private String display;\/\/\u53ef\u9009\n\n        public Builder(String cup,String ram){\n            this.cpu=cup;\n            this.ram=ram;\n        }\n\n        public Builder setUsbCount(int usbCount) {\n            this.usbCount = usbCount;\n            return this;\n        }\n        public Builder setKeyboard(String keyboard) {\n            this.keyboard = keyboard;\n            return this;\n        }\n        public Builder setDisplay(String display) {\n            this.display = display;\n            return this;\n        }        \n        public Computer build(){\n            return new Computer(this);\n        }\n    }\n  \/\/\u7701\u7565getter\u65b9\u6cd5\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u4f7f\u7528<\/h2>\n\n\n\n<p>\u5728\u5ba2\u6237\u7aef\u4f7f\u7528\u94fe\u5f0f\u8c03\u7528\uff0c\u4e00\u6b65\u4e00\u6b65\u7684\u628a\u5bf9\u8c61\u6784\u5efa\u51fa\u6765\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Computer computer=new Computer.Builder(\"\u56e0\u7279\u5c14\",\"\u4e09\u661f\")\n                .setDisplay(\"\u4e09\u661f24\u5bf8\")\n                .setKeyboard(\"\u7f57\u6280\")\n                .setUsbCount(2)\n                .build();<\/code><\/pre>\n\n\n\n<p>\u53c2\u8003\u6587\u7ae0\uff1a<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/58093669\">\u79d2\u61c2\u8bbe\u8ba1\u6a21\u5f0f\u4e4b\u5efa\u9020\u8005\u6a21\u5f0f\uff08Builder pattern\uff09 &#8211; \u77e5\u4e4e (zhihu.com)<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u770b\u7ed3\u8bba\uff1a\u5f53\u4e00\u4e2a\u7c7b\u7684\u6784\u9020\u51fd\u6570\u53c2\u6570\u4e2a\u6570\u8d85\u8fc74\u4e2a\uff0c\u800c\u4e14\u8fd9\u4e9b\u53c2\u6570\u6709\u4e9b\u662f\u53ef\u9009\u7684\u53c2\u6570\uff0c\u8003\u8651\u4f7f\u7528\u6784\u9020\u8005\u6a21\u5f0f\u3002 \u89e3\u51b3\u7684\u95ee\u9898  [&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\/1763"}],"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=1763"}],"version-history":[{"count":2,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1763\/revisions"}],"predecessor-version":[{"id":1765,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1763\/revisions\/1765"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}