{"id":98,"date":"2025-01-15T06:24:00","date_gmt":"2025-01-15T06:24:00","guid":{"rendered":"https:\/\/boochlin.com\/?p=98"},"modified":"2026-09-19T18:04:25","modified_gmt":"2026-09-19T18:04:25","slug":"clean-code-%e7%84%a1%e6%9a%87%e7%9a%84%e7%a8%8b%e5%bc%8f%e7%a2%bc-%e6%9b%b8%e6%91%98%e5%bf%83%e5%be%97%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=98","title":{"rendered":"Clean code: \u7121\u7455\u7684\u7a0b\u5f0f\u78bc \u2013 \u66f8\u6458\u5fc3\u5f97\uff08\u4e00\uff09"},"content":{"rendered":"<p><img decoding=\"async\" src=\"..\/..\/assets\/images\/clean_code_cover.jpg\" alt=\"Clean Code \u66f8\u7c4d\u5c01\u9762\"><\/p>\n<pre><code class=\"hljs language-plaintext\">+-------------------------------------------------------------+\n|             THE ONLY VALID MEASUREMENT OF CODE QUALITY      |\n|                                                             |\n|    [Good Code Review Room]           [Bad Code Review Room] |\n|   \"WTF is that?\" (x2)               \"WTF! WTF!! WTFFFF!!!\"  |\n|    == 2 WTFs \/ min                   == 60 WTFs \/ min       |\n+-------------------------------------------------------------+<\/code><\/pre>\n<p>\u300e\u552f\u4e00\u6709\u6548\u7684\u7684\u300e\u7a0b\u5f0f\u54c1\u8cea\u300f\u5ea6\u91cf\u55ae\u4f4d\uff1a \u6bcf\u5206\u9418\u7f75\u9ad2\u8a71\u7684\u6b21\u6578(wtf\/minutes)\u300f<a href=\"http:\/\/www.osnews.com\/story\/19266\/WTFs_m\">\u7248\u6b0a\u7db2\u5740\u9644\u4e0a<\/a><\/p>\n<p>\u9019\u53e5\u8a71\u8b93\u6211\u60f3\u5230\u540c\u4e8b\u6539\u5176\u4ed6\u90e8\u9580\u7a0b\u5f0f\u78bc\u6642\u7684\u72c0\u6cc1\uff0c\u6211\u60f3\u4ee5\u6b64\u4f5c\u70ba\u5f15\u8a00\uff0c\u5df2\u7d93\u76f8\u7576\u8db3\u5920\u7684\u8868\u793a\u51fa \u5beb\u51faclean code \u7684\u91cd\u8981\u6027<\/p>\n<p>\uff08\u5e8f\uff09<\/p>\n<p>\u8a8d\u771f\u4e00\u9ede\u7684\u8a71\u5c31\u662f\uff1a<\/p>\n<p>\u300e\u4f60\u6709\u5169\u500b\u539f\u56e0\u4f86\u8b80\u9019\u672c\u66f8\uff1a\u9996\u5148\u4f60\u662f\u500b\u7a0b\u5f0f\u8a2d\u8a08\u5e2b\uff0c\u63a5\u8005\uff0c\u4f60\u60f3\u6210\u70ba\u4e00\u500b\u66f4\u597d\u7684\u8a2d\u8a08\u5e2b\u300f<\/p>\n<p>\u6211\u81ea\u5df1\u52a0\u4e0a\u53bb\u7684\uff1a\u300e\u525b\u597d\uff0c\u597d\u7684\u8a2d\u8a08\u5e2b\u85aa\u6c34\u597d\u50cf\u9084\u80fd\u770b\u300f<\/p>\n<p>\uff08p1\uff09<\/p>\n<p>\u9019\u88e1\u5077\u5077\u5de5\u5546\u6642\u9593\u4e00\u4e0b\uff0c\u5c0f\u5f1f\u5beb\u4e86\u4e00\u500b\u7c21\u55ae\u7684 app <a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.heaton.funnyvote\">\u8da3\u6295\u7968<\/a>\u5e0c\u671b\u5927\u5bb6\u6709\u7a7a\u53ef\u4ee5\u5e6b\u5fd9\u73a9\u4e00\u4e0b\u3002\u958b\u767c\u5c0f\u6545\u4e8b<a href=\"https:\/\/boochlin.com\/?p=289\">Funny Vote \u958b\u767c\u96dc\u8a18<\/a><\/p>\n<p>\u66f8\u6458\u5fc3\u5f97\uff1a<\/p>\n<h2>\u7b2c\u4e00\u7ae0\uff1a\u7121\u7455\u7684\u7a0b\u5f0f\u78bc<\/h2>\n<p>\u627f\u63a5&lt;\u5e8f&gt;\u7684\u90e8\u4efd\uff0c\u70ba\u4f55\u6211\u5011\u9700\u8981\u5beb\u51fa clean code:<\/p>\n<p>\u300e\u5728\u5927\u90e8\u4efd\u7684\u60c5\u6cc1\u4e0b\uff0c\u5982\u679c\u628a\u81ea\u5df1\u7684 coding \u904e\u7a0b\u7d66\u9304\u4e0b\u4f86\u7684\u8a71\uff0c\u6703\u767c\u73fe\u4ee5\u4e0b\u884c\u70ba<\/p>\n<p>Heaton \u6253\u958b\u7a0b\u5f0f\u78bc<\/p>\n<p>\u627e\u5230\u8981\u4fee\u6539\u7684\u51fd\u5f0f\uff0c\u958b\u59cb\u60f3\u60f3\u8981\u6539\u5565<\/p>\n<p>\u5411\u4e0a\u6372\uff0c\u6aa2\u67e5\u51fd\u6578\u7684\u521d\u59cb\u72c0\u6cc1<\/p>\n<p>\u5411\u4e0b\u5377\u56de\u525b\u525b\u7684\u51fd\u5f0f\uff0c\u958b\u59cb\u6539\u4e00\u6539<\/p>\n<p>\u963f\uff0c\u53c8\u522a\u6389\u525b\u525b\u7684\u7a0b\u5f0f\u78bc\uff0c\u518d\u8f38\u5165\u4e00\u6b21\uff0c\u518d\u4e00\u6b21\u522a\u6389<\/p>\n<p>\u767c\u73fe\u8ddf\u5176\u4ed6\u51fd\u5f0f\u6709\u95dc\u4fc2\uff0c\u6372\u5230\u525b\u525b\u7684\u51fd\u6578\uff0c\u770b\u4e86\u4e00\u4e0b\u8981\u600e\u67d0\u547c\u53eb<\/p>\n<p>\u56de\u5230\u525b\u525b\u7684\u51fd\u5f0f\uff0c\u91cd\u65b0\u4fee\u6539\uff0c\u767c\u73fe\u6253\u932f\uff0c\u518d\u522a\u6389\uff0c\u53c8\u589e\u52a0\u300f<\/p>\n<p>\uff08p.15\uff09<\/p>\n<p>\u524d\u9762\u9019\u4e00\u6bb5\u8a71\u5b8c\u5168\u53ef\u4ee5\u7528\u500b\u8ff4\u5708\u7d66\u5305\u8d77\u4f86\uff0c\u56e0\u70ba\u53ea\u6703\u4e0d\u65b7\u5730\u91cd\u8907<\/p>\n<p>\u4ee5\u9019\u6a23\u7684\u8a18\u9304\u4f86\u770b\uff0c\u5176\u5be6\u82b1\u8cbb\u5728\u95b1\u8b80\u7a0b\u5f0f\u8ddf\u64b0\u5beb\u7a0b\u5f0f\u7684\u6642\u9593\u5927\u6982\u662f\uff11\uff10\uff1a\uff11<\/p>\n<p>\u6240\u4ee5\u8aaa\u5982\u679c\u628a\u6280\u80fd\u9ede\u6578\u9ede\u5728\u95b1\u8b80\u7a0b\u5f0f\u7684\u6280\u80fd\u4e0a\uff0c\u6703\u6bd4\u9ede\u5728\u5beb\u7a0b\u5f0f\u9019\u6280\u80fd<\/p>\n<p> \u5728\u958b\u767c\u4e0a\uff0c\u6548\u76ca\u597d\u5f88\u591a\uff0c\u6240\u4ee5\u4e00\u958b\u59cb\u5c31\u628acode \u5beb\u5f97\u80fd\u770b\uff0c\u4fdd\u6301\u6574\u6f54\uff0c\u5c0d\u65bc\u4e4b\u5f8c\u7684\u958b\u767c\u7d55\u5c0d\u662f\uff11\uff10\u500d\u6578\u7684\u5f71\u97ff<\/p>\n<h2>\u7b2c\u4e8c\u7ae0\uff1a\u6709\u610f\u7fa9\u7684\u547d\u540d<\/h2>\n<p>\u8981\u6709\u610f\u7fa9\u7684\u5340\u5206<\/p>\n<p>\u8acb\u554f\u4ee5\u4e0b\u4e09\u500b\u51fd\u6578\u8981\u505a\u7684\u4e8b\u60c5\u4f60\u5206\u7684\u51fa\u4f86\u55ce\uff1f<\/p>\n<ol>\n<li>\n<p>getActitiveAccount();<\/p>\n<\/li>\n<li>\n<p>getActitiveAccounts();<\/p>\n<\/li>\n<li>\n<p>getActitiveAccountInfo();<\/p>\n<\/li>\n<\/ol>\n<p>\u8acb\u76e1\u91cf\u907f\u514d\u9019\u6a23\u66d6\u6627\u7684\u547d\u540d<\/p>\n<p>\uff08p.23\uff09<\/p>\n<h3>\u6210\u54e1\u7684\u5b57\u9996\uff1a<\/h3>\n<h4>2.1 \u00a0\u5168\u57df\u8b8a\u6578:<\/h4>\n<p>\u5f88\u591a\u60c5\u6cc1\u4e0b\uff0c\u6703\u5728\u5168\u57df\u8b8a\u6578\u7684\u524d\u9762\u52a0\u4e0a m \u6216\u8005 m_ \u9019\u4e00\u985e\u7684\u958b\u982d\uff0c\u4f86\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u52a0\u5bb9\u6613\u7684\u4fbf\u662f\u6b64\u8b8a\u6578\uff0c\u4f46\u5be6\u969b\u4e0a\u9019\u4e26\u6c92\u6709\u592a\u6709\u610f\u7fa9\uff0c\u5927\u90e8\u5206\u7684\u4eba\u9084\u662f\u53ea\u6703\u95b1\u8b80 m \u4e4b\u5f8c\u6709\u610f\u7fa9\u7684\u547d\u540d\uff0c\u66f4\u4f55\u6cc1\u73fe\u5728\u7684ide \u90fd\u5df2\u7d93\u9032\u5316\u5230\uff0c\u53ef\u4ee5\u4e3b\u52d5\u628a \u5168\u57df\u8b8a\u6578\u81ea\u52d5\u63db\u8272 \u6240\u4ee5 m \u9019\u6a23\u7684\u5b57\u9996\u63d0\u793a\u5c31\u8b8a\u5f97\u53ea\u6703\u5e72\u64fe<\/p>\n<p>\uff08p27\uff09<\/p>\n<p>\u672c\u4eba m \u7684\u7fd2\u6163\u7528\u6cd5\u5230\u73fe\u5728\u9084\u662f\u4e0d\u592a\u6703\u6539\uff0c\u9019\u5be6\u5728\u6709\u9ede\u898b\u4ec1\u898b\u667a<\/p>\n<h4>2.2 \u00a0\u4ecb\u9762\u548c\u5be6\u4f5c:<\/h4>\n<p>\u4ecb\u9762\u7684\u7684\u5ba3\u544a\u6642\uff0c\u4e0d\u5c11 open source project \u90fd\u6703\u7279\u5730\u52a0\u4e0a I \u9019\u6a23\u7684\u958b\u982d\uff0c\u4f8b\u5982 IImageBackView\uff0c\u85c9\u6b64\u544a\u77e5\u958b\u767c\u8005\u9019\u662f\u4e00\u500b\u4ecb\u9762\uff0c\u4f46\u9019\u500b I \u540c\u6a23\u4e5f\u53ea\u6709\u8b93\u4eba\u5206\u5fc3\u7684\u529f\u80fd\uff0c\u958b\u767c\u8005\u6839\u672c\u4e0d\u5728\u610f\u662f\u4e0d\u662f\u4ecb\u9762\uff0c\u800c\u662f\u5728\u610f\u9019 ImageBackView \u5230\u5e95\u662f\u600e\u6a23\u7684 view\uff0c\u5982\u679c\u771f\u7684\u60f3\u8981\u5340\u5206 interface \u9084\u662f implement \uff0c\u90a3\u6703\u6bd4\u8f03\u5efa\u8b70\u5728\u5be6\u4f5c\u7aef\u6539\u6210 ImageBackViewImpl\u3002<\/p>\n<p>(p28)<\/p>\n<h4>2.3 \u6bcf\u7a2e\u6982\u5ff5\u4f7f\u7528\u4e00\u7a2e\u5b57\u8a5e\uff1a<\/h4>\n<p>\u5728\u540c\u4e00\u500b\u5c08\u6848\u6642\uff0c\u76e1\u53ef\u80fd\u7684\u5728\u67d0\u4e9b\u95dc\u9375\u5b57\u4fdd\u6301\u4e00\u81f4\uff0c\u4f8b\u5982\u5728\u53d6\u5f97\u8cc7\u6599\u6642\u51fd\u5f0f\u547d\u540d fetchXXX , getXXX , retrieveXXX \u9019\u5e7e\u500b\u958b\u982d\uff0c\u8981\u5340\u5206\u958b\u4f86\uff0c\u6216\u662f\u7d71\u4e00\u53ea\u7528\u4e00\u7a2e\uff0c\u7dad\u6301\u4e00\u81f4\u6027\uff0c\u8b93\u958b\u767c\u8005\u5728 search method \u6642\u53ef\u4ee5\u66f4\u5feb\u5730\u627e\u51fa\u95dc\u9375\u5b57\u3002<\/p>\n<p>\u540c\u6a23\u7684\u7528\u5728 class \u547d\u540d\uff0c\u4e00\u500b\u5c08\u6848\u88e1\u540c\u6642\u6709 XXXController , XXXManager , XXXDriver \u5728\uff0c\u5c0d\u65bc\u958b\u767c\u8005\u4e5f\u53ea\u6703\u611f\u5230\u56f0\u60d1\uff0c\u9019\u5e7e\u7a2e\u5230\u5e95\u5dee\u5728\u54ea\u88e1\uff0c\u5982\u679c\u6c92\u6709\u5dee\u5230\u5929\u7ffb\u5730\u9060\uff0c\u7d71\u4e00\u8d77\u4f86\u53ea\u6703\u6709\u597d\u8655\uff0c\u9019\u88e1\u53ef\u4ee5\u914d\u5408\u6b63\u5728\u7528\u7684 design pattern \u7fd2\u6163\u7528\u540d\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u9032\u5165\u72c0\u6cc1\u3002<\/p>\n<p>\uff08p.30\uff09<\/p>\n<p>\u500b\u4eba\u5728home \u958b\u767c\u6642\uff0c\u6211\u5c31\u5e38\u5e38\u540c\u6642\u4f7f\u7528 controller \u548c manager \u6df7\u7528\uff0c\u5f8c\u4f86\u89ba\u5f97\u597d\u50cf\u8b8a\u5f97\u4e0d\u597d\u8ddf\u8001\u5927\u8aaa\u660e\u54ea\u500b\u6771\u897f\u653e\u5728\u54ea\u500b class \uff0c\u90a3\u4e7e\u8106\u901a\u901a\u7528 manager \uff0c\u9019\u6a23\u6e9d\u901a\u6642\u4e5f\u65b9\u4fbf\u591a\u4e86\u3002<\/p>\n<h2>\u7b2c\u4e09\u7ae0\uff1a\u51fd\u5f0f<\/h2>\n<p>\u7c21\u77ed\uff0c\u53ea\u505a\u4e00\u4ef6\u4e8b<\/p>\n<p>\u4e00\u6bb5\u7a0b\u5f0f\u78bc\uff0c\u5047\u8a2d\u505a\u4e86\u4e09\u4ef6\u4e8b\u60c5\uff0c\u6700\u597d\u628a\u9019\u4e09\u4ef6\u4e8b\u60c5\u5206\u958b\u7368\u7acb\u6210\u4e09\u500b\u51fd\u5f0f\uff0c\u7136\u5f8c\u7d66\u4e88\u6709\u610f\u7fa9\u7684\u547d\u540d\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u5feb\u7406\u89e3\u7a0b\u5f0f\u78bc<\/p>\n<p>\u4ee5 home \u7684 BitmapVolleyManager \u7684\u90e8\u5206\u7a0b\u5f0f\u78bc\u4f86\u770b\uff08\u5982\u4e0b\uff09\uff0c\u4ee5\u4e0b load bitmap \u505a\u4e86\u4e09\u4ef6\u4e8b\u60c5<\/p>\n<ol>\n<li>\n<p>set view loading url and check url is valid.<\/p>\n<\/li>\n<li>\n<p>get bitmap from image cache.<\/p>\n<\/li>\n<li>\n<p>assign bitmap worker task for this view and url<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">synchronized<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title function_\">loadBitmap<\/span><span class=\"hljs-params\">(String imageUri, ImageBackView imageView\n            , BitmapConfiguration configuration, <span class=\"hljs-type\">boolean<\/span> stackKeep, <span class=\"hljs-type\">boolean<\/span> clearFirst)<\/span> {\n\n        imageView.setKey(imageUri);\n        <span class=\"hljs-keyword\">if<\/span> (clearFirst)\n            imageView.setBitmap(<span class=\"hljs-literal\">null<\/span>);\n        <span class=\"hljs-keyword\">if<\/span> (!Utils.isValid(imageUri)) {\n            Log.d(<span class=\"hljs-string\">&quot;BitmapVolleyManager&quot;<\/span>, <span class=\"hljs-string\">&quot;loadBitmap, image uri should not be null, cancel loading&quot;<\/span>);\n            <span class=\"hljs-keyword\">return<\/span>;\n        }\n\n        <span class=\"hljs-type\">Bitmap<\/span> <span class=\"hljs-variable\">bitmap<\/span> <span class=\"hljs-operator\">=<\/span> VolleyManager.getPageImageCache(mContext)\n                .getBitmap(ImageLoader.getCacheKey(imageUri, configuration.requestWidth\n                        , configuration.requestHeight, configuration.mScaleType));\n        <span class=\"hljs-keyword\">if<\/span> (bitmap != <span class=\"hljs-literal\">null<\/span>) {\n            imageView.setBitmap(bitmap);\n            <span class=\"hljs-keyword\">return<\/span>;\n        }\n        <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-type\">BitmapWorkerTask<\/span> <span class=\"hljs-variable\">task<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">BitmapWorkerTask<\/span>(imageView, imageUri, configuration);\n        <span class=\"hljs-keyword\">if<\/span> (stackKeep) {\n            <span class=\"hljs-keyword\">synchronized<\/span> (<span class=\"hljs-built_in\">this<\/span>) {\n                mStackLoadTask.push(task);\n            }\n        } <span class=\"hljs-keyword\">else<\/span> {\n            Log.d(<span class=\"hljs-string\">&quot;BitmapVolleyManager&quot;<\/span>, <span class=\"hljs-string\">&quot;Add a Bitmap task ,image uri:&quot;<\/span> + imageUri);\n            task.executeOnExecutor(getExecutorPool());\n        }\n    }<\/code><\/pre>\n<p>\u76f4\u63a5\u4f86\u770b\u4e5f\u4e26\u4e0d\u96e3\u61c2\uff0c\u4f46\u662f\u66f4\u9032\u4e00\u6b65\u4f86\u770b\u6216\u8a31\u53ef\u4ee5\u66f4\u52a0\u7368\u7acb\u6210<\/p>\n<pre><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">synchronized<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title function_\">loadBitmap<\/span><span class=\"hljs-params\">(String imageUri, ImageBackView imageView\n            , BitmapConfiguration configuration, <span class=\"hljs-type\">boolean<\/span> stackKeep, <span class=\"hljs-type\">boolean<\/span> clearFirst)<\/span> {\n\n        checkViewForURL(imageuri, imageview);\n        \n        <span class=\"hljs-keyword\">if<\/span> (clearFirst)\n            imageView.setBitmap(<span class=\"hljs-literal\">null<\/span>);\n\n        <span class=\"hljs-type\">Bitmap<\/span> <span class=\"hljs-variable\">bitmap<\/span> <span class=\"hljs-operator\">=<\/span> getBitmapFromCache(imageUri, configuration);\n        <span class=\"hljs-keyword\">if<\/span> (bitmap != <span class=\"hljs-literal\">null<\/span>) {\n            imageView.setBitmap(bitmap);\n            <span class=\"hljs-keyword\">return<\/span>;\n        }\n        startNetworkBitmapLoadingTask(imageUri, imageView , configuration , stackkeep);     \n    }\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">boolean<\/span> <span class=\"hljs-title function_\">checkViewURLIsValid<\/span><span class=\"hljs-params\">(String targetURL, ImageBackView imageView)<\/span> {\n    imageView.setKey(imageUri);\n    <span class=\"hljs-keyword\">if<\/span> (!Utils.isValid(imageUri)) {\n       Log.d(<span class=\"hljs-string\">&quot;BitmapVolleyManager&quot;<\/span>, <span class=\"hljs-string\">&quot;loadBitmap, image uri should not be null, cancel loading&quot;<\/span>);\n       <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">false<\/span>;\n       }\n    } <span class=\"hljs-keyword\">else<\/span> {\n       <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">true<\/span>;\n    }\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">Bitmap<\/span> <span class=\"hljs-title function_\">getBitmapFromCache<\/span><span class=\"hljs-params\">(String targetURL , BitmapConfiguration configuration)<\/span> {\n    <span class=\"hljs-type\">Bitmap<\/span> <span class=\"hljs-variable\">bitmap<\/span> <span class=\"hljs-operator\">=<\/span> VolleyManager.getPageImageCache(mContext)\n                .getBitmap(ImageLoader.getCacheKey(imageUri, configuration.requestWidth\n                        , configuration.requestHeight, configuration.mScaleType));\n    <span class=\"hljs-keyword\">return<\/span> bitmap;\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title function_\">startNetworkBitmapLoadingTask<\/span><span class=\"hljs-params\">(String targetURL , ImageBackView imageView , BitmapConfiguration configuration\n    , <span class=\"hljs-type\">boolean<\/span> stackKeep)<\/span> {\n    <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-type\">BitmapWorkerTask<\/span> <span class=\"hljs-variable\">task<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">BitmapWorkerTask<\/span>(imageView, targetURL, configuration);\n        <span class=\"hljs-keyword\">if<\/span> (stackKeep) {\n            <span class=\"hljs-keyword\">synchronized<\/span> (<span class=\"hljs-built_in\">this<\/span>) {\n                mStackLoadTask.push(task);\n            }\n        } <span class=\"hljs-keyword\">else<\/span> {\n            Log.d(<span class=\"hljs-string\">&quot;BitmapVolleyManager&quot;<\/span>, <span class=\"hljs-string\">&quot;Add a Bitmap task ,image uri:&quot;<\/span> + targetURL);\n            task.executeOnExecutor(getExecutorPool());\n        }\n}<\/code><\/pre>\n<p>\u597d\u5427\uff0c\u81f3\u5c11\u7b2c\u4e00\u968e\u6bb5\u7684\u539f\u5247\u5df2\u7d93\u4fee\u6b63\u5b8c\u6210\u4e86\uff08\u96d6\u7136\u9084\u662f\u9055\u53cd\u5f88\u591a\u4e4b\u5f8c\u6703\u8ac7\u5230\u7684\u539f\u5247\uff0c\u63d0\u5230\u6642\u518d\u6539\uff09<\/p>\n<p>(p40.41)<\/p>\n<h3>3.1 switch \u6558\u8ff0\uff1a<\/h3>\n<p>\u8981 switch \u7c21\u77ed\uff0c\u4e26\u4e0d\u5bb9\u6613\uff0c\u56e0\u70baswitch \u7e3d\u662f\u6703\u9055\u53cd\u300e\u55ae\u4e00\u539f\u5247\uff08single responsibility principle\uff09\u300f<\/p>\n<p> \u548c \u300e\u958b\u653e\u9589\u548c\u539f\u5247\uff08open closed principle\uff09\u300f\uff0c\u8a73\u7d30\u7a0b\u5f0f\u78bc\u7684\u7bc4\u4f8b\u9084\u662f\u770b\u66f8\u6bd4\u8f03\u597d\u3002<\/p>\n<p>\u7c21\u55ae\u4f86\u8aaa\u5c31\u662f switch \u7684\u7528\u6cd5\u5f80\u5f80\u7d81\u5b9a\u7279\u6b8a\u503c\uff0c\u4f8b\u5982 enum \u00a0, \u9019\u6a23\u5728\u8981\u64f4\u589e\u7684\u6642\u5019\uff0c\u52e2\u5fc5\u8981\u5230\u8655\u4fee\u6539\u7528\u5230\u7684\u5730\u65b9\u3002\u53ef\u662f\u66f8\u4e2d\u4e5f\u63d0\u5230\u9019\u662f\u5fc5\u8981\u4e4b\u60e1\uff0c\u5982\u679c\u53ef\u4ee5\u7684\u8a71\uff0c\u6700\u597d\u4e00\u5927\u6bb5\u7a0b\u5f0f\u78bc\uff0c\u5c31\u53ea\u6709\u4e00\u500bswitch \u5c31\u597d\uff0c\u5269\u4e0b\u7684\u7528\u591a\u578b\u7d66\u8655\u7406\u6389\u3002<\/p>\n<p>\uff08p.44\uff09<\/p>\n<h3>3.2 \u51fd\u5f0f\u7684\u53c3\u6578\uff1a<\/h3>\n<p>\u51fd\u5f0f\u7684\u6700\u4f73\u53c3\u6578\u6578\u91cf\uff0c\u6700\u7406\u60f3\u7684\u662f\u96f6\u500b\uff0c\u5176\u6b21\u662f\u4e00\u500b\uff0c\u63a5\u4e0b\u4f86\u662f\u5169\u500b\uff0c\u597d\u5427\u4e09\u500b\u662f\u6975\u9650\uff0c\u5225\u518d\u4e0a\u53bb\u4e86\uff0c\u56e0\u70ba\u53c3\u6578\u672c\u8eab\u4e5f\u662f\u4e00\u7a2e\u6982\u5ff5\u7684\u50b3\u9054\u3002<\/p>\n<p>\u4e0b\u9762\u5169\u500b\u51fd\u5f0f\u54ea\u500b\u6bd4\u8f03\u597d\u7406\u89e3<\/p>\n<p>includeSetupPageInfo(newPageContent);<\/p>\n<p>includeSetupPage();<\/p>\n<p>\u57fa\u672c\u4e0a\u53c3\u6578\u548c\u51fd\u5f0f\u540d\u7a31\u662f\u5c6c\u65bc\u4e0d\u540c\u5206\u5340\uff0c\u7406\u89e3\u4e0a\u662f\u6703\u6bd4\u8f03\u56f0\u96e3\u4e00\u4e9b\uff0c\u7121\u6cd5\u4e00\u773c\u5c31\u5feb\u901f\u91d0\u6e05\uff0c\u53c3\u6578\u548c\u51fd\u5f0f\u7684\u95dc\u4fc2\uff0c\u800c\u4e14\u53c3\u6578\u6703\u5f37\u8feb\u958b\u767c\u8005\u53bb\u7406\u89e3\u4e0d\u5fc5\u8981\u7684\u8cc7\u8a0a\uff0c\u56e0\u70ba\u958b\u767c\u8005\u6703\u53bb\u64d4\u5fc3\u9019\u53c3\u6578\u5230\u5e95\u6703\u5f71\u97ff\u5230\u5565\uff0c\u53c8\u8a72\u600e\u67d0\u8655\u7406\uff0c\u6b64\u5916\uff0c\u53c3\u6578\u8d8a\u591a\uff0c\u6e2c\u8a66\u7684\u8907\u96dc\u5ea6\u66f4\u6703\u6307\u6578\u578b\u4e0a\u5347\u3002<\/p>\n<p>(p.47)<\/p>\n<p>\u8f38\u51fa\u578b\u53c3\u6578\u7684\u8a2d\u8a08\u66f4\u662f\u4e00\u5927\u96e3\u4ee5\u7406\u89e3\u9ede\uff0c\u5927\u90e8\u5206\u958b\u767c\u8005\u5c0d\u65bc\u51fd\u5f0f\u7684\u7406\u89e3\u901a\u5e38\u90fd\u662f\u5f9e\u53c3\u6578\u8f38\u5165\u8cc7\u6599\uff0c\u7136\u5f8c\u518dreturn \u88e1\u5f97\u5230\u8f38\u51fa\u8cc7\u6599\u3002\u4e00\u822c\u60c5\u6cc1\u4e0b\u6839\u672c\u4e0d\u6703\u60f3\u5230\u6703\u7528\u653e\u5728\u53c3\u6578\u7684\u8cc7\u6599\u505a\u70ba\u50b3\u905e\u7684\u5de5\u5177\u3002<\/p>\n<p>\uff08\u672c\u4eba\u9019\u9ede\u6df1\u611f\u75db\u82e6\uff0c\u8001\u662f\u6709\u4eba\u628a list \u653e\u9032\u53c3\u6578\u88e1\uff0c\u7136\u5f8c\u76f4\u63a5\u5c0d list \u64cd\u4f5c\uff0c\u4e00\u822c\u6211\u5728 trace \u90fd\u6703\u611f\u5230\u56f0\u60d1\uff0c\u60f3\u8aaa\u5565\u6642\u9019list \u88ab\u6539\u904e\u4e86\uff09<\/p>\n<pre><code class=\"hljs language-plaintext\">List output = sortByTime(input);\n\u5e95\u4e0b\u7684\u5beb\u6cd5\u771f\u7684\u5f88\u5bb9\u6613\u56f0\u60d1\nsortByTime(input);<\/code><\/pre>\n<p>(p.52)<\/p>\n<h4>3.2.1 \u55ae\u4e00\u7684\u53c3\u6578<\/h4>\n<p>\u9019\u4e00\u5c0f\u6bb5\uff0c\u4f5c\u8005\u5341\u5206\u53cd\u5c0d\u4f7f\u7528 flag \u4f5c\u70ba\u53c3\u6578<\/p>\n<p>\u4e3b\u56e0\u70ba boolean is true \u6703\u505a\u7b2c\u4e00\u7a2e\u4e8b\uff0c\u90a3 false \u5c31\u6703\u505a\u7b2c\u4e8c\u7a2e\u4e8b<\/p>\n<p>\u5f88\u660e\u986f\u5730\u9055\u53cd\u55ae\u4e00\u539f\u5247\uff0c\u4ee5\u4e0a\u9762\u7684\u4f8b\u5b50\u5c31\u662f<\/p>\n<pre><code class=\"hljs language-plaintext\">public synchronized void loadBitmap(String imageUri, ImageBackView imageView\n            , BitmapConfiguration configuration, boolean stackKeep, boolean clearFirst)<\/code><\/pre>\n<p>\u9019\u88e1\u9762\u7684 stackKeep , clearFirst<\/p>\n<p>\u9019\u5169\u500b\u53c3\u6578\u5206\u5225\u662f\uff0c\u8b80\u53d6\u5716\u7247\u4efb\u52d9\uff0c\u5148\u653e\u5728stack \u7b49\u5f85\u5176\u4ed6\u6642\u9593\uff0c\u5728\u958b\u59cb\u555f\u52d5\u4efb\u52d9\u548c \u5148\u6e05\u9664 ImageView \u4e0a\u9762\u7684\u5716\u7247\uff0c\u5728\u8f09\u5165\u5716\u7247<\/p>\n<p>\u8001\u5be6\u8aaa\u53ea\u770b\u9019\u53c3\u6578\u540d\u7a31\u5e7e\u4e4e\u4e0d\u61c2\u5565\u610f\u7fa9<\/p>\n<p>(p.48)<\/p>\n<p>\uff08\u4e0d\u904e\u6211\u500b\u4eba\u4e26\u4e0d\u662f\u90a3\u67d0\u53cd\u5c0d\u55ae\u4e00 boolean \u53c3\u6578\u7684\u8a2d\u8a08\uff0c\u56e0\u70ba\u4efb\u4f55\u5f48\u6027\u7684\u8a2d\u8a08\u518d\u591a\u5f62\u7684\u57fa\u790e\u4e0a<\/p>\n<p>\u7e3d\u6703\u6709\u500b method \u662f\u6700\u5927\u53c3\u6578\u96c6\u5408\uff0c\u5982\u4e0b\uff0c\u56e0\u6b64\u53ea\u8981\u7279\u5730\u628a\u53c3\u6578\u9084\u6709\u51fd\u5f0f\u529f\u80fd\u7684\u8a3b\u89e3\u5beb\u597d\u5c31\u53ef\u4ee5\u4e86\uff0c\u4e0d\u904e\u4e4b\u5f8c\u7684\u7ae0\u7bc0\u4e5f\u6703\u63d0\u5230\u4f5c\u8005\u4e26\u4e0d\u559c\u6b61\u8a3b\u89e3\uff09<\/p>\n<pre><code class=\"hljs language-java\"><span class=\"hljs-comment\">\/**\n    \n    **\/<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title function_\">loadBitmap<\/span><span class=\"hljs-params\">(String imageUri, ImageBackView imageView)<\/span> {\n        loadBitmap(imageUri, imageView, <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">BitmapConfiguration<\/span>(DEFAULT_LEFT_ITEM_WIDTH, DEFAULT_LEFT_ITEM_HEIGHT), <span class=\"hljs-literal\">false<\/span>, <span class=\"hljs-literal\">true<\/span>);\n    }\n\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title function_\">loadBitmapStackKeep<\/span><span class=\"hljs-params\">(String imageUri, ImageBackView imageView, BitmapConfiguration configuration)<\/span> {\n        loadBitmap(imageUri, imageView, configuration, <span class=\"hljs-literal\">true<\/span>, <span class=\"hljs-literal\">true<\/span>);\n    }\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">synchronized<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title function_\">loadBitmap<\/span><span class=\"hljs-params\">(String imageUri, ImageBackView imageView\n            , BitmapConfiguration configuration, <span class=\"hljs-type\">boolean<\/span> stackKeep, <span class=\"hljs-type\">boolean<\/span> clearFirst)<\/span> {<\/code><\/pre>\n<h4>3.2.2 \u7269\u4ef6\u578b\u614b\u7684\u53c3\u6578<\/h4>\n<p>\u8d85\u904e\u5169\u500b\u4e09\u500b\u7684\u53c3\u6578\u6642\uff0c\u5f88\u53ef\u80fd\u9700\u8981\u5c07\u5176\u4e2d\u5e7e\u500b\u7d44\u5408\u6210\u4e00\u500b\u985e\u5225\uff0c\u770b\u8d77\u4f86\u5f88\u50cf\u53ea\u662f\u898f\u907f\u53c3\u6578\u6578\u91cf\u76e1\u91cf\u5c11\u7684\u539f\u5247<\/p>\n<p> \u5176\u5be6\u4e26\u4e0d\uff0c\u9019\u5b8c\u5168\u53ef\u4ee5\u5e6b\u52a9\u958b\u6cd5\u8005\u53bb\u7406\u89e3\u53c3\u6578\u7684\u7406\u5ff5\uff0c\u4ee5\u4e0a\u9762\u4f8b\u5b50\u4f86\u770b \u5c31\u662f BitmapConfiguration \u88e1\u9762\u5305\u542b\u5bec\u9ad8\uff0c\u7e2e\u653e\u985e\u578b<\/p>\n<p>\u6216\u8a31\u53ef\u4ee5\u518d\u628a stack keep \u548c clearfirst \u52a0\u9032\u53bb\uff0c\u4e0d\u904e\u500b\u4eba\u89ba\u5f97\u52a0\u9032\u53bb\u7684\u8a71\u5c31\u4e0d\u80fd\u7528BitmapConfiguration \u800c\u662f\u4f7f\u7528<\/p>\n<p>LoadConfiguration \u6703\u6bd4\u8f03\u597d\uff0c\u7136\u5f8c\u5305\u542bBitmapConfiguration and stackkeep and clearfirst.<\/p>\n<p>(\u53ef\u662f\u8001\u6a23\u5b50\uff0c\u6211\u89ba\u5f97\u9019\u6709\u9ede\u898b\u4ec1\u898b\u667a)<\/p>\n<p>book example:<\/p>\n<pre><code class=\"hljs language-plaintext\">Circle makeCircle(double x , double y , double radius);\nCircle makeCircle(Point center , double radius);<\/code><\/pre>\n<p>(p.50)<\/p>\n<h3>3.3 \u4f7f\u7528\u4f8b\u5916\u8655\u7406\u4ee3\u66ff\u56de\u50b3\u932f\u8aa4\u78bc<\/h3>\n<p>\u4f7f\u7528try\/catch\uff0c\u4e26\u5728 catch \u628a\u5f8c\u7e8c\u8655\u7406\u5b8c\u6210\uff0c\u800c\u4e0d\u8981\u7528\u56de\u50b3\u932f\u8aa4\u78bc\u7684\u65b9\u5f0f\uff0c\u9019\u4ee3\u8868\u8005\u4e8b\u60c5\u5f80\u5f8c\u9762\u4e1f\uff0c\u5f62\u6210\u66f4\u6df1\u5c64\u7684\u5de2\u72c0\u7d50\u69cb\uff0c\u5c24\u5176\u662f\u81ea\u5b9a\u7fa9\u7684 enum error code.<\/p>\n<h3>3.4 \u628a try\/catch \u5340\u584a\u63d0\u53d6\u51fa\u4f86<\/h3>\n<p>\u5728 try\/catch \u5340\u584a\u5167\u6700\u597d\u5c31\u53ea\u6709\u4e00\u884c\u51fd\u5f0f\uff0c\u7136\u5f8c\u628acode\u5beb\u5728\u51fd\u5f0f\u88e1<\/p>\n<p>\uff08p.54\uff09<\/p>\n<p>\u5c11\u7528 break , continue\uff0c\u4fdd\u6301\u6548\u7387\u53ef\u4ee5\u659f\u914c\u4f7f\u7528<\/p>\n<p> \u9084\u6709\u7d55\u5c0d\u4e0d\u8981\u7528 goto\uff0c\u9019\u53ea\u6703\u8b93\u770b\u7684\u4eba\u75db\u82e6<\/p>\n<pre><code class=\"hljs language-plaintext\">while (condition1) {\n  xxx\n    if (condition2) {\n      break;\n    }\n}<\/code><\/pre>\n<p>\u63db\u6210\u4e0b\u9762\uff0c\u9019\u6a23\u660e\u986f\u597d\u591a\u4e86<\/p>\n<pre><code class=\"hljs language-plaintext\">while (condition1 &amp;&amp; !condition2) {\n  xxx\n}<\/code><\/pre>\n<p>\u81f3\u65bccontinue\uff0c\u600e\u67d0\u4e0d\u8003\u616e\u4e00\u4e0b\u628a\u689d\u4ef6\u5f0f\u53cd\u8f49\u4e00\u4e0b<\/p>\n<pre><code class=\"hljs language-plaintext\">List goodNames = new ArrayList&lt;&gt;();\nfor (String name: names) {\n    if (name.contains(\"bad\")) {\n       continue;\n    }\n    goodNames.add(name);\n    ...\n}<\/code><\/pre>\n<p>\u6539\u5beb\u4e00\u4e0b<\/p>\n<pre><code class=\"hljs language-plaintext\">List goodNames = new ArrayList&lt;&gt;();\nfor (String name: names) {\n    if (!name.contains(\"bad\")) {\n       goodNames.add(name);\n       ...\n    }\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u5169\u6bb5code , reference :<\/p>\n<p><a href=\"http:\/\/www.jianshu.com\/p\/7645a5ea7f46?utm_content=buffer50622\">http:\/\/www.jianshu.com\/p\/7645a5ea7f46?utm_content=buffer50622<\/a><\/p>\n<p>\uff08p.57\uff09<\/p>\n<h2>\u7b2c\u56db\u7ae0 \u8a3b\u89e3<\/h2>\n<p>\u6c92\u6709\u4efb\u4f55\u6771\u897f\u53ef\u4ee5\u6bd4\u4e00\u6bb5\u653e\u5c0d\u4f4d\u7f6e\u7684\u8a3b\u89e3\u9084\u6709\u7528\uff0c\u4f46\u662f\u653e\u932f\u5730\u65b9\u7684\u8a3b\u89e3\uff0c\u66f4\u53ef\u4ee5\u5f04\u4e82\u6a21\u7d44\u5316\uff0c\u66f4\u5225\u8aaa\u5beb\u932f\u7684\u8a3b\u89e3\uff0c\u9019\u6839\u672c\u662f\u8b20\u8a00\u7522\u751f\u5668<\/p>\n<p>\uff08p.61\uff09<\/p>\n<h3>4.1 \u8a3b\u89e3\u6c92\u8fa6\u6cd5\u5f4c\u88dc\u7cdf\u7cd5\u7684\u7a0b\u5f0f\u78bc<\/h3>\n<p>\u5982\u679c\u7a0b\u5f0f\u78bc\u5df2\u7d93\u9ad2\u4e82\u5230\u5fc5\u9808\u5beb\u4e00\u5927\u5806\u8a3b\u89e3\u4f86\u641e\u6e05\u695a\u610f\u5716\uff0c\u90a3\u8acb\u91cd\u5beb\u4f60\u7684\u7a0b\u5f0f\u78bc\uff0c\u8b93\u4ed6\u66f4\u6574\u6f54\uff0c\u9032\u800c\u4e0d\u9700\u8981\u90a3\u4e00\u5806\u96e3\u641e\u7684\u8a3b\u89e3<\/p>\n<p> \u6574\u6f54\u7684\u7a0b\u5f0f\u78bc\u7d55\u5c0d\u597d\u904e\u4e00\u5927\u5806\u7684\u8a3b\u89e3\uff0c\u6700\u9ad8\u5883\u754c\u5c31\u662f\u4e0d\u7528\u8a3b\u89e3<\/p>\n<p>\uff08p.63\uff09<\/p>\n<h4><strong>4.1.1 \u7576\u7136\u4e5f\u662f\u6709\u5fc5\u8981\u6216\u597d\u7684\u8a3b\u89e3<\/strong><\/h4>\n<p>\uff11\uff0e\u6cd5\u5f8b\u5f62\u7248\u6b0a\u5ba3\u544a<\/p>\n<p>\uff12\uff0e\u51fd\u5f0f\u540d\u7a31\u591a\u5230\u4e0d\u884c\uff0c\u5f04\u500b\u6709\u5206\u5b57\u7684\u8a3b\u89e3\u6bd4\u8f03\u597d\u61c2<\/p>\n<p>\uff13\uff0e\u89e3\u91cb\u81ea\u5df1\u8a2d\u8a08\u7684\u610f\u5716\u60f3\u6cd5\uff0c\u4f8b\u5982\u51fd\u5f0f\u7684\u9806\u5e8f\uff0c\u6216\u662f\u7279\u6b8a\u7684spec request.<\/p>\n<p>\uff14\uff0e\u51fd\u5f0f\u5eab\u88e1\u7684\u51fd\u5f0f\u771f\u7684\u592a\u8a6d\u7570\uff0c\u4f46\u662f\u6211\u5011\u4e0d\u80fd\u53bb\u4fee\u6539\u51fd\u5f0f\u5eab\u7684\u51fd\u5f0f\u540d\u7a31\uff0c\u53ea\u597d\u81ea\u5df1\u5728\u4f7f\u7528\u6642\uff0c\u8aaa\u6e05\u695a<\/p>\n<p>\uff15\uff0eside effect \u7684\u63d0\u793a<\/p>\n<p>\uff16\uff0eTODO \u7684\u8a3b\u89e3\uff0c\u9019\u6700\u9ad8\u5883\u754c\u5c31\u662f\u5168\u90e8\u6e05\u6389\uff0c\u4f46\u662f\u6211\u5011\u4e5f\u77e5\u9053\u9019\u4e0d\u592a\u53ef\u80fd\uff0c\u4f7f\u7528TODO\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u627e\u5230\u4fee\u6539\u9ede<\/p>\n<h4><strong>4.1.2 \u7cdf\u7cd5\u7684\u8a3b\u89e3<\/strong><\/h4>\n<p>\uff11\uff0e\u795e\u79d8\u8a3b\u89e3\uff0c\u53ea\u6709\u4f60\u770b\u5f97\u61c2\u7684\u6587\u5b57<\/p>\n<p>\uff12\uff0e\u5beb\u592a\u9577\u8a3b\u89e3\uff0c\u770b\u4f60\u7684\u8a3b\u89e3\u53ef\u80fd\u80fd\u6bd4\u770b\u4f60\u7a0b\u5f0f\u78bc\u9084\u8981\u6d6a\u8cbb\u6642\u9593\u7684\u8a3b\u89e3, html \u5f0f\u7684\u8a3b\u89e3\uff0c\u89e3\u91cb rfc \u6587\u4ef6\u7de8\u78bc\u6a21\u5f0f\u554a\uff0c\u7406\u89e3\u8d77\u4f86\u53c8\u7d2f\u53c8\u4e0d\u9700\u8981<\/p>\n<p>\uff13\uff0e\u8aa4\u5c0e\u5f62\u8a3b\u89e3\uff0c\u9019\u53ef\u80fd\u662f\u6b77\u53f2\u5171\u696d\uff0c\u62dc\u8a17\u5feb\u522a\u6389<\/p>\n<p>\uff14\uff0e\u898f\u5b9a\u884c\u8a3b\u89e3\uff0c\u4f8b\u5982 java doc \u7684\u898f\u5b9a\u683c\u5f0f\uff0c\u6bcf\u500b\u90fd\u8981\u5beb\u771f\u7684\u8822\u7206\u4e86<\/p>\n<p>\uff15\uff0e\u65e5\u8a8c\u5f62\u8a3b\u89e3\uff0c\u8001\u5be6\u8aaa\u9019\u5728\u6c92\u6709 git \u7684\u6642\u4ee3\u9084\u6709\u7406\u7531\uff0c\u4f46\u73fe\u5728\u7528 commit message \u53bb\u53d6\u4ee3\u4e0d\u662f\u5f88\u597d\u55ce\uff1f\u4f5c\u8005\u8ddf\u51fa\u8655\u6c92\u898f\u5b9a\u7684\u8a71\u4e5f\u662f\u540c\u6a23<\/p>\n<p>\uff16\uff0e\u5e72\u64fe\u5f62\u8a3b\u89e3\uff0c\u5c31\u662f\u5ee2\u8a71\uff0c\u5beb\u932f\u5730\u65b9\u554a\uff0c\u5168\u57df\u548c\u5340\u57df\u7684\u8a3b\u89e3\u8981\u5206\u6e05\u695a<\/p>\n<p>\uff17\uff0e\u88ab\u8a3b\u89e3\u8d77\u4f86\u7684\u7a0b\u5f0f\u78bc<\/p>\n<p>\u9019\u500b\u6211\u89ba\u5f97\u8981\u5927\u529b\u63d0\u5021\uff0c\u901a\u5e38\u4f60\u8a3b\u89e3\u8d77\u4f86\u7684\u7a0b\u5f0f\u78bc\uff0c\u5176\u4ed6\u4eba\u90fd\u6703\u4ee5\u70ba\u5f88\u91cd\u8981\uff0c\u7136\u5f8c\u4e0d\u6562\u53bb\u522a\u6389\uff0c\u5176\u4ed6\u90fd\u6703\u4ee5\u70ba\u7559\u8005\u5fc5\u6709\u539f\u56e0\uff0c\u6700\u5f8c\u5c31\u8b8a\u6210\u985e\u4f3c\u9ef4\u83cc\u7684\u6771\u897f\uff0c\u5be6\u969b\u4e0a\u9019\u5728\u6c92 git \u7684\u6642\u4ee3\u6216\u8a31\u53ef\u4ee5\u5e6b\u52a9\u628a code \u4fdd\u7559\u8457\uff0c\u4f46\u662f\u73fe\u5728\u6709\u7248\u672c\u63a7\u5236\u4e86\uff0c\u8acb\u76f4\u63a5\u522a\u6389\u3002<\/p>\n<p> \uff08\u9019\u500b\u53ef\u4ee5\u5927\u529b\u63a8\u5ee3\uff0c\u8001\u5be6\u8aaa\u8a3b\u89e3\u8d77\u4f86\u5230\u6700\u5f8c\u7684\u4e0b\u5834\u90fd\u53ea\u662f\u5fd8\u4e86\u522a\uff0c\u800c\u4e14\u96a8\u8005\u958b\u767c\uff0c\u90a3\u4e9b\u88ab\u8a3b\u89e3\u7684\u7a0b\u5f0f\u78bc\u4e00\u9ede\u90fd\u4e0d\u91cd\u8981\uff09<\/p>\n<p>\u63a5\u4e0b\u4f86\u6211\u4f86\u8868\u6f14\u4e00\u6bb5\u7cdf\u7cd5\u7684\u4ee3\u78bc\uff0c\u81f3\u65bc\u5982\u4f55 refactor \u8acb\u81ea\u5df1\u7df4\u7fd2\u5427\uff1a<\/p>\n<pre><code class=\"hljs language-java\"><span class=\"hljs-comment\">\/**\n * changes (from 11-OCT-2009 ) author : heaton\n *\n * Add a CD with title , author , number , like celine.\n * <span class=\"hljs-doctag\">@param<\/span> title The title of the CD\n * <span class=\"hljs-doctag\">@param<\/span> author The author of the CD\n * <span class=\"hljs-doctag\">@param<\/span> tracks The number of tracks on the CD\n*\/<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title function_\">addCD<\/span><span class=\"hljs-params\">(String title , String author , <span class=\"hljs-type\">int<\/span> tracks)<\/span> {\n   <span class=\"hljs-comment\">\/\/ new CD for add to total list.<\/span>\n   <span class=\"hljs-type\">CD<\/span> <span class=\"hljs-variable\">cd<\/span>  <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">CD<\/span>();\n   cd.title = title;\n   cd.author = author;\n   <span class=\"hljs-comment\">\/\/ size mean the cd duration from start to end.<\/span>\n   <span class=\"hljs-comment\">\/\/cd.size = size;<\/span>\n   cd.tracks = tracks;\n   cdList.add(cd);\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u5410\u69fd\u9ede\u771f\u7684\u592a\u591a\u4e86\uff0c\u8acb\u597d\u597d\u6aa2\u8996\u73fe\u5728\u624b\u4e0a\u7684\u5c08\u6848<\/p>\n<p>\uff08p.63-77\uff09<\/p>\n<h2>\u7b2c\u4e94\u7ae0 \u7de8\u6392<\/h2>\n<p>\u7a0b\u5f0f\u7684\u7de8\u6392\u5f88\u91cd\u8981\uff0c\u7dad\u6301\u4e00\u5b9a\u7684\u5718\u968a\u898f\u5247\u9032\u884c\u7de8\u6392\u7d55\u5c0d\u53ef\u4ee5\u52a0\u901f\u95b1\u8b80<\/p>\n<p> \u66f4\u91cd\u8981\u7684\u662f \u53ea\u8981\u78ba\u7acb\u597d\u5718\u968a\u898f\u5247\u5f8c\uff0c\u81ea\u52d5\u5316\u5de5\u5177\u53ef\u4ee5\u5e7e\u4e4e\u7121\u75db\u5c0e\u5165\uff0ccp\u503c\u6975\u9ad8<\/p>\n<p>(P.86)<\/p>\n<h3>\u500b\u4eba\u89ba\u5f97\u9019\u6bb5\u9084\u597d\uff0c\u4e3b\u8981\u5c31\u662f\u5169\u500b\u91cd\u9ede<\/h3>\n<p>5.1\u6c34\u5e73\u8ddd\u96e2\uff1a<\/p>\n<p>5.1.1 :\u7a0b\u5f0f\u78bc\u7684\u4efb\u4f55\u4e00\u884c\u4e0d\u61c9\u8a72\u592a\u9577\uff0c\u6700\u4f4e\u6700\u4f4e\u7684\u8981\u6c42\u5c31\u662f\u4e0d\u5f97\u8d85\u51fa\u87a2\u5e55\u9801\u9762\u5bec\u5ea6<\/p>\n<p>\uff0c\u4fdd\u6301\u5728\u8996\u7dda\u7bc4\u570d\u5167\uff0c\u5982\u679c\u9700\u8981\u5de6\u53f3\u6ed1\u52d5\uff0c\u90a3\u80af\u5b9a\u9020\u6210\u56f0\u64fe<\/p>\n<p>5.1.2\uff1a\u6bcf\u884c\u7684\u7a0b\u5f0f\u78bc\uff0c\u662f\u60c5\u6cc1\u4fdd\u6301\u7a7a\u767d\u884c\uff0c\u5598\u53e3\u6c23<\/p>\n<p>(P.86)<\/p>\n<h3>5.2. \u5782\u76f4\u8ddd\u96e2\uff1a<\/h3>\n<p>5.2.1\uff1a\u8b8a\u6578\u5ba3\u544a\u4f4d\u7f6e\u7d71\u4e00\u653e\u7f6e\uff0cjava \u7fd2\u6163\u5168\u57df\u8b8a\u6578\u90fd\u653e\u5728\u6587\u4ef6\u6700\u4e0a\u7aef\uff0c\u800c\u5340\u57df\u8b8a\u6578\u5247\u76e1\u91cf\u9760\u8fd1\u4f7f\u7528\u4f4d\u5b50<\/p>\n<p>5.2.2\uff1a\u76f8\u4f9d\u51fd\u5f0f\u76e1\u91cf\u9644\u8fd1\uff0c\u6700\u597d\u7684\u60c5\u6cc1\u5c31\u662f\uff0c\u53ef\u4ee5\u5f9e\u958b\u982d\u7684\u51fd\u5f0f\u9806\u9806\u5730\u5f80\u4e0b\u770b\uff0c\u800c\u4e0d\u9700\u8981\u5927\u7bc4\u570d\u7684\u6372\u52d5\u6ed1\u9f20<\/p>\n<p>5.2.3\uff1a\u4fdd\u6301\u4e00\u5b9a\u7684\u7a7a\u767d\u884c\uff0c\u4eba\u5011\u559c\u6b61\u6709\u4e9b\u7559\u767d\u7684\u4eba\u751f<\/p>\n<p>(P.90)<\/p>\n<p>\u9019\u4e00\u6bb5\u500b\u4eba\u89ba\u7684\uff0c\u4e0a\u9762\u7684\u6c34\u5e73\u548c\u5782\u76f4\u8ddd\u96e2\u7684\u898f\u5247\u90fd\u53ef\u4ee5\u9760IDE\u505a\u5230<\/p>\n<p>\u4f46\u662f\u5171\u7528\u7684\u8b8a\u6578\u540d\uff0c\u6216\u662f\u6578\u503c\u5b9a\u7fa9\u7684\u4f4d\u5740\u8981\u78ba\u4e0d\u597d\u57f7\u884c<\/p>\n<p>ex: \u50cf\u662f\u5e38\u898b\u7684 xxxSetting.java , xxxConstant.java , xxxDefine.java , \u9019\u4e00\u985e\u7684\u767e\u5bb6\u722d\u9cf4<\/p>\n<p> \u4e0d\u503c\u5f97\u9f13\u52f5\uff0c\u52e2\u5fc5\u8981\u6709\u4eba\u7d71\u4e00\u898f\u7bc4\u4e26\u6574\u7406\uff0c\u653e\u5728\u5dee\u4e0d\u591a\u7684\u8def\u5f91\u4e0b\uff0c\u4e26\u7528\u6b63\u78ba\u7684\u547d\u540d\u5e6b\u52a9\u95b1\u8b80<\/p>\n<p> \u9019\u9ede\u624b\u4e0a\u7684 Home \u5c08\u6848\uff0c\u4e5f\u6709\u540c\u6a23\u7684\u554f\u984c<\/p>\n<h2>\u7b2c\u516d\u7ae0 \u7269\u4ef6\u53ca\u8cc7\u6599\u7d50\u69cb<\/h2>\n<p>java \u958b\u767c\u4e0a\u6211\u5011\u5e38\u5e38\u8b93\u7269\u4ef6\u7684\u8b8a\u6578\u4fdd\u6301 private \uff0c\u907f\u514d\u5176\u4ed6\u958b\u767c\u8005\u7a81\u767c\u5947\u60f3\u7684\u60f3\u8981\u6539\u52d5\u5176\u6578\u503c\u5c0e\u81f4\u72c0\u6cc1\u5927\u4e82<\/p>\n<p> \u4f46\u70ba\u4f55\u9084\u662f\u6709\u90a3\u67d0\u591a\u5de5\u7a0b\u5e2b\u6703\u52a0\u4e0a setter and getter\uff0c\u641e\u5f97\u50cf\u662f public \u4e00\u6a23\uff0c\u5927\u5bb6\u60f3\u6539\u5c31\u6539<\/p>\n<p>\uff08p.105\uff09<\/p>\n<p>\u5176\u5be6\u5c31\u662f\u8cc7\u6599\u62bd\u8c61\u5316\uff0c\u6211\u63d0\u4f9b\u4f60\u7684\u8b80\u53d6\u8cc7\u6599\u7684\u76ee\u7684\u8981\u6c42\uff0c\u4f46\u662f\u624b\u6bb5\u7531\u6211\u63a7\u5236\uff0c\u91cdsetter and getter \u4f86\u770b<\/p>\n<p>\u9019\u6a23\u7684\u6548\u679c\u4e26\u4e0d\u660e\u986f\uff0c\u4f46\u540c\u6a23\u662f\u4e0d\u544a\u8a34\u4f60\u88e1\u9762\u8a73\u7d30\u600e\u67d0\u904b\u4f5c\u7684<\/p>\n<h3>6.1 \u7269\u4ef6\u8207\u8cc7\u6599\u7684\u53cd\u5c0d\u7a31\u6027<\/h3>\n<p>\u9996\u5148\u5148\u5b9a\u7fa9\u597d\u7269\u4ef6\u8207\u8cc7\u6599<\/p>\n<p> \u7269\u4ef6\u5c07\u4ed6\u5011\u7684\u8cc7\u6599\u518d\u62bd\u8c61\u5c64\u4e4b\u5f8c\u96b1\u85cf\u8d77\u4f86\uff0c\u7136\u5f8c\u5c07\u64cd\u4f5c\u9019\u4e9b\u8cc7\u6599\u7684\u51fd\u5f0f\u66b4\u9732\u5728\u5916<\/p>\n<p> \u8cc7\u6599\u5247\u5c07\u8cc7\u6599\u66b4\u9732\u5728\u5916\uff0c\u800c\u4e14\u4e5f\u6c92\u63d0\u4f9b\u6709\u610f\u7fa9\u7684\u51fd\u5f0f<\/p>\n<p> \u6ce8\u610f\u9019\u5169\u500b\u5b9a\u7fa9\uff0c\u9019\u662f\u5c0d\u7acb\u4e14\u4e5f\u4e92\u88dc<\/p>\n<p>\uff08p.107\uff09<\/p>\n<p>\u770b\u500b\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"hljs language-java\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Square<\/span> {\n    <span class=\"hljs-keyword\">public<\/span> Point topLeft;\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">double<\/span> side;\n}\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Rectangle<\/span> {\n    <span class=\"hljs-keyword\">public<\/span> Point topLeft;\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">double<\/span> height;\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">double<\/span> width;\n}\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Circle<\/span> {\n    <span class=\"hljs-keyword\">public<\/span> Point center;\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">double<\/span> radius;\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Geometry<\/span> {\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">final<\/span> <span class=\"hljs-type\">double<\/span> <span class=\"hljs-variable\">PI<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-number\">3.14159<\/span>;\n    \n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">double<\/span> <span class=\"hljs-title function_\">area<\/span><span class=\"hljs-params\">(Object Shape)<\/span> <span class=\"hljs-keyword\">throws<\/span> NoSuchShapeException {\n        <span class=\"hljs-keyword\">if<\/span> (shape <span class=\"hljs-keyword\">instanceof<\/span> Square) {\n            <span class=\"hljs-type\">Square<\/span> <span class=\"hljs-variable\">s<\/span> <span class=\"hljs-operator\">=<\/span> (Square) shape;\n            <span class=\"hljs-keyword\">return<\/span> s.side * s.side;\n        } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (shape <span class=\"hljs-keyword\">instanceof<\/span> Rectangle) {\n            <span class=\"hljs-type\">Rectangle<\/span> <span class=\"hljs-variable\">r<\/span> <span class=\"hljs-operator\">=<\/span> (Rectangle) shape;\n            <span class=\"hljs-keyword\">return<\/span> r.height * r.width\n        } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (shape <span class=\"hljs-keyword\">instanceof<\/span> Circle) {\n            <span class=\"hljs-type\">Circle<\/span> <span class=\"hljs-variable\">c<\/span> <span class=\"hljs-operator\">=<\/span> (Circle) shape;\n            <span class=\"hljs-keyword\">return<\/span> PI * c.radius * c.radius;\n        }\n        <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">NoSuchShapeException<\/span>();\n    }\n}<\/code><\/pre>\n<p>\u8001\u5be6\u8aaa\u9019\u6a23\u7684\u7a0b\u5f0f\uff0c\u5fe0\u5be6\u7684\u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u8a2d\u8a08\u5e2b\u6703\u89ba\u5f97\u5f88\u8a6d\u7570\uff08\u6211\uff09\uff0c\u9019\u6839\u672c\u5c31\u662f procedural function<\/p>\n<p>\u6240\u4ee5\u63db\u500b\u65b9\u5f0f\u8a66\u8a66\u770b\uff08\u7269\u4ef6\u7248\uff09<\/p>\n<pre><code class=\"hljs language-java\"><span class=\"hljs-keyword\">interface<\/span> <span class=\"hljs-title class_\">Shape<\/span> {\n   <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">double<\/span> <span class=\"hljs-title function_\">area<\/span><span class=\"hljs-params\">()<\/span>;\n}\n\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Square<\/span> implement Shape {\n   <span class=\"hljs-keyword\">private<\/span> Point topLeft;\n   <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-type\">double<\/span> side;\n   <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">double<\/span> <span class=\"hljs-title function_\">area<\/span><span class=\"hljs-params\">()<\/span> {\n       <span class=\"hljs-keyword\">return<\/span> side * side;\n   }\n}\n<span class=\"hljs-comment\">\/\/....other class implement Shape<\/span><\/code><\/pre>\n<p>\u5f9e\u4e0a\u9762\u5169\u500b\u4f8b\u5b50\u53ef\u4ee5\u770b\u51fa<\/p>\n<p> \u5982\u679c\u8981\u52a0\u5165\u4e00\u500b\u65b0\u7684\u5716\u5f62\u985e\u5225\u6642\uff0c\u6703\u767c\u751f\u5565\u4e8b\uff1f<\/p>\n<p> \u7b2c\u4e00\u6bb5\u7a0b\u5f0f\u78bc\uff0c\u6211\u5fc5\u9808\u8981\u4fee\u6539Geometry \u5167\u7684\u6240\u6709\u7684\u76f8\u95dc\u51fd\u5f0f\u624d\u80fd\u8655\u7406\u9019\u4ef6\u4e8b\u60c5<\/p>\n<p> \u4f46\u662f\u63a1\u7528\u7b2c\u4e8c\u6bb5\u7a0b\u5f0f\u78bc\u7684\u8a71\uff0c\u5247\u6839\u672c\u4e0d\u9700\u8981Geometry \u9019\u500b\u985e\u5225<\/p>\n<p> \u65b0\u589e\u4efb\u4f55\u985e\u5225\u90fd\u4e0d\u6703\u5f71\u97ff\u5230\u73fe\u6709\u7684\u51fd\u5f0f\u3002<\/p>\n<p>\u90a3\u5982\u679c\u8981\u52a0\u5165\u4e00\u500b\u65b0\u7684\u51fd\u5f0f\u50cf\u662f perimeter() \u7684\u8a71\uff0c\u6703\u767c\u751f\u5565\u4e8b\uff1f<\/p>\n<p> \u7b2c\u4e8c\u6bb5\u7a0b\u5f0f\u78bc\u7684\u6240\u6709 Shape \u7269\u4ef6\u90fd\u5fc5\u9808\u8ddf\u8005\u4fee\u6539\uff0c<\/p>\n<p> \u4f46\u7b2c\u4e00\u6bb5\u7a0b\u5f0f\u78bc\u537b\u53ea\u8981\u4fee\u6539 Geometry \u985e\u5225\uff0c\u589e\u52a0\u4e00\u500b\u51fd\u5f0f\u5373\u53ef<\/p>\n<p>\uff08p.108\uff09<\/p>\n<p>\u7531\u6b64\u53ef\u4ee5\u770b\u51fa\u9019\u5169\u7a2e\u8a2d\u8a08\u7684\u5c0d\u7acb\u4ee5\u53ca\u4e92\u88dc\u9762<\/p>\n<p>\u518d\u6b21\u63a8\u5c0e\u51fa\u4f86\uff1a<\/p>\n<p>\u300e\u7d50\u69cb\u5316\u7a0b\u5f0f\u78bc\u5bb9\u6613\u6dfb\u52a0\u65b0\u7684\u51fd\u5f0f\uff0c\u800c\u4e0d\u9700\u8981\u8b8a\u52d5\u5df2\u6709\u7684\u8cc7\u6599\u7d50\u69cb<\/p>\n<p> \u800c\u7269\u4ef6\u5c0e\u5411\u7684\u7a0b\u5f0f\u78bc\uff0c\u5bb9\u6613\u6dfb\u52a0\u65b0\u7684\u985e\u5225\uff0c\u800c\u4e0d\u7528\u8b8a\u52d5\u5df2\u6709\u7684\u51fd\u5f0f\u300f<\/p>\n<p>\u53cd\u904e\u4f86\u8aaa\uff1a<\/p>\n<p>\u300e\u7d50\u69cb\u5316\u7a0b\u5f0f\u78bc\u96e3\u4ee5\u6dfb\u52a0\u65b0\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u56e0\u70ba\u5fc5\u9808\u6539\u8b8a\u6240\u6709\u7684\u51fd\u5f0f<\/p>\n<p> \u7269\u4ef6\u5c0e\u5411\u7a0b\u5f0f\u78bc\u96e3\u4ee5\u6dfb\u52a0\u65b0\u7684\u51fd\u5f0f\uff0c\u56e0\u70ba\u5fc5\u9808\u6539\u8b8a\u6240\u6709\u7684\u985e\u5225\u300f<\/p>\n<p>\uff08p.109\uff09<\/p>\n<p>\u672c\u4eba\u5118\u7ba1\u8eab\u70ba\u7269\u4ef6\u5c0e\u5411\u6d3e\u7684\u4e00\u4efd\u5b50\uff0c\u7576\u7136\u4e5f\u77e5\u9053\u5168\u90e8\u90fd\u662f\u7269\u4ef6\uff0c\u57fa\u672c\u4e0a\u662f\u500b\u795e\u8a71<\/p>\n<p>\u6240\u4ee5\u5728\u9069\u5408\u7684\u6642\u5019\uff0c\u8981\u61c2\u5f97\u7528\u7d50\u69cb\u5316\u7684\u7a0b\u5f0f\uff08\u9700\u8981\u5e38\u5e38\u589e\u52a0\u6e1b\u5c11\u51fd\u5f0f\u7684\u60c5\u6cc1\uff09\uff0c\u5e6b\u52a9\u958b\u767c<\/p>\n<p>\u6240\u6709\u7684\u8a9e\u6cd5\u90fd\u662f\u5de5\u5177\uff0c\u6311\u6700\u9069\u5408\u7684\u5c31\u5c0d\u4e86<\/p>\n<h3>6.2 Data Transfer Object<\/h3>\n<p>\u9019\u7a2e\u7269\u4ef6\u975e\u5e38\u5e38\u898b\uff0c\u57fa\u672c\u4e0a\u5c31\u662f\u8cc7\u6599\u5eab\u7684\u539f\u59cb\u8cc7\u6599 \u8f49\u6210 \u61c9\u7528\u7684\u7a0b\u5f0f\u5167\u7684\u7269\u4ef6<\/p>\n<p> \u4f8b\u5982\uff1a greendao , json to object , gson \u9019\u985e\u7684\u95dc\u9375\u5b57<\/p>\n<p> \u4fd7\u7a31bean<\/p>\n<p> \u9019\u5176\u5be6\u5f88\u591a\u8cc7\u6599\u5167\u5bb9\u90fd\u53ef\u4ee5\u7528\u516c\u958b\u8b8a\u6578\uff0c\u4f46\u662f\u9084\u662f\u7fd2\u6163\u5beb\u6210setter and getter<\/p>\n<p> \u5176\u5be6\u5c31\u662f\u4e00\u7a2e\u50cf\u7269\u4ef6\u5c0e\u5411\u958b\u767c\u8005\u7684\u59a5\u5354<\/p>\n<p>\uff08p.113\uff09<\/p>\n<p>\u8b80\u5230\u9019\u6bb5\u6642\uff0c\u7a81\u7136\u60f3\u5230\u4e4b\u524d\u8ddf\u8001\u5927\u5728\u8a0e\u8ad6\uff08\u611f\u89ba\u8ddf\u9019\u7ae0\u7bc0\u6c92\u5565\u95dc\u4fc2\uff09<\/p>\n<p>android \u63d0\u4f9b\u5916\u90e8 api access content provider \u7684\u65b9\u6cd5\uff0c\u7528\u54ea\u7a2e\u6bd4\u8f03\u597d\uff1f<\/p>\n<p>\u672c\u4eba\u89ba\u5f97 \u5c31\u8d70 content provider \u7684\u73a9\u6cd5\u52a0\u6587\u4ef6\uff0c\u63d0\u4f9b URI \u7136\u5f8c\u7528android \u539f\u751f\u7684 sqlite crud \u64cd\u4f5c<\/p>\n<p>\u4e3b\u8981\u5c31\u662f\u4fdd\u6301\u8cc7\u6599\u7684\u5f48\u6027\u64cd\u4f5c\uff0c\u539f\u751f\u7684\u73a9\u6cd5\u5230\u54ea\u90fd\u4e0d\u6703\u6709\u932f\uff0c\u9019\u6a23\u53ef\u4ee5\u4fdd\u6301\u76ee\u524d\u7684\u7a0b\u5f0f\u78bc\uff0c\u4ee5\u6700\u5c0f\u7684\u4fee\u6539\u5e45\u5ea6<\/p>\n<p>\u5c31\u53ef\u4ee5\u5b8c\u6210\u9019\u9805\u4efb\u52d9<\/p>\n<p>\u8001\u5927\u89ba\u5f97 \u51fa\u5c08\u9580\u7684 lib \u628a\u6240\u6709\u7684crud\u64cd\u4f5c\u90fd\u898f\u7bc4\u8d77\u4f86\uff0c\u63d0\u4f9b example , \u8981\u6c42\u5b58\u53d6\u8005\u9020\u898f\u5247\u8d70<\/p>\n<p>\u6709\u9ede\u5fd8\u4e86\u70ba\u4f55\uff0c\u4f46\u4e3b\u8981\u662f\u5229\u7528\u51fd\u5f0f\u7684\u64cd\u4f5c\uff0c\u8b93\u7b2c\u4e09\u65b9\u5fc5\u9808\u8a8d\u771f\u7684\u770b\u61c2\u624d\u80fd\u53bb\u64cd\u4f5c\u8cc7\u6599\u5eab<\/p>\n<p>\u78ba\u4fdd\u5b89\u5168\uff0c\u4e26\u4fdd\u6301\u7a0b\u5f0f\u7684\u62bd\u8c61\u5316\uff0c\u4ee5\u5f8c\u96a8\u4fbf\u6539\u90fd\u53ef\u4ee5<\/p>\n<p>\u4e0d\u77e5\u9053\u500b\u4f4d\u770b\u5b98\u89ba\u5f97\u512a\u7f3a\u9ede\u662f\u5565\uff0c\u6216\u662f\u53ef\u4ee5\u88dc\u5145\u6c92\u6ce8\u610f\u5230\u7684\u9ede\uff0c\u6b61\u8fce\u8a0e\u8ad6<\/p>\n<h2>\u7b2c\u4e03\u7ae0 \u932f\u8aa4\u8655\u7406<\/h2>\n<p>\u5b9a\u7fa9\u6700\u5c0f\u7684 handle scope \u548c\u6b63\u5e38\u7684\u7a0b\u5f0f\u6d41\u7a0b<\/p>\n<h3>7.1 try\/catch \u6700\u597d\u80fd\u5b9a\u7fa9\u5728\u6700\u7cbe\u6e96\u7684\u7bc4\u570d\uff1a<\/h3>\n<p>\u4e00\u822c\u60c5\u6cc1\u4e0b\uff0c\u4e0d\u5c11\u4eba\u6703\u8caa\u5716\u65b9\u4fbf\uff0c\u6216\u53ea\u662f\u60f3\u907f\u958b\u7de8\u8b6f\u5668\u8df3\u51fa\u7684\u932f\u8aa4\uff0c\u9032\u800c\u628a\u4e00\u5927\u6bb5\u7a0b\u5f0f\u78bc\u7528 try\/catch \u5305\u8d77\u4f86\uff0c\u66f4\u751a\u8005\u53ea\u7528 Exception \u9019\u500b\u5de8\u5927\u7684\u932f\u8aa4\u76ee\u6a19\uff0c\u4f46\u9019\u6a23\u4ee5\u5f8c\u5fc5\u5b9a\u4ed8\u51fa\u4ee3\u50f9\uff0c\u6700\u4e3b\u8981\u7684\u5c31\u662f\u7121\u6cd5\u5728\u932f\u8aa4\u6642\uff0c\u63d0\u4f9b\u8db3\u5920\u7cbe\u78ba\u7684 error message\uff0e<\/p>\n<h3>7.2 \u6b63\u5e38\u7684\u7a0b\u5f0f\u6d41\u7a0b<\/h3>\n<p>try\/catch \u7684 catch \u53ea\u53bb\u8655\u7406\u932f\u8aa4\u7684\u56de\u5fa9\uff0c\u78ba\u4fdd\u7a0b\u5f0f\u4e0d\u6703\u5361\u5728\u9019\u88e1\uff0c\u4f46\u662f\u4e0d\u8981\u628a\u5546\u696d\u908f\u8f2f\u5750\u5728 catch \u8655\u7406\uff0c\u4e5f\u5c31\u662f\u628a\u932f\u8aa4\u767c\u751f\u60c5\u6cc1\u7d66\u5e38\u614b\u5316\uff0c\u9019\u6703\u9020\u6210\u5f8c\u7e8c\u5176\u4ed6\u958b\u767c\u8005\uff0c\u7121\u6cd5\u5340\u5206\u5546\u696d\u908f\u8f2f\u548c\u932f\u8aa4\u8655\u7406\u7684\u5dee\u7570\uff0c\u4e5f\u5c31\u662f\u4e0d\u597d\u8b80\uff0c\u5149\u662f\u9019\u6a23\u5c31\u9055\u53cd\u4e86 clean code.<\/p>\n<h3>7.3 \u4e0d\u8981\u56de\u50b3 null<\/h3>\n<p>\u300e\u8bb8\u591a\u8bed\u8a00\uff08C\uff0cC++\uff0cJava\uff0cC#\uff0c\u2026\u2026\uff09\u7684\u7c7b\u578b\u7cfb\u7edf\u5bf9\u4e8enull\u7684\u5904\u7406\uff0c\u5176\u5b9e\u662f\u5b8c\u5168\u9519\u8bef\u7684\u3002\u8fd9\u4e2a\u9519\u8bef\u6e90\u81ea\u4e8eTony Hoare\u6700\u65e9\u7684\u8bbe\u8ba1\uff0cHoare\u628a\u8fd9\u4e2a\u9519\u8bef\u79f0\u4e3a\u81ea\u5df1\u7684\u201cbillion dollar mistake\u201d\uff0c\u56e0\u4e3a\u7531\u4e8e\u5b83\u6240\u4ea7\u751f\u7684\u8d22\u4ea7\u548c\u4eba\u529b\u635f\u5931\uff0c\u8fdc\u8fdc\u8d85\u8fc7\u5341\u4ebf\u7f8e\u5143\u3002\u300f<\/p>\n<p>\u6709\u4eba\u56de\u50b3 null \u90a3\u5c31\u4ee3\u8868\u6709\u4eba\u8981\u8655\u7406 null \uff0c \u5f88\u597d\u9019\u6642\u5019\u500b\u4eba\u78ba\u8a8d\u4e00\u4e0b\u624b\u4e0a\u5c08\u6848\u7684 LPageLayout.java \uff0c\u5927\u6982\u7e3d\u5171\u5beb\u4e86\u7d04\uff14\uff10\u500b null \u6aa2\u67e5\uff0c\u9019\u5b8c\u5168\u5c31\u662f\u7d66\u81ea\u5df1\u627e\u9ebb\u7169\u7684\u884c\u70ba\uff0c\u9019\u6642\u5019\u6709\u4eba\u53ef\u80fd\u8a8d\u70ba\u53ef\u4ee5\u7528 NullPointerException \u4f86\u7701\u4e0b\u5927\u91cf\u7684 null check , \u4f46\u9019\u6642\u5019\u548c\u7b2c\u4e00\u9805\u7684\u300etry\/catch \u6700\u597d\u80fd\u5b9a\u7fa9\u5728\u6700\u7cbe\u6e96\u7684\u7bc4\u570d\uff1a\u300f\u5b8c\u5168\u885d\u7a81\uff0c\u5982\u679c\u7bc4\u570d\u5927\uff0c\u78ba\u5be6\u53ef\u4ee5\u7701\u4e0b\u5927\u91cf\u7684 null check \uff0c \u4f46\u662f\u7b2c\u4e00\u9805\u7684\u9ebb\u7169\u4e5f\u6703\u8ddf\u8005\u7522\u751f\u3002<\/p>\n<p>\u56e0\u6b64\u6700\u597d\u7684\u65b9\u6cd5\u5c31\u662f\uff1a \u4e0d\u8981\u56de\u50b3 null<\/p>\n<p>\u6700\u5e38\u51fa\u73fe\u9019\u7a2e\u60c5\u6cc1\u5c31\u662f\uff0cList \u7684 null check \uff0c<\/p>\n<pre><code class=\"hljs language-plaintext\">List personalList = loader.getPersionalData();\nif (personalList != null) {\n    for (int i = 0; i &lt; personalList.size() ; i ++ ) {\n        xxxxx;\n        \/\/ \u5982\u679c\u9019\u6642\u5019\u727d\u626f\u5230\u591a\u57f7\u884c\u7dd2\u90a3\u5c31\u66f4\u523a\u6fc0\u4e86\n    }\n}<\/code><\/pre>\n<p>\u56e0\u6b64\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u6211\u5011\u5b8c\u5168\u5efa\u8b70 loader.getPersionalData() \u56de\u50b3\u4e00\u500b list size is 0 \u7684 empty list\uff0c\u53ef\u4ee5\u7528<\/p>\n<pre><code class=\"hljs language-plaintext\">Collections.emptyList();<\/code><\/pre>\n<p>\u4f86\u7522\u751f\u4e00\u500b\u7a7a list<\/p>\n<h3>7.4 \u4e0d\u8981\u50b3\u905e null<\/h3>\n<p>\u6bd4\u56de\u50b3 null \u9084\u8981\u7cdf\u7cd5\u7684\u5c31\u662f\u50b3\u905e null \u53c3\u6578\u5230\u51fd\u5f0f\u88e1\uff0c\u5927\u90e8\u5206\u51fd\u5f0f\u7684\u53c3\u6578\u8655\u7406\u90fd\u4e0d\u6703\u9810\u60f3\u5230\u8f38\u5165\u53c3\u6578\u662f null \u7684\u60c5\u6cc1\uff08\u57fa\u672c\u578b\u5225\u60f3\u5230\uff10\u7684\u60c5\u6cc1\u53ef\u80fd\u9084\u6709\uff09\uff0c\u5927\u90e8\u5206\u5c31\u76f4\u63a5\u7206\u500b NullPointerException \u7d66\u4f60\uff0c\u9019\u9020\u6210\u8981\u78bc\u662f\u4f60\u505a null check \uff0c\u4e0d\u7136\u5c31\u662f\u51fd\u5f0f\u7279\u5225\u5c0d\u53c3\u6578\u505a null check \uff08\u5efa\u8b70\u5229\u7528 @NonNull\uff09\uff0c\u7136\u5f8c\u7206 InvalidArgumentException\uff0c\u6211\u60f3\u9019\u5287\u60c5\u6c92\u6709\u597d\u592a\u591a\uff0c\u4e00\u6a23\u641e\u6b7b\u5927\u5bb6.<\/p>\n<p>\u56e0\u6b64\u540c\u6a23\u6700\u597d\u7684\u65b9\u6cd5\u5c31\u662f \u4e0d\u8981\u50b3\u905e null<\/p>\n<p>\u2014\u2013\u7b2c\u4e00\u7bc0\u5148\u5230\u9019\u908a\uff0d\uff0d\uff0d\uff0d<\/p>\n<p>Clean code: \u7121\u6687\u7684\u7a0b\u5f0f\u78bc\u7684\u7b2c\u4e00\u6bb5\u5fc3\u5f97\u5148\u5beb\u5230\u9019\u88e1<\/p>\n<p>\u7e3d\u5171\u6709\uff11\uff17\u7ae0\uff0c\u7e7c\u7e8c\u6574\u7406<\/p>\n<p>ps.\u539f\u672c\u4e0b\u4e00\u7ae0\u4ee5\u70ba\u6703\u5beb\u5f88\u9577\u7684<\/p>\n<p>\u5982\u679c\u6709\u4efb\u4f55\u9055\u53cd\u8457\u4f5c\u6b0a\u7684\u60c5\u4e8b\u767c\u751f\uff0c\u8acb\u4e3b\u52d5\u544a\u77e5\uff0c\u5c0f\u5f1f\u6703\u5feb\u901f\u4e0b\u67b6<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300e\u552f\u4e00\u6709\u6548\u7684\u7684\u300e\u7a0b\u5f0f\u54c1\u8cea\u300f\u5ea6\u91cf\u55ae\u4f4d\uff1a \u6bcf\u5206\u9418\u7f75\u9ad2\u8a71\u7684\u6b21\u6578(wtf\/minutes)\u300f\u7248\u6b0a\u7db2\u5740\u9644\u4e0a<br \/>\n\u9019\u53e5\u8a71\u8b93\u6211\u60f3\u5230\u540c\u4e8b\u6539\u5176\u4ed6\u90e8\u9580\u7a0b\u5f0f\u78bc\u6642\u7684\u72c0\u6cc1\uff0c\u6211\u60f3\u4ee5\u6b64\u4f5c\u70ba\u5f15\u8a00\uff0c\u5df2\u7d93\u76f8\u7576\u8db3\u5920\u7684\u8868\u793a\u51fa \u5beb\u51faclean code \u7684\u91cd\u8981\u6027<br \/>\n\uff08\u5e8f\uff09<\/p>\n<p>\u8a8d\u771f\u4e00\u9ede\u7684\u8a71\u5c31\u662f\uff1a<br \/>\n\u300e\u4f60\u6709\u5169\u500b\u539f\u56e0\u4f86\u8b80\u9019\u672c\u66f8\uff1a\u9996\u5148\u4f60\u662f\u500b\u7a0b\u5f0f\u8a2d\u8a08\u5e2b\uff0c\u63a5\u8005\uff0c\u4f60\u60f3\u6210\u70ba\u4e00\u500b\u66f4\u597d\u7684\u8a2d\u8a08\u5e2b\u300f<br \/>\n\u6211\u81ea\u5df1\u52a0\u4e0a\u53bb\u7684\uff1a\u300e\u525b\u597d\uff0c\u597d\u7684\u8a2d\u8a08\u5e2b\u85aa\u6c34\u597d\u50cf\u9084\u80fd\u770b\u300f<\/p>\n","protected":false},"author":1,"featured_media":1263,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[29],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/98"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=98"}],"version-history":[{"count":6,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/98\/revisions"}],"predecessor-version":[{"id":1589,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/98\/revisions\/1589"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/media\/1263"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}