{"id":11823,"date":"2016-11-17T16:49:44","date_gmt":"2016-11-17T08:49:44","guid":{"rendered":"https:\/\/lifeparty.idv.tw\/blog\/?p=11823"},"modified":"2016-11-17T19:57:43","modified_gmt":"2016-11-17T11:57:43","slug":"%e5%ae%a3%e5%91%8a%e5%bc%8f%e8%aa%9e%e6%84%8f%e7%9a%84%e8%84%88%e7%b5%a1","status":"publish","type":"post","link":"https:\/\/lifeparty.idv.tw\/blog\/archives\/11823","title":{"rendered":"\u5ba3\u544a\u5f0f\u8a9e\u610f\u7684\u8108\u7d61"},"content":{"rendered":"<p>\u9019\u500b\u7db2\u8a8c\u767c\u8868\u904e\u4e0d\u5c11<a href=\"https:\/\/lifeparty.idv.tw\/blog\/?s=\u5ba3\u544a\u5f0f\u8a9e\u610f\">\u5ba3\u544a\u5f0f\u8a9e\u610f<\/a>\u7684\u6587\u7ae0\uff0c\u5b83\u5011\u662f\u540c\u4eba\u4ee5<a href=\"https:\/\/en.wikipedia.org\/wiki\/Functional_programming\">\u6cdb\u51fd\u7de8\u7a0b<\/a>\u7684\u601d\u7dad\uff0c\u4ee5\u4e0d\u540c\u8a9e\u8a00\u5305\u62ec C++\u3001\u4ee5\u53ca Java 1.6 \u5be6\u4f5c\u5ba3\u544a\u5f0f\u8a9e\u610f\u7684\u624b\u6cd5\u3002\u7136\u800c\uff0c\u9019\u534a\u5e74\u4f86\uff0c\u540c\u4eba\u7528 Java 1.8 \u5be6\u73fe\u5ba3\u544a\u5f0f\u8a9e\u610f\u7684\u8a2d\u8a08\u624b\u6cd5\uff0c\u6df1\u6df1\u611f\u53d7\u5230\u5ba3\u544a\u5f0f\u8a9e\u610f\u8108\u7d61\u4e4b\u5999\uff0c\u6240\u4ee5\u9019\u7bc7\u6587\u7ae0\u8981\u4f86\u8ac7\u4e00\u8ac7\u5ba3\u544a\u5f0f\u8a9e\u610f\u7684\u8108\u7d61\uff0c\u8b93\u6211\u5011\u59d1\u4e14\u7a31\u5b83\u70ba Context \u6a23\u5f0f\u5427\uff01<\/p>\n<p>\u5ba3\u544a\u5f0f\u7684\u904b\u7b97\u908f\u8f2f\u548c\u547d\u4ee4\u5f0f\u7684\u904b\u7b97\u908f\u8f2f\u7684\u5dee\u5225\uff0c\u7528\u6700\u7c21\u55ae\u5be6\u969b\u800c\u53c8\u5177\u9ad4\u7684\u8a9e\u8a00\u4f86\u8aaa\u660e\uff0c\u5c31\u662f\u524d\u8005\u7528\u300c\u689d\u4ef6\u5224\u65b7\u300d\u548c\u300c\u8ff4\u5708\u8655\u7406\u300d\u4f86\u63a7\u5236\u6d41\u7a0b\uff0c\u800c\u5f8c\u8005\u5247\u662f\u53d6\u6cd5\u65bc\u6578\u5b78\u6a21\u578b\u5178\u7bc4\uff1b\u4e5f\u5c31\u662f\u5229\u7528\u64cd\u4f5c\u300c\u96c6\u5408\u300d\u7684\u6982\u5ff5\u4e26\u4e14\u904b\u7528\u300c\u905e\u8ff4\u300d\u7684\u624b\u6cd5\u4f86\u5316\u7c21\u554f\u984c\uff0c\u9084\u6709\u7528\u51fd\u5f0f\u8868\u793a\u5f0f\u4f86\u300c\u5ef6\u9072\u904b\u7b97\u300d\uff0c\u4f7f\u5ba3\u544a\u5f0f\u8a9e\u610f\u8b8a\u70ba\u53ef\u884c\u3002<!--more-->\u5982\u540c\u4e0b\u9762\u5ba3\u544a\u5f0f\u8a9e\u610f\u7684\u7684\u7c21\u55ae\u7bc4\u4f8b\uff1a<\/p>\n<pre class=\"brush: java; gutter: true\">List&lt;MyResult&gt; result = myList.stream()\r\n    .filter(item -&gt; item.getScore() &gt; 60)\r\n    .map(item -&gt; new MyResult(item))\r\n    .collect(Collectors.toList());<\/pre>\n<p>\u4e0a\u9762\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5229\u7528 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Lambda_expression\">Lambda \u8868\u793a\u5f0f<\/a>\u628a\u7be9\u9078\u904e\u7684\u5143\u7d20\u9032\u884c\u6620\u5c04\u8655\u7406\uff0c\u518d\u6536\u96c6\u6210\u65b0\u7684 List \u5bb9\u5668\u3002Lambda \u8868\u793a\u5f0f\u662f\u4e00\u7a2e\u533f\u540d\u7684\u51fd\u5f0f\uff0c\u4e0a\u9762\u7684\u5beb\u6cd5\u770b\u8d77\u4f86\u5f88\u7c21\u55ae\u6613\u61c2\uff0c\u4f46\u771f\u6b63\u7684\u689d\u4ef6\u7be9\u9078\u548c\u6620\u5c04\u901a\u5e38\u6c92\u6709\u90a3\u9ebc\u7c21\u55ae\uff0c\u6240\u4ee5\u5c0d\u6bd4\u8f03\u8907\u96dc\u7684\u7be9\u9078\u6216\u6620\u5c04\u908f\u8f2f\uff0c\u901a\u5e38\u6703\u5beb\u6210\u5177\u540d\u7684<a href=\"https:\/\/en.wikipedia.org\/wiki\/Function_composition_(computer_science)\">\u51fd\u5f0f<\/a>\u3002\u9019\u6a23\u5beb\u6703\u6709\u5169\u500b\u597d\u8655\uff0c\u4e00\u4f86\u662f\u53ef\u8b80\u6027\u8f03\u9ad8\uff0c\u518d\u4f86\u5c31\u662f\u6703\u4fc3\u4f7f\u6211\u5011\u601d\u8003\u5177\u9ad4\u6f14\u7b97\u80cc\u5f8c\u7684\u62bd\u8c61\u6982\u5ff5\uff0c\u800c\u9019\u4e9b\u62bd\u8c61\u6982\u5ff5\u53ef\u4ee5\u5316\u6210\u4f01\u696d\u5143\u4ef6\u63d0\u4f9b\u5176\u5b83\u7a0b\u5f0f\u91cd\u8907\u4f7f\u7528\u4ee5\u53ca\u589e\u52a0\u7cfb\u7d71\u5f48\u6027\u3002\u6240\u4ee5\u7a0b\u5f0f\u78bc\u5c31\u6703\u6f14\u9032\u6210\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"brush: java; gutter: true\">{\r\n\r\n...\r\n\r\nList&lt;MyResult&gt; result = myList.stream()\r\n    .filter(this::isScoreGreatSixty)\r\n    .map(this::toResult)\r\n    .collect(Collectors.toList());\r\n\r\n...\r\n\r\n}\r\n\r\nprivate boolean isScoreGreatSixty(MyItem item) {\r\n    return item.getScore() &gt; 60;\r\n}\r\n\r\nprivate MyResult toResult(MyItem item) {\r\n    return new MyResult(item);\r\n}<\/pre>\n<p>\u5177\u540d\u51fd\u5f0f\u64c1\u6709\u548c\u4f7f\u7528\u5b83\u7684\u7a0b\u5f0f\u78bc\u5927\u534a\u7684\u8108\u7d61\uff0c\u9664\u4e86\u5340\u57df\u8b8a\u6578\u548c\u547c\u53eb\u5176\u5b83\u5143\u4ef6\u7684\u56de\u50b3\u503c\u4e4b\u5916\u3002\u5728\u9019\u7a2e\u72c0\u6cc1\u4e0b\uff0c\u6211\u5011\u5c31\u8981\u7528 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Function_object\">Function Object<\/a>\uff0c\u4e5f\u5c31\u662f\u5728 <a href=\"https:\/\/en.wikipedia.org\/wiki\/C%2B%2B11\">C0x STL<\/a> \u7a31\u70ba Functor \u7684\u76f8\u540c\u624b\u6cd5\uff0c\u5982\u540c\u4e0a\u9762\u7684\u7be9\u9078\u5668\uff0c\u6211\u5011\u5c07\u6703\u6539\u5beb\u6210\u4e0b\u9762\u7684\u9019\u500b\u6a23\u5b50\uff1a<\/p>\n<pre class=\"brush: java; gutter: true\">{\r\n\r\n...\r\n\r\nList result = myList.stream()\r\n    .filter(new ItemScoreGreatPredicate(60))\r\n    .map(this::toResult)\r\n    .collect(Collectors.toList());\r\n\r\n...\r\n\r\n}\r\n\r\nclass ItemScoreGreatPredicate implements Predicate&lt;MyItem&gt; {\r\n\r\n    private Integer targetNumber;\r\n\r\n    public ItemScoreGreatPredicate(Integer targetNumber) {\r\n        this.targetNumber = targetNumber;\r\n    }\r\n\r\n    public boolean test(MyItem item) {\r\n        return item.getScoreNumber) &gt; targetNumber;\r\n    }\r\n\r\n}<\/pre>\n<p>\u60f3\u5fc5\u9019\u6642\u4f60\u5df1\u7d93\u767c\u73fe\u5230\uff0cItemScoreGreatPredicate \u5df2\u7d93\u88ab\u53c3\u6578\u5316\uff0c\u4ee3\u8868\u5b83\u53ef\u4ee5\u91cd\u8986\u4f7f\u7528\u5728\u4e0d\u540c\u7684\u4f7f\u7528\u60c5\u5883\u4e0b\u3002\u4f46\u9019\u4e5f\u610f\u5473\u8981\u5171\u4eab\u547c\u53eb\u8005\u7684\u8108\u7d61\uff0c\u5fc5\u9808\u591a\u82b1\u8cbb\u4e00\u756a\u529f\u592b\uff0c\u5df2\u7d93\u4e0d\u5982 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Closure_(computer_programming)\">closure<\/a> \u822c\u81ea\u7531\uff0c\u7136\u800c\u9019\u662f\u76f8\u4f9d\u6027\u548c\u7368\u7acb\u6027\u7684\u53d6\u6368\u3002No <a href=\"https:\/\/en.wikipedia.org\/wiki\/Side_effect_(computer_science)\">side effect<\/a>s \u662f\u6cdb\u51fd\u7de8\u7a0b\u7684\u4e00\u500b\u91cd\u8981\u7279\u6027\uff0c\u4f46\u5728\u5ba3\u544a\u5f0f\u8a9e\u610f\u4e2d\uff0c\u8108\u7d61\u5247\u662f\u5ef6\u9072\u904b\u7b97\u4e0d\u53ef\u6216\u7f3a\u7684\u8981\u7d20\u3002<\/p>\n<p>\u5ef6\u9072\u904b\u7b97\u662f\u5ba3\u544a\u5f0f\u8a9e\u610f\u4e2d\uff0c\u7528\u4f86\u5c0d\u6cbb\u8907\u96dc\u6027\u7684\u5229\u5668\u3002\u5047\u5982\u6211\u5011\u8981\u89e3\u6c7a\u7684\u554f\u984c\uff0c\u4e26\u6c92\u8fa6\u6cd5\u55ae\u7368\u6bcf\u4e00\u5143\u7d20\u4ee5 item -&gt; toResult(item) \u7684\u65b9\u5f0f\u89e3\u6c7a\uff0c\u800c\u662f\u6bcf\u4e00\u500b\u5143\u7d20\u7684\u8a08\u7b97\u90fd\u6703\u548c\u5176\u5b83\u5143\u7d20\u626f\u4e0a\u95dc\u4fc2\uff0c\u800c\u4e14\u56e0\u61c9\u4e0d\u540c\u7684\u60c5\u5883\uff0c\u6703\u5b58\u5728\u4e0d\u540c\u7684\u6f14\u7b97\u7b56\u7565\u7684\u8a71\uff0c\u9019\u6642\u5019\u5c31\u8a72\u8f2a\u5230\u8108\u7d61\u7269\u4ef6\u4e0a\u5834\u4e86\uff0c\u5c31\u50cf\u4e0b\u9762\u9019\u4e00\u6bb5\u7a0b\u5f0f\u4e00\u6a23\u3002<\/p>\n<pre class=\"brush: java; gutter: true\">{\r\n\r\n...\r\nMyResultContext ctx = MyResultContext.create();\r\n\r\nmyList.stream()\r\n    .filter(new ItemScoreGreatPredicate(60))\r\n    .forEach(ctx::add);\r\n\r\nList&lt;MyResult&gt; result = ctx.getResult(myResultPolicyFactory);\r\n...\r\n\r\n}\r\n\r\nclass MyResultPolicy implements Function&lt;MyItem, MyResult&gt; {\r\n...\r\n\r\n}\r\n\r\nclass MyResultContext {\r\n\r\nprivate List&lt;MyItem&gt; items = new ArrayList&lt;&gt;();\r\n\r\npublic static MyResultContext create() {\r\n    return new MyResultContext();\r\n}\r\n\r\nprivate MyResultContext {\r\n}\r\n\r\npublic void add(MyItem item) {\r\n    items.add(item);\r\n}\r\n\r\npublic List&lt;MyResult&gt; getResult(\r\n    ResultPolicyFactory policyFactory) {\r\n    Function&lt;MyItem, MyResult&gt; policy = policyFactory.get(this);\r\n\r\n    return items.stream().map(policy);\r\n}\r\n\r\n}<\/pre>\n<p>\u5728\u9019\u88e1\u540c\u4eba\u53ea\u662f\u60f3\u7c21\u55ae\u8868\u9054<a href=\"https:\/\/en.wikipedia.org\/wiki\/Encapsulation_(computer_programming)\">\u5c01\u88dd<\/a>\u7b56\u7565\u6f14\u7b97\u6cd5\u7684\u89c0\u5ff5\uff0c\u6240\u4ee5\u5728\u5be6\u969b\u4e0a\uff0c\u4e0a\u9762\u7684\u5be6\u4f5c\u591a\u534a\u8981\u66f4\u8907\u96dc\u8a31\u591a\u3002\u5f9e\u9019\u500b\u7c21\u55ae\u7bc4\u4f8b\u53ef\u4ee5\u767c\u73fe Context \u6a23\u5f0f\u53ef\u4ee5\u8b93\u5ba3\u544a\u5f0f\u8a9e\u610f\u66f4\u6709\u5f48\u6027\uff0c\u4e0d\u904e\uff0c\u5c0d <a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">GOF \u8a2d\u8a08\u6a23\u5f0f<\/a>\u719f\u6089\u7684\u4eba\u61c9\u8a72\u6703\u767c\u73fe\uff0c\u9019\u4e0d\u904e\u662f <a href=\"https:\/\/en.wikipedia.org\/wiki\/Builder_pattern\">Builder<\/a> \u548c <a href=\"https:\/\/en.wikipedia.org\/wiki\/Strategy_pattern\">Strategy<\/a> \u6a23\u5f0f\u7684\u5ef6\u4f38\uff0c\u4e5f\u518d\u4e00\u6b21\u8b93\u6211\u5011\u5370\u8b49 FP \u642d\u914d <a href=\"https:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming\">OOP<\/a> \u7684\u5f37\u5927\u5a01\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5ef6\u9072\u904b\u7b97\u662f\u5ba3\u544a\u5f0f\u8a9e\u610f\u4e2d\uff0c\u7528\u4f86\u5c0d\u6cbb\u8907\u96dc\u6027\u7684\u5229\u5668\u3002\u5047\u5982\u6211\u5011\u8981\u89e3\u6c7a\u7684\u554f\u984c\uff0c\u4e26\u6c92\u8fa6\u6cd5\u55ae\u7368\u6bcf\u4e00\u5143\u7d20\u4ee5 item -&gt; toResult(item) \u7684\u65b9\u5f0f\u89e3\u6c7a\uff0c\u800c\u662f\u6bcf\u4e00\u500b\u5143\u7d20\u7684\u8a08\u7b97\u90fd\u6703\u548c\u5176\u5b83\u5143\u7d20\u626f\u4e0a\u95dc\u4fc2\uff0c\u800c\u4e14\u56e0\u61c9\u4e0d\u540c\u7684\u60c5\u5883\uff0c\u8981\u50b3\u5165\u4e0d\u540c\u7684\u6f14\u7b97\u7b56\u7565\u7684\u8a71\uff0c\u9019\u6642\u5019\u5c31\u8a72\u8108\u7d61\u7269\u4ef6\u4e0a\u5834 <a href=\"https:\/\/lifeparty.idv.tw\/blog\/archives\/11823\">\u95b1\u8b80\u5168\u6587 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,41,37,43,19,3],"tags":[],"class_list":["post-11823","post","type-post","status-publish","format-standard","hentry","category-29","category-thinking","category-37","category-43","category-19","category-3"],"_links":{"self":[{"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/posts\/11823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/comments?post=11823"}],"version-history":[{"count":38,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/posts\/11823\/revisions"}],"predecessor-version":[{"id":11857,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/posts\/11823\/revisions\/11857"}],"wp:attachment":[{"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/media?parent=11823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/categories?post=11823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lifeparty.idv.tw\/blog\/wp-json\/wp\/v2\/tags?post=11823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}