{"id":45,"date":"2009-08-19T01:29:17","date_gmt":"2009-08-18T17:29:17","guid":{"rendered":"http:\/\/www.gemchen.cn\/?p=45"},"modified":"2009-08-19T01:29:17","modified_gmt":"2009-08-18T17:29:17","slug":"%e6%b0%b8%e8%bf%9c%e4%b8%8d%e8%a6%81%e5%9c%a8java%e4%b8%ad%e4%bd%bf%e7%94%a8string%e8%87%b3%e5%b0%91%e4%b9%9f%e5%b0%bd%e9%87%8f%e5%b0%91%e7%94%a8","status":"publish","type":"post","link":"https:\/\/gemchen.cn\/?p=45","title":{"rendered":"\u6c38\u8fdc\u4e0d\u8981\u5728Java\u4e2d\u4f7f\u7528String(\u81f3\u5c11\u4e5f\u5c3d\u91cf\u5c11\u7528:-)"},"content":{"rendered":"<p>\u6c38\u8fdc\u4e0d\u8981\u4f7f\u7528(\u672a\u5c01\u88c5)\u7684String\u6216long,int. \u4e3a\u4ec0\u4e48\u5462? \u56e0\u4e3a\u8fd9\u4e9b\u57fa\u7840\u7c7b\u578b(Primitive)\u6ca1\u6709\u8bed\u4e49(Semantic meaning). \u5b83\u4eec\u5f88\u96be\u88ab\u7406\u89e3,\u7ef4\u62a4\u548c\u6269\u5c55.<\/p>\n<p>\u7a7a\u53e3\u65e0\u51ed,\u4e3e\u4f8b\u4e3a\u8bc1: \u5047\u8bbe\u6709\u4e2a\u4f8b\u5b50\u662f\u8ba2\u9605\u7535\u5f71\u7968\u7684\u670d\u52a1.<\/p>\n<p>\u8bd5\u7740\u6bd4\u8f83:<\/p>\n<blockquote><p><code>public void bookTicket( String name, String firstName, String film, int count, String cinema);<\/code> <\/p><\/blockquote>\n<p>\u4e0e:(\u5f53\u7136\u5b9e\u9645\u4f7f\u7528\u65f6,\u7528\u4e00\u4e2aOrder\u5bf9\u8c61\u4f1a\u66f4\u597d:-)<\/p>\n<blockquote><p><code>public void bookTicket( Name name, FirstName firstName, Film film, Count count, Cinema cinema);<\/code> <\/p><\/blockquote>\n<p> \u5f88\u660e\u663e,\u7b2c\u4e8c\u4e2a\u8981\u5bb9\u6613\u9605\u8bfb\u7684\u591a. \u5c24\u5176\u662f\u5f53\u5728IDE\u4e2d\u4f7f\u7528\u81ea\u52a8\u8865\u5168\u7684\u65f6\u5019,<code><strong>bookTicket(String arg0, String arg1, String arg2, int arg3, String arg4)<\/strong><\/code>\u6bd4<code><strong>bookTicket(Name arg0, FirstName arg1, Film arg2, Count arg3, Cinema arg4)<\/strong><\/code>\u5dee\u7684\u592a\u660e\u663e\u4e86.\u5f53\u67d0\u4e2a\u63a5\u53e3\u88ab\u7f6e\u4e8e\u4e00\u4e2a\u4e0d\u5e26\u6e90\u7801\u7684jar\u5305\u5185\u65f6,\u8fd9\u79cd\u60c5\u51b5\u5f88\u5e38\u89c1.   <\/p>\n<p>\u518d\u5c1d\u8bd5\u6bd4\u8f83:<\/p>\n<blockquote><p><code>void book(String orderId);<\/code> <\/p><\/blockquote>\n<p>\u4e0e:<\/p>\n<blockquote><p><code>void book(OrderId order);<\/code> <\/p><\/blockquote>\n<p>\u7b2c\u4e00\u79cd\u60c5\u51b5\u4e0b,\u5f00\u53d1\u4eba\u5458\u770b\u5230\u8fd9\u884c\u4ee3\u7801\u4f1a\u8003\u8651:a.)\u600e\u6837\u83b7\u5f97\u4e00\u4e2aorderId \u548cb.)orderId\u5230\u5e95\u662f\u4ec0\u4e48\u73a9\u610f, &quot;1212&quot;, &quot;ABC-123&quot; or &quot;12-34-45-SCHWEINEBACKE&quot;. \u7b2c\u4e8c\u79cd\u60c5\u51b5,\u4ed6\u53ef\u4ee5\u53bb\u67e5\u770bOrderId\u7c7b\u7684Javadoc\u6216\u8005\u4f7f\u7528\u624b\u518c\u83b7\u53d6\u5b83\u7684\u6b63\u786e\u7528\u6cd5. \u6216\u8bb8\u89c9\u5f97, \u8fd9\u4e0d\u8fc7\u662f\u4e2aorderId\u7f62\u4e86. \u4f46\u5728<a href=\"http:\/\/en.wikipedia.org\/wiki\/Legacy_system\">\u9057\u7559\u7cfb\u7edf<\/a>\u4e2d,\u540d\u79f0\u548c\u8bed\u4e49\u662f\u7ecf\u5e38\u4e0d\u4e00\u81f4\u7684. \u6211\u66fe\u7ecf\u89c1\u8fc7\u4e00\u4e2a\u7cfb\u7edf\u91cc\u9762\u628aorder ID\u547d\u540d\u4e3a&quot;orderId&quot;, &quot;auftragsId&quot;, &quot;id&quot;\u4ee5\u53ca\u5176\u4ed6\u4ec0\u4e48\u4e1c\u897f,\u800c\u4e14\u5c45\u7136\u6307\u7684\u662f\u540c\u4e00\u4e2a\u4e1c\u897f: order ID!<\/p>\n<p>\u591a\u4f7f\u7528\u7c7b\u66ff\u4ee3\u57fa\u672c\u7c7b\u578b\u4e0d\u5149\u6709\u8bed\u4e49\u4e0a\u7684\u4f18\u52bf. \u6269\u5c55\u4e0a\u4e5f\u66f4\u4f73: \u6bd4\u5982\u4f60\u53ef\u4ee5\u66f4\u8f7b\u677e\u7684\u5c06OrderId\u7c7b\u91cc\u7684int\u66ff\u6362\u6210long, \u589e\u52a0\u6821\u9a8c\u548cid\u751f\u6210\u903b\u8f91. \u76f8\u6bd4\u800c\u8a00,\u6700\u521d\u82e5\u4f7f\u7528String\u7c7b\u578b\u8981\u505a\u8fd9\u4e9b\u5c31\u56f0\u96be\u7684\u591a.<\/p>\n<h6>\u4f7f\u7528fluent interface\u5b9e\u73b0<\/h6>\n<p>fluent interface\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u77ed,\u7f16\u5199\u66f4\u5bb9\u6613. Google Collections\u7684MapMaker\u662f\u4e2a\u4e0d\u9519\u7684\u4f8b\u5b50:<\/p>\n<blockquote>\n<pre>ConcurrentMap graphs = new MapMaker()\n    .concurrencyLevel(32)\n    .softKeys()\n    .weakValues()\n    .expiration(30, TimeUnit.MINUTES)\n    .makeComputingMap(\n        new Function() {\n            public Graph apply(Key key) {\n                return createExpensiveGraph(key);\n            }\n        });<\/pre>\n<\/blockquote>\n<p>\u4f7f\u7528fluent interface\u5b9e\u73b0\u7684\u5982\u4e0b\u6240\u793a,\u50cf\u7b80\u5355\u7684\u9886\u57df\u7c7b(Domain classes)\u6216\u8005\u50cf\u4e0d\u53ef\u53d8\u503c\u7684\u5bf9\u8c61. \u4ec5\u4ec5\u662f\u5c01\u88c5\u4e86String\u5e76\u589e\u52a0\u4e86\u4e00\u4e9b\u8bed\u4e49\u7684\u4fe1\u606f\u7ed9String.<\/p>\n<blockquote>\n<pre>public class Name {\n   public Name(String name) {\n      ...\n   }\n   public static Name name(String name) {\n     return new Name(name);\n   }\n}<\/pre>\n<\/blockquote>\n<p>\u6709\u4eba\u6216\u8bb8\u89c9\u5f97\u8fd9\u79cd\u65b9\u5f0f\u592a\u8fc7\u7e41\u6742(noisy)\u4e86. \u6bd4\u5982:<\/p>\n<blockquote><p><code>new Customer(new FirstName(&quot;Stephan&quot;), new Name(&quot;Schmidt&quot;));<\/code> <\/p><\/blockquote>\n<p>\u8981\u6bd4\u5982\u4e0b\u7684\u76f4\u63a5\u4f7f\u7528String\u53c2\u6570\u7684\u65b9\u5f0f\u7e41\u6742.<\/p>\n<blockquote><p><code>new Customer(&quot;Stephan&quot;, &quot;Schmidt&quot;);<\/code> <\/p><\/blockquote>\n<p>\u4f46,\u4e0d\u53ef\u5426\u8ba4,\u7b2c\u4e00\u4e2a\u8981\u66f4\u52a0\u5bb9\u6613\u7406\u89e3, \u82e5\u4f7f\u7528\u9759\u6001\u65b9\u6cd5,\u7b2c\u4e00\u4e2a\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u53d8\u4e3a:<\/p>\n<blockquote><p><code>new Customer(firstName(&quot;Stephan&quot;), name(&quot;Schmidt&quot;));<\/code> <\/p><\/blockquote>\n<p>\u8fd9\u6837\u4e5f\u8f83\u597d\u7684\u89e3\u51b3\u4e86\u53c2\u6570\u8f83\u591a\u65f6\u8ba9\u4eba\u7cca\u6d82\u7684\u95ee\u9898\uff0e<\/p>\n<p>\uff3b\u6587\u7ae0\u7ffb\u8bd1\u81ea\uff1a<a title=\"http:\/\/codemonkeyism.com\/never-never-never-use-string-in-java-or-at-least-less-often\/\" href=\"http:\/\/codemonkeyism.com\/never-never-never-use-string-in-java-or-at-least-less-often\/\">http:\/\/codemonkeyism.com\/never-never-never-use-string-in-java-or-at-least-less-often\/<\/a>&#160;<strong>\u8bf7\u4efb\u610f\u8f6c\u8f7d<\/strong>\uff3d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6c38\u8fdc\u4e0d\u8981\u4f7f\u7528(\u672a\u5c01\u88c5)\u7684String\u6216long,int. \u4e3a\u4ec0\u4e48\u5462? \u56e0\u4e3a\u8fd9\u4e9b\u57fa\u7840\u7c7b\u578b(Primitive)\u6ca1\u6709\u8bed\u4e49(Semantic meaning). \u5b83\u4eec\u5f88\u96be\u88ab\u7406\u89e3,\u7ef4\u62a4\u548c\u6269\u5c55. \u7a7a\u53e3\u65e0\u51ed,\u4e3e\u4f8b&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,1],"tags":[25],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-dev","category-uncategorized","tag-java"],"_links":{"self":[{"href":"https:\/\/gemchen.cn\/index.php?rest_route=\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gemchen.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gemchen.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gemchen.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gemchen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=45"}],"version-history":[{"count":0,"href":"https:\/\/gemchen.cn\/index.php?rest_route=\/wp\/v2\/posts\/45\/revisions"}],"wp:attachment":[{"href":"https:\/\/gemchen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gemchen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gemchen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}