{"id":320,"date":"2021-09-11T04:56:27","date_gmt":"2021-09-11T04:56:27","guid":{"rendered":"https:\/\/boochlin.com\/?p=320"},"modified":"2026-09-18T16:19:21","modified_gmt":"2026-09-18T16:19:21","slug":"funny-vote-app-mvp-%e6%9b%b4%e6%96%b0%e9%9b%9c%e8%a8%98","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=320","title":{"rendered":"Funny vote APP \u2013 MVP \u67b6\u69cb\u91cd\u69cb\u8207\u81ea\u52d5\u5316\u96dc\u8a18\uff08\u4e8c\uff09"},"content":{"rendered":"<div class=\"wp-block-group\" style=\"border-left:4px solid #C14420;background:#f8f9fa;padding:14px 18px;margin-bottom:28px;border-radius:0 6px 6px 0\">\n<p style=\"margin:0;font-size:0.95em;color:#444\">\n    \u672c\u6587\u70ba<strong>\u300aFunny Vote \u5341\u5e74\u6f14\u9032\u7cfb\u5217\u300b<\/strong>\u7b2c 2 \u7bc7\uff08\u5168\u7cfb\u5217\u5171 8 \u7bc7\uff09\uff5c<a href=\"https:\/\/boochlin.com\/?page_id=1319\" style=\"font-weight:bold;color:#C14420;text-decoration:underline\">\u67e5\u770b\u5168\u7cfb\u5217\u67b6\u69cb\u5c0e\u89bd<\/a>\n  <\/p>\n<\/div>\n<p>\u597d\u4e00\u9663\u5b50\u5fd8\u8a18\u8981\u66f4\u65b0\u4e00\u4e0b blog\uff0c\u6c92\u60f3\u5230\u597d\u4e0d\u5bb9\u6613\u5efa\u7acb\u7684\u958b\u767c\u7fd2\u6163\u4e5f\u6703\u5fd8\u8a18\uff0c\u53ef\u898b\u6020\u60f0\u771f\u7684\u958b\u767c\u7684\u6e90\u982d\u554a(?)\uff0c\u79c9\u6301\u8457 something record \u7684\u7cbe\u795e\uff0c\u7e7c\u7e8c\u4fdd\u6301\u597d\u7fd2\u6163\u3002\u5176\u5be6\u9019\u4e00\u7bc7\u662f Funny vote \u958b\u767c\u672d\u8a18\u7684\u5f8c\u7e8c\uff0c\u4e3b\u8981\u662f\u8a18\u9304\u91cb\u51fa\u5f8c\u7684\u5e7e\u6b21\u6280\u8853\u6539\u7248\u7df4\u7fd2\u3002\u9806\u9053\u4e00\u63d0\u4e0d\u6703\u6559\u4f60\u600e\u9ebc\u6539\u3001\u600e\u9ebc\u7528\uff0c\u4e5f\u6c92\u653e\u4e0a\u5565\u7a0b\u5f0f\u78bc\uff0c\u5e7e\u4e4e\u90fd\u662f\u6211\u7684 murmur\u3002<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img fetchpriority=\"high\" fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"500\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_funny_vote_banner.png\" alt=\"Funny Vote \u8da3\u6295\u7968 Android \u67b6\u69cb\u6f14\u9032\u7cfb\u5217\" class=\"wp-image-1277\" srcset=\"https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_funny_vote_banner.png 1024w, https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_funny_vote_banner-300x146.png 300w, https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_funny_vote_banner-768x375.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Funny Vote \u8da3\u6295\u7968 \u00b7 Android \u67b6\u69cb\u91cd\u69cb\u8207\u6f14\u9032\u7df4\u7fd2<\/figcaption><\/figure>\n<p>\u6839\u64da Google \u5b98\u65b9\u7684 Android Architecture \u6280\u8853\u85cd\u5716\uff0c\u6211\u5c07 Funny vote \u5206\u6210\u5e7e\u500b Git branch\uff0c\u5404\u81ea\u653e\u4e0a\u60f3\u73a9\u73a9\u7684\u6280\u8853\u67b6\u69cb\uff0c\u4e3b\u8981\u53ef\u5206\u70ba\uff1a<\/p>\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/boochlin06\/FunnyVote\/tree\/mvp\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>MVP \u67b6\u69cb\u6539\u7248<\/strong><\/a>\uff1a\u88dc\u4e0a Unit test by Mockito\u3001Instrument test by Espresso\uff0c\u4ee5\u53ca JaCoCo \u6574\u5408\u9032 Travis CI\u3002<\/li>\n<li><a href=\"https:\/\/github.com\/boochlin06\/FunnyVote\/tree\/mvp_dagger\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>MVP_Dagger<\/strong><\/a>\uff1a\u5728 MVP \u67b6\u69cb\u4e0a\u4f7f\u7528 Dagger 2 \u9032\u884c\u4f9d\u8cf4\u6ce8\u5165\u3002<\/li>\n<li><a href=\"https:\/\/github.com\/boochlin06\/FunnyVote\/tree\/mvp_kotlin\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>MVP_Kotlin<\/strong><\/a>\uff1a\u5728 MVP \u67b6\u69cb\u4e0a\u6539\u7528 Kotlin \u91cd\u5beb\u3002<\/li>\n<\/ol>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2 class=\"wp-block-heading\">\u4e00\u3001 MVP \u67b6\u69cb\u6539\u7248<\/h2>\n<p>\u5148\u8aaa\u8aaa\u6700\u521d\u7684\u7a0b\u5f0f\u67b6\u69cb\u4e26\u6c92\u6709\u63a1\u7528 MVP \u67b6\u69cb\uff0c\u800c\u662f MVC\u3002\u539f\u56e0\u6c92\u751a\u9ebc\u7279\u5225\u7684\uff0c\u5c31\u662f\u6211\u6c92\u60f3\u5230\u8981\u7528\u554a\uff01\u4eba\u53ea\u8981\u4e00\u6025\u8457\u60f3\u51fa\u7522\u54c1\u6642\uff0c\u90fd\u53ea\u6703\u8003\u616e\u6700\u5feb\u505a\u51fa\u4f86\u7684\u65b9\u6cd5\uff08\u4e5f\u4e0d\u6703\u60f3\u8981\u5beb test case\uff09\u3002\u9019\u4e26\u975e\u662f\u8aaa MVC \u662f\u500b\u7cdf\u7cd5\u4f46\u5feb\u901f\u7684\u67b6\u69cb\uff08\u662f\u8aaa\u5230\u73fe\u5728 MVC \u5230\u5e95\u662f\u5565\uff0c\u597d\u50cf\u4e5f\u4e0d\u662f\u5f88\u78ba\u5b9a\uff09\uff0c\u800c\u662f\u56e0\u70ba\u516c\u53f8\u7684\u6240\u6709 project \u5e7e\u4e4e\u90fd\u662f MVC \u7684\u95dc\u4fc2\uff0c\u6240\u4ee5\u6700\u719f\u6089\u3001\u53ef\u4ee5\u958b\u767c\u5f97\u5f88\u5feb\u3002\u4f46\u662f\u4e5f\u6df1\u6df1\u77e5\u9053\u4ed6\u7684\u7f3a\u9ede\u53ca\u512a\u9ede\uff0c\u4e14\u8ddf MVP \u6bd4\u8f03\u4e4b\u5f8c\uff0c\u591a\u591a\u5c11\u5c11\u80fd\u7406\u89e3 Google \u60f3\u529b\u63a8 MVP \u67b6\u69cb\u7684\u539f\u56e0\uff08\u5176\u4ed6\u9084\u6709\u4e00\u5806\u5566\uff0c\u50cf\u662f MVVM\u3001Lifecycle\u3001Clean Architecture\uff09\u3002<\/p>\n<p>MVC \u6700\u4e3b\u8981\u7684\u554f\u984c\u662f\u8b93 <strong>Activity \u53bb\u8ca0\u8cac\u6240\u6709\u7684 Controller \u908f\u8f2f\u4efb\u52d9\uff0c\u4f46\u662f\u672c\u8eab\u53c8\u8ca0\u64d4 View \u5c55\u793a\u8207\u7528\u6236\u4ecb\u9762\u64cd\u4f5c\u4ea4\u4e92<\/strong>\u3002\u9019\u6a23\u8b8a\u6210 Controller \u548c View \u7684\u4efb\u52d9\u6df7\u96dc\u5728\u4e00\u8d77\uff0c\u6240\u4ee5\u5e38\u5e38\u6703\u770b\u5f97\u5230\u8d85\u904e 2000 \u884c\u7684\u81c3\u816b Activity\u3002\u6839\u64da Clean Code \u7684\u6982\u5ff5\uff0c\u9019\u80af\u5b9a\u6703\u9020\u6210 Debug \u4e0a\u7684\u6975\u5927\u56f0\u96e3\uff0c\u56e0\u6b64 Google \u63d0\u51fa\u4e86\u5207\u5206\u66f4\u4e7e\u6de8\u7684 MVP \u67b6\u69cb\uff1a<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"1100\" height=\"650\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_android_mvp_diagram.png\" alt=\"Google Android Architecture Blueprints MVP \u6838\u5fc3\u89e3\u8026\u67b6\u69cb\" class=\"wp-image-1278\" srcset=\"https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_android_mvp_diagram.png 1100w, https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_android_mvp_diagram-300x177.png 300w, https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_android_mvp_diagram-1024x605.png 1024w, https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_android_mvp_diagram-768x454.png 768w\" sizes=\"(max-width: 1100px) 100vw, 1100px\" \/><figcaption class=\"wp-element-caption\">Google Android Architecture Blueprints \u00b7 MVP \u6838\u5fc3\u89e3\u8026\u6a21\u5f0f\u67b6\u69cb\u5716<\/figcaption><\/figure>\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\" style=\"border-left-color:#0d9488;border-left-width:4px;background-color:#f8fafc;padding-top:16px;padding-right:20px;padding-bottom:16px;padding-left:20px\">\n<p style=\"margin:0 0 8px 0;font-weight:700;color:#0f766e;\">\ud83d\udd39 VIEW (\u8996\u5716\u5c64)<\/p>\n<p style=\"margin:0;color:#334155;\">\u53ea\u8ca0\u8cac UI Init\u3001User \u64cd\u4f5c\u4e92\u52d5\uff0c<strong>\u4e0d\u80fd\u76f4\u63a5\u8207 Model \u6e9d\u901a<\/strong>\uff0c\u53ea\u80fd\u63a5\u53d7 Presenter \u7684\u6307\u4ee4\u3002<\/p>\n<\/div>\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\" style=\"border-left-color:#2563eb;border-left-width:4px;background-color:#f8fafc;padding-top:16px;padding-right:20px;padding-bottom:16px;padding-left:20px;margin-top:12px\">\n<p style=\"margin:0 0 8px 0;font-weight:700;color:#1d4ed8;\">\ud83d\udd39 PRESENTER (\u696d\u52d9\u4e2d\u4ecb\u5c64)<\/p>\n<p style=\"margin:0;color:#334155;\">\u8ca0\u8cac View \u8207 Model \u7684\u4e2d\u4ecb\u4e92\u52d5\uff0c<strong>\u8ca0\u8cac\u6240\u6709\u696d\u52d9\u908f\u8f2f\u8207\u6d41\u7a0b\u6c7a\u7b56<\/strong>\uff0c\u7d14 Java\/Kotlin \u5be6\u73fe\uff0c\u4e0d\u4f9d\u8cf4 Android SDK\u3002<\/p>\n<\/div>\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\" style=\"border-left-color:#d97706;border-left-width:4px;background-color:#f8fafc;padding-top:16px;padding-right:20px;padding-bottom:16px;padding-left:20px;margin-top:12px\">\n<p style=\"margin:0 0 8px 0;font-weight:700;color:#b45309;\">\ud83d\udd39 MODEL (\u8cc7\u6599\u6578\u64da\u5c64)<\/p>\n<p style=\"margin:0;color:#334155;\">\u53ea\u8ca0\u8cac\u8655\u7406\u8cc7\u6599\u5eab\u4e92\u52d5\uff08Room\/SQLite\uff09\u8207\u9060\u7aef API\uff08Retrofit\/Firebase\uff09\uff0c\u63d0\u4f9b\u8cc7\u6599\u7d66 Presenter \u505a\u6c7a\u7b56\u3002<\/p>\n<\/div>\n<p>\u8ddf MVC \u5be6\u4f5c\u4e0a\u7684\u6700\u5927\u5dee\u7570\uff0c\u5247\u662f\u8b93 Activity \u53ea\u55ae\u7d14\u5730\u53bb\u8655\u7406 View \u4efb\u52d9\uff0c\u628a\u539f\u672c\u5728 Activity \u4e2d\u7684 Controller \u908f\u8f2f\u4efb\u52d9\u5168\u90e8\u53e6\u5916\u62bd\u51fa\u653e\u5230 Presenter \u88e1\u3002\u6240\u6709\u7684\u4ea4\u4e92\u4efb\u52d9\u5247\u900f\u904e <code>View interface<\/code> \u548c <code>Presenter interface<\/code> \u4f86\u9032\u884c\uff0c\u9019\u975e\u5e38\u65b9\u4fbf\u65bc Unit test\u3002<\/p>\n<p>\ud83d\udc49 \u5be6\u969b\u7a0b\u5f0f\u78bc\uff1a<a href=\"https:\/\/github.com\/boochlin06\/FunnyVote\/tree\/mvp\" target=\"_blank\" rel=\"noreferrer noopener\">Funny Vote MVP version on GitHub<\/a><\/p>\n<p>\u5728\u6539\u7248\u6210 MVP \u67b6\u69cb\u7684\u540c\u6642\u4e5f\u88dc\u4e0a UT\uff0c\u6b63\u56e0\u70ba\u6709\u52a0\u5165\u6e2c\u8a66\u7684\u6982\u5ff5\uff0c\u66f4\u80fd\u7406\u89e3 MVP \u67b6\u69cb\u7684\u89e3\u8026\u512a\u9ede\u3002\u70ba\u4e86\u65b9\u4fbf\u6e2c\u8a66\u8003\u91cf\uff0c\u5728 Model, Presenter, View \u4ea4\u4e92\u7684 interface \u8a2d\u8a08\u6642\uff0c\u81ea\u7136\u800c\u7136\u5730\u6703\u4f7f\u5f97 code \u8b8a\u5f97\u66f4\u4e7e\u6de8\uff0c\u6211\u60f3\u9019\u61c9\u8a72\u5c31\u662f TDD \u7684\u7406\u5ff5\u3002\u96d6\u7136\u6211\u4e26\u6c92\u6709\u8b93 coverage rate \u885d\u9ad8\uff0c\u4e0d\u904e\u4e3b\u8981\u662f\u56e0\u70ba\u81ea\u5df1\u61f6\u5f97\u5565\u90fd\u52a0\u4e0a\u6e2c\u8a66\uff0c\u53ea\u4e3b\u8981\u6e2c\u8a66\u6838\u5fc3\u908f\u8f2f\u696d\u52d9\u3002<\/p>\n<h3 class=\"wp-block-heading\">MVP \u5be6\u4f5c\u958b\u767c\u5fc3\u5f97<\/h3>\n<p><strong>1. \u4e00\u500b View \u5c0d\u61c9\u4e00\u500b Presenter \u7684\u591a Fragment \u56f0\u5883<\/strong><\/p>\n<p>\u901a\u5e38\u4e00\u500b VIEW \u5c0d\u61c9\u4e00\u500b Presenter\u3002\u4f46\u662f\u5982\u679c\u4e00\u500b Activity \u5167\u90e8\u6709\u5169\u500b Fragment View \u52a0\u4e0a Activity \u672c\u8eab\u7684\u90e8\u5206 View\uff08\u50cf\u662f Funny Vote \u5275\u5efa\u6295\u7968\u9801\u9762\uff09\uff1a\u5148\u4e0d\u8aaa\u6211\u7576\u521d UI \u8a2d\u8a08\u662f\u600e\u9ebc\u60f3\u7684\uff0c\u6700\u521d\u7684\u601d\u8def\u8a8d\u70ba\u61c9\u8a72\u8b93\u4e00\u500b <code>CreateVotePresenter<\/code> \u53bb handle setting fragment view\u3001option fragment view \u4ee5\u53ca activity \u7684 title and image view\u3002<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"715\" height=\"1240\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_create_vote_screen.png\" alt=\"Funny Vote \u7d93\u5178 2017 \u5275\u5efa\u6295\u7968\u9801\u9762 (CreateVoteActivity)\" class=\"wp-image-1279\" style=\"max-width:360px;width:100%;height:auto;\" srcset=\"https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_create_vote_screen.png 715w, https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_create_vote_screen-173x300.png 173w, https:\/\/boochlin.com\/wp-content\/uploads\/2026\/09\/post320_create_vote_screen-590x1024.png 590w\" sizes=\"(max-width: 715px) 100vw, 715px\" \/><figcaption class=\"wp-element-caption\">Funny Vote \u7d93\u5178\u5275\u5efa\u6295\u7968\u9801\u9762 (CreateVoteActivity) \u00b7 \u591a Fragment \u5171\u4eab Presenter \u7684\u5be6\u8e10\u5834\u666f<\/figcaption><\/figure>\n<p>\u6703\u9019\u6a23\u60f3\uff0c\u53ea\u662f\u55ae\u7d14\u5730\u8a8d\u70ba Activity \u8ddf Fragment View \u4e4b\u9593\u7684\u4e92\u52d5\uff0c\u53ef\u4ee5\u5f88\u7c21\u55ae\u5730\u5728\u4e00\u500b Presenter \u4e2d\u63a7\u5236\uff0c\u53ef\u4ee5\u5c11\u6389\u5f88\u591a\u696d\u52d9\u4e0a\u7684 Callback\uff0c\u53ef\u4ee5\u8b93 code \u8b8a\u5f97\u6613\u8b80\u3002\u4f46\u662f\u4e8b\u60c5\u679c\u7136\u4e0d\u662f\u5c0f\u5f1f\u60f3\u7684\u90a3\u9ebc\u7c21\u55ae\uff0c\u5728\u7d93\u904e\u5be6\u4f5c\u5f8c\uff0c\u767c\u73fe\u5c31\u6703\u767c\u751f\u4e00\u500b Presenter \u7684 code \u592a\u591a\uff0c\u4e14\u592a\u591a View \u8981\u4e00\u8d77 ready\u3002\u9019\u5728\u4e00\u500b\u4eba\u958b\u767c\u6642\u9084\u597d\uff0c\u4f46\u5982\u679c\u662f\u591a\u4eba\u958b\u767c\u6642\uff0c\u5c0d\u65bc\u5354\u8abf interface \u8a2d\u8a08\u5de5\u4f5c\u5c31\u6703\u6709\u6975\u5927\u7684\u8ca0\u64d4\u3002<\/p>\n<p>\u7576\u7136\u9019\u4e26\u975e\u662f\u7d55\u5c0d\u7684\u539f\u5247\uff0c\u4e3b\u8981\u770b widget \u7684\u5167\u5bb9\u91cf\u4f86\u5b9a\u7fa9 MVP \u6703\u6bd4\u8f03\u597d\u3002\u81f3\u65bc Callback \u592a\u591a\u9019\u500b\u554f\u984c\uff0c\u7576\u7136\u4e0d\u662f\u53ea\u6709\u6211\u60f3\u5230\uff0c\u5404\u65b9\u5927\u795e\u65e9\u5df2\u6709\u89e3\u6cd5\uff0c\u4e5f\u5c31\u662f <strong>RxJava<\/strong>\u3002\u8a72\u6709\u7684 callback \u662f\u5c11\u4e0d\u4e86\uff0c\u4f46\u662f\u53ef\u4ee5\u8b93 callback \u597d\u5275\u9020\u3001\u597d\u7ba1\u7406\uff0c\u9019\u5c31\u662f\u597d\u6846\u67b6\u3002<\/p>\n<p><strong>2. \u56b4\u7981\u5728 Presenter \u51fa\u73fe Android SDK \u76f8\u4f9d<\/strong><\/p>\n<p>Presenter \u4e0d\u61c9\u8a72\u6709\u4efb\u4f55\u76f8\u95dc Android \u7684 Object and Method\uff08\u4f8b\u5982 Context\u3001View \u7b49\uff09\uff0c\u9019\u6c92\u5565\u597d\u8cea\u7591\u7684\uff0c\u53ea\u662f\u6211\u60f3\u4f86\u81ea\u6211\u63d0\u9192\u800c\u5df2\uff0c\u4e0d\u7136\u4ee5\u5f8c\u5beb UT \u53ea\u6703\u60f3\u54ed\u3002Presenter \u4e5f\u4e0d\u61c9\u8a72\u50b3\u5165 Adapter \u88e1\u3002<\/p>\n<p><strong>3. Model \u5c64\u7684\u8cc7\u6599\u4f86\u6e90\u9694\u96e2\u8207\u6a23\u677f\u6a21\u5f0f<\/strong><\/p>\n<p>Model \u8a2d\u8a08\u4e0a\uff0c\u5982\u679c\u5e38\u5e38\u6703\u9047\u5230\u5f8c\u7aef\u9084\u672a ready \u6642\uff0c\u5e38\u5e38\u9700\u8981\u81ea\u5df1\u5148\u5728 local fake data\uff0c\u7576\u5f8c\u7aef\u5b8c\u6210\u5f8c\u518d\u91cd\u65b0\u63a5\u4e0a\u3002\u9019\u7a2e\u60c5\u6cc1\u4e0b\u975e\u5e38\u9069\u5408\u4f7f\u7528\u6a23\u677f\u6a21\u5f0f\uff0c\u642d\u914d\u4e0a Injection \u548c build variant \u53ef\u4ee5\u7c21\u55ae\u5340\u5206\u51fa\u4f55\u6642\u7528\u4f55\u7a2e content\uff0c\u8d85\u7d1a\u8212\u723d\u3002\u5728\u9019\u500b\u6982\u5ff5\u4e4b\u4e0a\uff0c\u540c\u6642\u65b9\u4fbf\u6e2c\u8a66\u7684\u76ee\u7684\uff0cDagger \u7684\u4f9d\u8cf4\u6ce8\u5165\u6846\u67b6\u4e5f\u56e0\u6b64\u88ab\u63d0\u51fa\u4f86\u3002<\/p>\n<p><strong>4. \u6e2c\u8a66\u6846\u67b6\u898f\u7bc4\u8207\u547d\u540d\u5b78\u554f<\/strong><\/p>\n<p>\u6e2c\u8a66\u6846\u67b6\u5728\u6c92\u6709\u7279\u6b8a\u9700\u6c42\u4e0b\uff0c\u5247\u4f9d\u64da <a href=\"https:\/\/developer.android.com\/training\/testing\/\" target=\"_blank\" rel=\"noreferrer noopener\">Google \u5efa\u8b70<\/a>\u7684 Mockito \u505a UT\u3001Espresso \u505a Instrument test\u3002\u76ee\u524d\u611f\u89ba\u5c31\u662f\u5beb\u800c\u5df2\uff0c\u4e5f\u6c92\u7528\u904e\u5225\u7684\u6e2c\u8a66\u6846\u67b6\u3002\u786c\u8981\u8aaa\u7684\u8a71\u5c31\u662f test method \u7684\u547d\u540d\u600e\u9ebc\u6a23\u6bd4\u8f03\u597d\uff08\u53c3\u8003<a href=\"http:\/\/teddy-chen-tw.blogspot.com\/2016\/05\/blog-post_12.html\" target=\"_blank\" rel=\"noreferrer noopener\">\u5927\u5e2b Teddy Chen \u8aaa\u6cd5<\/a>\uff09\uff1a\u4e00\u76f4\u4ee5\u4f86\u90fd\u662f Presenter, View UT \u7528\u300c\u60f3\u6e2c\u5565\u529f\u80fd\u300d\u4f5c\u70ba\u547d\u540d\u898f\u5247\uff0cModel UT \u5247\u591a\u63a1\u7528\u300c\u9810\u671f\u884c\u70ba\u52a0\u4e0a\u6e2c\u8a66\u72c0\u614b\u300d\u4f5c\u70ba\u547d\u540d\u898f\u5247\uff0cInstrument test \u5247\u4ee5 <code>Given-When-Then<\/code> \u70ba\u547d\u540d\u57fa\u6e96\u3002\u8001\u5be6\u8aaa\u9019\u6a23\u597d\u4e0d\u597d\u6211\u4e5f\u4e0d\u9069\u5f88\u6e05\u695a\uff0c\u500b\u4eba\u89ba\u5f97\u6709\u898f\u5247\u5c31\u597d\u3002<\/p>\n<p><strong>5. CI\/CD \u81ea\u52d5\u5316\u8207 JaCoCo \u4ee3\u78bc\u8986\u84cb\u7387<\/strong><\/p>\n<p>CI \u9019\u500b\u4ee5\u524d\u5b8c\u5168\u6c92\u6709\u6478\u904e\uff0c\u4f46\u662f\u807d\u8aaa Travis \u8d85\u7c21\u55ae\uff0c\u6240\u4ee5\u6211\u5c31\u7d66\u4ed6\u84cb\u4e0b\u53bb\u4e86\uff0c\u8001\u5be6\u8aaa\u723d\u611f\u5341\u8db3\uff0c\u770b\u4ed6\u5728\u90a3\u908a\u81ea\u52d5\u5316\uff0c\u6211\u5728\u770b YouTube \u771f\u7684\u65b9\u4fbf\u3002\u57fa\u672c\u529f\u80fd\u53ea\u9a57\u8b49\u80fd\u4e0d\u80fd build \u904e\uff0c\u5982\u679c\u6709\u5beb\u6e2c\u8a66\u7684\u8a71\uff0c\u5247\u6703\u9806\u624b\u8dd1\u5b8c\u3002\u53ef\u662f\u537b\u767c\u73fe\u7528 Android Studio \u90fd\u5167\u5efa\u986f\u793a coverage rate \u7bc4\u570d\uff0c\u7d50\u679c Travis \u6c92\u6709\u9806\u624b\u63d0\u4f9b\u9019\u529f\u80fd\u4e0d\u662f\u592a\u53ef\u60dc\u4e86\u55ce\uff0c\u56e0\u6b64\u4e5f\u6574\u5165 JaCoCo\uff0c\u6e2c\u8a66\u5b8c\u5f8c\u81ea\u52d5 gen report\uff08\u53c3\u8003<a href=\"https:\/\/github.com\/codecov\/example-android\" target=\"_blank\" rel=\"noreferrer noopener\">\u5927\u795e\u6574\u5408\u597d\u7684\u7bc4\u4f8b<\/a>\uff09\u3002<\/p>\n<blockquote class=\"wp-block-quote\" style=\"background-color:#f1f5f9;border-left:4px solid #64748b;padding:16px 20px;margin:24px 0;border-radius:4px;\">\n<p style=\"font-style:italic;margin-bottom:8px;color:#1e293b;\">\u201cYou need to provide a way of accessing the member variables so you can pass in a mock (the most common ways would be a setter method or a constructor which takes a parameter).\u201d<\/p>\n<p style=\"font-style:italic;margin:0;color:#1e293b;\">\u201cIf your code doesn\u2019t provide a way of doing this, it\u2019s incorrectly factored for TDD (Test Driven Development).\u201d<\/p>\n<p><cite style=\"display:block;margin-top:10px;font-size:13px;color:#64748b;font-style:normal;\">\u2014 \u67e5\u8cc7\u6599\u6642\u7bc0\u9304\u7684\u540d\u8a00<\/cite>\n<\/p><\/blockquote>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<h2 class=\"wp-block-heading\">\u4e8c\u3001 MVP with Dagger 2<\/h2>\n<p>\u5728\u4e0a\u4e00\u7ae0\u7bc0\u8ac7\u5230\u5169\u500b\u53ef\u4ee5\u5728 MVP \u67b6\u69cb\u4e0a\u4f7f\u7528\u7684\u6846\u67b6\uff0c\u4e00\u500b\u662f\u89e3\u6c7a\u5927\u91cf Callback \u4ee5\u53ca\u6d41\u7a0b\u5316\u7684 <strong>RxJava<\/strong>\uff0c\u53e6\u4e00\u500b\u662f\u89e3\u6c7a\u5927\u91cf new \u4f9d\u8cf4\u4e14\u65b9\u4fbf Injection \u7ba1\u7406\u7684 <strong>Dagger 2<\/strong>\u3002\u800c\u9019\u500b\u7ae0\u7bc0\u5247\u662f\u8ac7\u5f15\u5165 Dagger 2 \u7684\u5fc3\u5f97\u3002\u4f60\u8aaa\u70ba\u4f55\u6c92\u6709\u4e00\u8d77\u5f15\u5165 RxJava\uff0c\u96e3\u9053\u90a3\u4e9b\u554f\u984c\u4e0d\u7528\u4e00\u8d77\u89e3\u6c7a\u55ce\uff1f\u6211\u7d55\u5c0d\u4e0d\u6703\u8aaa\u56e0\u70ba\u770b\u8d77\u4f86\u592a\u7c21\u55ae\u4e86\uff0c\u6240\u4ee5\u6211\u61f6\u5f97\u7528\uff0c\u800c\u662f\u56e0\u70ba Kotlin \u5c31\u76f4\u63a5\u628a\u9019\u584a\u6574\u5408\u5f97\u66f4\u597d\u3002\u96d6\u7136\u6211\u77e5\u9053\u9019\u6709\u9ede\u96de\u817f\u6bd4\u61f6\u89ba\u7684\u554f\u984c\uff0c\u4f46\u662f\u56e0\u70ba\u9019\u4e4b\u5f8c\u6211\u6253\u7b97\u76f4\u63a5\u5beb Kotlin \u7248\u672c\uff0c\u6240\u4ee5\u5c31\u5ffd\u7565\u4e86\u3002<\/p>\n<p>\u6839\u64da\u4e0a\u9762\u4e00\u5806\u5c41\u8a71\uff0c\u76f4\u63a5\u958b\u59cb Dagger \u6574\u5408\u3002\u901a\u5e38\u4e00\u958b\u59cb\u67e5\u627e\u8cc7\u6599\u90fd\u6703\u807d\u5230\u300cMVP + Retrofit + RxJava + Dagger\u300d\u6574\u500b\u8d85\u8212\u723d\uff0c\u4f46\u662f\u63a5\u4e0b\u4f86\u5c31\u6703\u770b\u5230\u8aaa Dagger \u7684\u5b78\u7fd2\u96e3\u5ea6\u6bd4\u8f03\u9ad8\uff0c\u5927\u90e8\u5206\u7528\u5728\u5927\u578b\u5354\u4f5c\u5c08\u6848\u6bd4\u8f03\u9069\u5408\u3002\u6069~\u8001\u5be6\u8aaa\u6211\u89ba\u5f97\u9084\u884c\u554a\uff0c\u4ee5\u524d\u6709\u73a9\u904e\uff0c\u539f\u672c\u9810\u4f30\u500b 8 \u5c0f\u6642\u61c9\u8a72\u53ef\u4ee5\u641e\u5b9a\uff0c\u6700\u5f8c\u82b1\u4e86 15 \u5c0f\u6642\uff0c\u662f\u6bd4\u8f03\u9ebb\u7169\u5566\uff0c\u4e3b\u8981\u662f Dagger \u7684\u932f\u8aa4\u63d0\u793a\u8a0a\u606f\u6709\u9ede\u96e3\u61c2\u3002<\/p>\n<p>\u9019\u7a2e\u6771\u897f\u5c31\u662f\u521d\u59cb\u5316\u9ebb\u7169\uff0c\u4f46\u662f\u5f8c\u4f86\u7528\u8d77\u4f86\u6574\u500b\u8212\u723d\uff01\u6700\u660e\u986f\u7684\u5c31\u662f MVP \u7248\u672c\u6642\u6709\u4e00\u500b <code>Injection.java<\/code>\uff0c\u9019\u6771\u897f\u53ef\u4ee5\u6839\u64da\u653e\u7684\u8cc7\u6599\u593e\u4ee5\u53ca build variant \u53bb\u63d0\u4f9b debug version, prod version \u4e0d\u4e00\u6a23\u7684\u5be6\u4f5c\u6ce8\u5165\uff1b\u4f46\u662f Dagger \u5c31\u4e0d\u7528\u4e86\u554a\uff01\u7b49\u7b49\uff0c\u597d\u50cf\u53ea\u662f\u79fb\u5230 Module \u4e86\u3002\u4e0d\u8981\u5c0f\u770b\u9019\u500b Module, Component\uff0c\u4e00\u5806\u5947\u5999\u7684 <code>new<\/code> \u5c31\u901a\u901a\u4e0d\u898b\u4e86\uff08\u89e3\u8026\u7684\u6027\u8cea\u5c31\u51fa\u73fe\u4e86\uff09\uff0c\u4e5f\u4e0d\u7528\u7ba1\u7406\u7269\u4ef6\u7684\u751f\u547d\u9031\u671f\uff08\u5c08\u6ce8\u696d\u52d9\u908f\u8f2f\uff09\uff0c\u5728\u7ba1\u7406\u4e0a\u4e5f\u56e0\u70ba\u6709\u597d\u597d\u7684\u5206\u985e\uff0c\u958b\u767c\u6548\u7387\u4e5f\u6703\u63d0\u9ad8\uff08\u6e05\u6670\u7684 code\uff09\uff0c\u64b0\u5beb UT \u4e5f\u6703\u65b9\u4fbf\u5f97\u591a\u3002<\/p>\n<p>\ud83d\udc49 \u5be6\u969b\u7a0b\u5f0f\u78bc\uff1a<a href=\"https:\/\/github.com\/boochlin06\/FunnyVote\/tree\/mvp_dagger\" target=\"_blank\" rel=\"noreferrer noopener\">Funny Vote MVP with Dagger2 version on GitHub<\/a><\/p>\n<h3 class=\"wp-block-heading\">Dagger \u5be6\u4f5c\u958b\u767c\u5fc3\u5f97<\/h3>\n<ul class=\"wp-block-list\">\n<li><strong>\u7406\u89e3\u5c0e\u5165\u6846\u67b6\u7684\u4ee3\u50f9<\/strong>\uff1a\u4f7f\u7528\u4efb\u4f55\u65b0\u6846\u67b6\u958b\u767c\u90fd\u8acb\u5148\u7406\u89e3\u70ba\u4f55\u8981\u5c0e\u5165\u3002\u4ee5 Dagger \u4f86\u8aaa\uff0c\u4e3b\u8981\u662f\u89e3\u6c7a\u4f9d\u8cf4\u56b4\u91cd\u7684\u554f\u984c\uff0c\u63a5\u8457\u5c31\u6703\u5f15\u5c0e\u5230\u65b9\u4fbf\u6e2c\u8a66\u7684\u597d\u8655\u3002\u4f46\u662f\u5c0e\u5165\u6846\u67b6\u90fd\u6703\u6709\u5b83\u61c9\u8a72\u4ed8\u51fa\u7684\u4ee3\u50f9\uff0c\u500b\u4eba\u89ba\u5f97\u6700\u7169\u7684\u662f\u521d\u59cb\u5316\u6d41\u7a0b\uff0c\u63a5\u8457\u70ba\u4e86\u8b93 Module, Component \u6574\u500b\u6d41\u7a0b\u80fd\u9054\u5230\u6ce8\u5165\u7684\u6548\u679c\uff0c\u5927\u6982\u6703\u591a\u51fa\u5169\u500d\u7684\u63a5\u53e3\u8a2d\u8a08\uff0c\u771f\u7684\u4e00\u822c\u5c0f\u578b\u958b\u767c\u7684 solo \u73a9\u5bb6\u5011\uff0c\u53ef\u4ee5\u597d\u597d\u601d\u8003\u9700\u8981\u55ce\uff1f<\/li>\n<li><strong>\u6a23\u677f\u4ee3\u78bc\u7c21\u5316\u4e4b\u9053<\/strong>\uff1a\u7531\u65bc\u591a\u51fa\u5927\u91cf Module, Component \u7684\u7269\u4ef6\u548c\u63a5\u53e3\u8a2d\u8a08\uff0c\u9019\u5c31\u662f\u4e00\u500b\u9ebb\u7169\u7684\u554f\u984c\uff0c\u5beb\u7684\u7a0b\u5f0f\u78bc\u8d8a\u591a\u5c31\u8d8a\u6709\u6a5f\u6703\u51fa BUG\u3002\u90a3\u9ebc\u554f\u984c\u4f86\u4e86\uff0c\u5c31\u8981\u601d\u8003\u6709\u6c92\u6709\u8fa6\u6cd5\u7c21\u5316\u3002\u56e0\u70ba\u5927\u90e8\u5206\u7684 Module, Component \u96d6\u7136\u4e00\u5806\u63a5\u53e3\u8981\u8a2d\u8a08\uff0c\u4f46\u5927\u90fd\u662f\u6a21\u677f\u5316\u7684\u4ee3\u78bc\uff0c\u679c\u7136 Google \u5927\u795e\u63d0\u51fa\u4e86 <a href=\"https:\/\/google.github.io\/dagger\/android\" target=\"_blank\" rel=\"noreferrer noopener\">dagger.android<\/a> \u7684\u5de5\u5177\uff0c\u7528 Annotation \u53bb\u7701\u7565\u6a21\u677f\u4ee3\u78bc\uff0c\u8b93 Compiler \u53bb\u81ea\u52d5\u751f\u6210\u7a0b\u5f0f\u78bc\u3002<\/li>\n<li><strong>\u6a21\u7d44\u89e3\u8026\u5f8c\uff0c\u52d9\u5fc5\u5beb\u6e2c\u8a66<\/strong>\uff1a\u8212\u723d\uff01<\/li>\n<\/ul>\n<blockquote class=\"wp-block-quote\" style=\"background-color:#f1f5f9;border-left:4px solid #64748b;padding:16px 20px;margin:24px 0;border-radius:4px;\">\n<p style=\"font-style:italic;margin-bottom:8px;color:#1e293b;\">\u201cDependency injection frameworks can simplify the code you write and provide an adaptive environment that\u2019s useful for testing and other configuration changes.\u201d<\/p>\n<p style=\"font-style:italic;margin:0;color:#1e293b;\">\u201cIf you intend to use a dependency injection framework in your app, consider using <a href=\"http:\/\/google.github.io\/dagger\/\" target=\"_blank\" rel=\"noreferrer noopener\">Dagger 2<\/a>. Dagger does not use reflection to scan your app\u2019s code. Dagger\u2019s static, compile-time implementation means that it can be used in Android apps without needless runtime cost or memory usage.\u201d<\/p>\n<p><cite style=\"display:block;margin-top:10px;font-size:13px;color:#64748b;font-style:normal;\">\u2014 Google \u5b98\u65b9\u6587\u6a94\u5c0d Dagger 2 \u7684\u8a55\u50f9<\/cite>\n<\/p><\/blockquote>\n<p>\u4ee5\u4e0a\u662f\u672c\u6b21\u5c0f\u5f1f\u7684\u611f\u60f3\uff0c\u6211\u771f\u7684\u61f6\u5f97\u8cbc CODE\uff0c\u5404\u4f4d\u771f\u7684\u60f3\u770b\u53bb GitHub \u5427\uff0c\u901a\u901a\u90fd\u653e\u5728\u90a3\u3002\u81f3\u65bc Kotlin \u7248\u672c\u6211\u5247\u653e\u5230\u4e0b\u4e00\u7bc7\uff0c\u5c0d\u5c0f\u5f1f\u4f86\u8aaa Kotlin \u7b97\u662f\u771f\u7684\u5b8c\u5168\u6c92\u7528\u904e\uff0c\u6240\u4ee5\u5074\u91cd\u9ede\u6703\u591a\u653e\u5728\u600e\u9ebc\u7528\uff0c\u8ddf\u672c\u7bc7\u6027\u8cea\u4e0d\u592a\u76f8\u540c\uff0c\u6240\u4ee5\u65b0\u958b\u6587\u7ae0\uff01<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n<div class=\"wp-block-group\" style=\"border:1px solid #e2e8f0;background:#ffffff;padding:18px 20px;margin-top:32px;margin-bottom:24px;border-radius:8px;box-shadow:0 1px 3px rgba(0,0,0,0.05)\">\n<p style=\"margin:0 0 12px 0;font-size:0.9em;color:#888;font-weight:bold;text-transform:uppercase;letter-spacing:1px\">\u300aFunny Vote \u5341\u5e74\u6f14\u9032\u7cfb\u5217\u300b\u7ae0\u7bc0\u5c0e\u822a<\/p>\n<div style=\"display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:12px;font-size:0.95em\">\n<div style=\"flex:1;min-width:200px\">\u4e0a\u4e00\u7bc7\uff1a<a href=\"https:\/\/boochlin.com\/?p=289\" style=\"color:#C14420;text-decoration:underline\">\u5f9e\u96f6\u5230\u4e00\u7684\u7368\u7acb\u958b\u767c\u96dc\u8a18\uff08\u4e00\uff09<\/a><\/div>\n<div style=\"text-align:center\"><a href=\"https:\/\/boochlin.com\/?page_id=1319\" style=\"color:#C14420;font-weight:bold;text-decoration:none\">\u5168\u7cfb\u5217\u7e3d\u89bd<\/a><\/div>\n<div style=\"flex:1;min-width:200px;text-align:right\">\u4e0b\u4e00\u7bc7\uff1a<a href=\"https:\/\/boochlin.com\/?p=1284\" style=\"color:#C14420;text-decoration:underline\">Kotlin \u8207 RxJava \u7684\u9072\u4f86\u6551\u8d16\uff08\u4e09\uff09<\/a><\/div>\n<\/p><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u70ba\u300aFunny Vote \u5341\u5e74\u6f14\u9032\u7cfb\u5217\u300b\u7b2c 2 \u7bc7\uff08\u5168\u7cfb\u5217\u5171 8 \u7bc7\uff09\uff5c\u67e5\u770b\u5168\u7cfb\u5217\u67b6\u69cb\u5c0e\u89bd \u597d\u4e00\u9663\u5b50\u5fd8\u8a18\u8981 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1266,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[179,253,259,304],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/320"}],"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=320"}],"version-history":[{"count":4,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/320\/revisions"}],"predecessor-version":[{"id":1465,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/320\/revisions\/1465"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/media\/1266"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}