Hello Worldã®äœæã¯åžžã«ç°¡åã§ãã ã³ãŒãã¯ã·ã³ãã«ã§ããããããããã«èŠããSDKã¯ã客æ§ã®ããŒãºã«åãããŠèª¿æŽãããŠããããã§ãã ããããããæŽç·ŽãããAndroidã¢ããªã±ãŒã·ã§ã³ãäœæããçµéšãããå Žåã¯ãåäœããã³ãŒãã®ãã¹ãŠãééã£ãŠããããšãããããŸãã WiFiãå©çšã§ããªãå Žåã«é»è©±ã®åããå€æŽããåŸãã·ã§ããã³ã°ã«ãŒããæŽæ°ãããªãçç±ãç解ããããšããŠäœæéãè²»ããããšãã§ããŸãã åé¡ã®è§£æ±ºçã¯ãã¢ã¯ãã£ããã£ã®457è¡ã®
onCreate()
ã¡ãœããã®å ŽåãAndroid 4.1ãæèŒããSamsungã§ã¯ã©ãã·ã¥ãä¿®æ£ããã³ãŒããšãã¯ãŒãã³ã衚瀺ããã³ãŒãã®éã®ã©ããã«å¥ã®ãŠãŒã¶ãŒã®èªçæ¥ããšã«5ãã«ã è¯ãæ¹æ³ããããŸã
RemindïŒ ããã Perã-èè ã®å€åããäŒç€Ÿã®ååïŒã§ã¯ã2é±éããšã«æ°ããæ©èœãå±éããŠããŸãããã®ã¹ããŒããšè£œåã®é«å質ãç¶æããããã«ãã³ãŒããã·ã³ãã«ã§ããµããŒããããåå²ãããç¶æ ã«ä¿ã€æ¹æ³ãå¿ èŠã§ãïŒ ããããççµåã®æå³ã§ã®ãåé¢ãïŒããã³ãã¹ãå¯èœã MVPã¢ãŒããã¯ãã£ãã¿ãŒã³ã䜿çšãããšããããå®è¡ããã³ãŒãã®æãéèŠãªéšåã§ããããžãã¹ããžãã¯ã«éäžã§ããŸãã
MVPãŸãã¯Model-View-Presenterã¯ããŠãŒã¶ãŒã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ããéã®è²¬ä»»ã®å ±æã«å¯äžããããã€ãã®ãã¿ãŒã³ã®1ã€ã§ãã ãããã®ãã¿ãŒã³ã®ããããã§ãã¬ã€ã€ãŒã®åœ¹å²ã¯ãããã«ç°ãªããŸãã ãã®èšäºã®ç®çã¯ããã¿ãŒã³éã®éãã説æããããšã§ã¯ãªãããããAndroidïŒ RailsãiOSãªã©ã®ææ°ã®UIãã¬ãŒã ã¯ãŒã¯ã«äŒŒãŠããŸãïŒã«é©çšããæ¹æ³ãšãã¢ããªã±ãŒã·ã§ã³ãããããã©ã®ããã«å©çãåŸããã瀺ãããšã§ãã
以äžã«èª¬æããã»ãšãã©ã®ã¢ãããŒãã瀺ããµã³ãã«ã³ãŒãã¯ãããã«ãããŸãã
https://github.com/remind101/android-arch-sample
ãªãŒã«ãã¹ã¯ãŒã«Android
Androidãã¬ãŒã ã¯ãŒã¯ã«ãã£ãŠæ瀺ããã責任ã®åé¢ã¯ã次ã®ããã«ãªããŸãã ã¢ãã«ã¯ä»»æã®POJOã§ããã ãã¥ãŒã¯XMLããŒã¯ã¢ããã§ããããã©ã°ã¡ã³ãïŒãŸãã¯æåã®ã¢ã¯ãã£ããã£ïŒã¯Controller / PresenterãšããŠæ©èœããŸã ã çè«çã«ã¯ãããã¯éåžžã«ããŸãæ©èœããŸãããã¢ããªã±ãŒã·ã§ã³ãæé·ãããšããã«ããã¥ãŒã«é¢é£ããå€ãã®ã³ãŒããã³ã³ãããŒã©ãŒã«è¡šç€ºãããŸãã ããã¯ãXMLã§ã§ããããšã¯ããŸããªãããããã¹ãŠã®ããŒã¿ãã€ã³ãã£ã³ã°ïŒããŒã¿ãã€ã³ãã£ã³ã°ïŒãã¢ãã¡ãŒã·ã§ã³ãå ¥ååŠçãªã©ããããžãã¹ããžãã¯ãšå ±ã«ãã©ã°ã¡ã³ãã§è¡ãããããã§ãã
è€éãªã€ã³ã¿ãŒãã§ã€ã¹èŠçŽ ããªã¹ããŸãã¯ã°ãªããã«é 眮ããããšããã¹ãŠãããã«æªåããŸãïŒ çŽ -ãGridView / GridLayoutãæããå®éã«ãã°ãªããèŠçŽ ããæããŸãïŒã çŸåšãã¢ããã¿ãŒã«ã¯ãããããã¹ãŠã®èŠçŽ ã®ãã¥ãŒãšã³ã³ãããŒã©ãŒã³ãŒããæ ŒçŽããã ãã§ãªããããããã³ã¬ã¯ã·ã§ã³ãšããŠç®¡çãã責任ããããŸãã ãããã®èŠçŽ ã¯ãã¹ãŠå¯æ¥ã«é¢é£ããŠãããããä¿å®ãéåžžã«é£ãããªãããã¹ããããã«é£ãããªããŸãã
Model-View-Presenterã®çŽ¹ä»
MVPã䜿çšãããšãã€ã³ã¿ãŒãã§ã€ã¹ã衚瀺ããŠãã¥ãŒã§æäœããããã«å¿ èŠãªéå±ãªäœã¬ãã«ã®Androidã³ãŒãããã¹ãŠåŒ·èª¿è¡šç€ºã ãã¢ããªã±ãŒã·ã§ã³ãè¡ãå¿ èŠã®ããé«ã¬ãã«ã®ããžãã¹ããžãã¯ãPresenterã«ç§»åã§ããŸãã
ãããå®çŸããã«ã¯ãAndroidã§ãã¢ã¯ãã£ããã£ãŸãã¯ãã©ã°ã¡ã³ãããã¬ãŒã³ããŒã·ã§ã³ã¬ã€ã€ãŒãšèŠãªãããã¬ãŒã³ããŒã·ã§ã³ãå¶åŸ¡ããããã®è»œéãªãã¬ãŒã³ã¿ãŒãæäŸããå¿ èŠããããŸãã æãéèŠãªããšã¯ãåå±€ã®è²¬ä»»ã決å®ãããããã®éã®ã€ã³ã¿ãŒãã§ãŒã¹ãæšæºåããããšã§ãã åé¢ã®äžè¬çãªèª¬æã¯æ¬¡ã®ãšããã§ãã
ãã¥ãŒïŒã¢ã¯ãã£ããã£ãŸãã¯ãã©ã°ã¡ã³ãïŒã®åœ¹å²ã¯æ¬¡ã®ãšããã§ãã
- ãã¬ãŒã³ã¿ãŒã€ã³ã¹ã¿ã³ã¹ãšãã®ã¢ã¿ããã¡ã³ã/ãã¿ããã¡ã³ãã¡ã«ããºã ã®äœæã
- çºè¡šè ã«éèŠãªã©ã€ããµã€ã¯ã«ã€ãã³ãã«ã€ããŠèŠåããã
- å ¥åã€ãã³ãã«é¢ããçºè¡šè ãžã®ã¡ãã»ãŒãžã
- ãã¥ãŒã®é 眮ãšããŒã¿ãšã®æ¥ç¶ã
- ã¢ãã¡ãŒã·ã§ã³
- ã€ãã³ã远跡;
- ä»ã®ç»é¢ã«åãæ¿ããŸãã
ãã¬ãŒã³ã¿ãŒã¯ä»¥äžãæ åœããŸãã
- ã¢ãã«ãããŠã³ããŒãããŠãã ããã
- ã¢ãã«åç §ãšè¡šç€ºç¶æ ã®ä¿åã
- ç»é¢ã«è¡šç€ºãããã®ããã©ãŒãããããããã衚瀺ãããããã¥ãŒã«æ瀺ããŸãã
- ãªããžããªïŒããŒã¿ããŒã¹ããããã¯ãŒã¯ãªã©ïŒãšã®çžäºäœçšïŒ çŽãªããžããªããš -ããã¯ãã¿ãŒã³ã§ãã念ã®ããïŒã
- ãã¥ãŒããå ¥åã€ãã³ããåä¿¡ãããšãã«å¿ èŠãªã¢ã¯ã·ã§ã³ã®æ±ºå®ã
ãã¬ãŒã³ããŒã·ã§ã³ãšãã¬ãŒã³ã¿ãŒã®éã®ã€ã³ã¿ãŒãã§ã€ã¹ã®äŸã次ã«ç€ºããŸãã
interface MessageView { // , // void setMessageBody(String body); void setAuthorName(String name); void showTranslationButton(boolean shouldShow); // void goToUserProfile(User user); } interface MessagePresenter { // , // void onStart(); // void onAuthorClicked(); void onThreeFingersSwipe(); }
ãã®ã€ã³ã¿ãŒãã§ã€ã¹ã«é¢é£ããŠæ€èšãã䟡å€ã®ããèå³æ·±ãç¹ãããã€ããããŸãã
- ãã¬ãŒã³ããŒã·ã§ã³ãæŽæ°ããæ¹æ³ã¯åçŽã§ãå¥ã®èŠçŽ ã察象ãšããå¿
èŠããããŸãã 衚瀺ããå¿
èŠããããã®ããã©ãŒãããããã®ã¯ãã¬ãŒã³ã¿ãŒã®è²¬ä»»ã§ããããããã¹ãŠãæŽæ°ãã1ã€ã®
setMessage(Message message)
ã¡ãœãããæã€ãããåªããŠããŸãã ããšãã°ãå°æ¥ãçŸåšã®ãŠãŒã¶ãŒãã¡ãã»ãŒãžã®äœæè ã§ããããããããžãã¹ããžãã¯ã®äžéšã§ããå ŽåããŠãŒã¶ãŒåã®ä»£ããã«ãããªããã衚瀺ããå¿ èŠããããŸãã - ãã¬ãŒã³ã¿ãŒã®ã©ã€ããµã€ã¯ã«ã€ãã³ãã¡ãœããã¯ã·ã³ãã«ã§ãããçã®ïŒé床ã«è€éãªïŒã·ã¹ãã ã©ã€ããµã€ã¯ã«ãåæ ãã¹ãã§ã¯ãããŸããã ããããåŠçããå¿ èŠã¯ãããŸããã ãã ãããã¬ãŒã³ã¿ãŒã«ãã®ãµã€ã¯ã«ã®ããŸããŸãªæ®µéã§ããã€ãã®ã¢ã¯ã·ã§ã³ãå®è¡ããããå Žåã¯ããã®äžã®å¥œããªã ãåŠçã§ããŸãã
- ãã¬ãŒã³ã¿ãŒã®å ¥åã€ãã³ãã¯é«ã¬ãã«ã®ãŸãŸã§ããå¿ èŠããããŸãã ããšãã°ã3æ¬æã®ã¹ã¯ã€ããªã©ã®è€éãªãžã§ã¹ãã£ãå®çŸ©ããå Žåããã®ã€ãã³ããšä»ã®ã€ãã³ãã¯ãã¥ãŒã«ãã£ãŠæ±ºå®ãããå¿ èŠããããŸãã
- ã¡ãœãã
MessagePresenter.onAuthorClicked()
ããã³MessageView.goToAuthorProfile()
泚æãæãããšãã§ããŸãã ãã¥ãŒã®å®è£ ã«ã¯ããã®ãã¬ãŒã³ã¿ãŒã¡ãœãããåŒã³åºãã¯ãªãã«ãŒãå«ãŸããŠããå¯èœæ§ãé«ãããã®ã¯ãªãã«ãŒã¯goToAuthorProfile()
ãåŒã³åºããŸãã ããããã¹ãŠåé€ããŠããããã®ã¯ãªãã¯ããçŽæ¥èè ã®ãããã¡ã€ã«ã«ç§»åããå¿ èŠããããŸããã ããïŒ ååãã¯ãªãã¯ãããšãã«ãŠãŒã¶ãŒãããã¡ã€ã«ã«åãæ¿ãããã©ããã®æ±ºå®ã¯ãããžãã¹ããžãã¯ã®äžéšã§ããããã¬ãŒã³ã¿ãŒããããæ åœããŸãã
å®éã«å€æããããã«ããã¬ãŒã³ã¿ãŒã®ã³ãŒãã«çŽç²ãªJavaã ãã§ãªãAndroidãã¬ãŒã ã¯ãŒã¯ã®ã³ãŒããå«ãŸããŠããå Žåãããããäœãééã£ãããšãããŠããããšã«ãªããŸãã ãããã£ãŠããã¥ãŒã«ã¢ãã«ãžã®åç §ãå¿ èŠãªå Žåã¯ãæããã«äœãééã£ãããšãããŠããããã§ãã
ãã¹ãåé¡ãçºçãããšããã«ããã¹ãããå¿ èŠãããã³ãŒãã®ã»ãšãã©ããã¬ãŒã³ã¿ãŒã«ãªããŸãã ãã°ãããã®ã¯ããã®ã³ãŒãã¯Androidã®ã³ã³ããã¹ãã§ã®å®è£ ã§ã¯ãªããç®çã®ãã¥ãŒã®ã€ã³ã¿ãŒãã§ã€ã¹ãžã®ãªã³ã¯ã®ã¿ãæã£ãŠãããããAndroidãå®è¡ããå¿ èŠããªãããšã§ãã ããã¯ããã¥ãŒã€ã³ã¿ãŒãã§ãŒã¹ããŠã§ããã«ããã¡ãœããããŠã§ãããã¥ãŒã«å¯ŸããŠæ£ããåŒã³åºãããããšã確èªããããžãã¹ããžãã¯ã®çŽç²ãªJUnitãã¹ããæžãããšãã§ããããšãæå³ããŸãã ããããã¹ãã®å€èŠ³ã§ãã
ãªã¹ãã¯ã©ãã§ããïŒ
ãããŸã§ãç§ãã¡ã®ã¢ã€ãã¢ã¯è¡å䞻矩ãšæçã§ãããšæ³å®ããŠããŸããããå®éã«ã¯äœã§ãæ§ããŸããã ãã¥ãŒã€ã³ã¿ãŒãã§ãŒã¹ãå®è£ ããViewHolder ïŒ
RecyclerView.ViewHolder
ãšListViewãšãšãã«äœ¿çšããéåžžã®å€ãViewHolderã®äž¡æ¹ïŒã䜿çšããŠããªã¹ããéåžžã«ããŸãæ©èœãããããšãã§ããŸãã ã ã¢ããã¿ãŒã§ã¯ããã¬ãŒã³ã¿ãŒã®ã¢ã¿ãã/ãã¿ãããåŠçããããã®åºæ¬çãªããžãã¯ãå¿ èŠã§ãïŒãã®ãã¹ãŠã®äŸã¯gitãªããžããªã«ãããŸãïŒã
ã¡ãã»ãŒãžã®ãªã¹ããããŠã³ããŒãã®é²è¡ç¶æ³ãããã³ç©ºã®ãã¥ãŒãå«ãç»é¢ã®äŸãèŠããšã責任åæ ã¯æ¬¡ã®ããã«ãªããŸãã
- ãªã¹ããã¬ãŒã³ã¿ãŒã¯ãã¡ãã»ãŒãžã®ããŒããšãªã¹ã/é²è¡ç¶æ³/空ã®ã¹ã¿ããã¥ãŒã®è¡šç€ºããžãã¯ãæ åœããŸãã
- ãã©ã°ã¡ã³ãã¯ããªã¹ã/é²è¡ç¶æ³/ã¹ã¿ããã¥ãŒã®è¡šç€ºããžãã¯ã®å®è£ ãšãä»ã®ç»é¢ãžã®ç§»è¡ãæ åœããŸãã
- ã¢ããã¿ãŒã¯ããã¬ãŒã³ã¿ãŒãViewHoldersã«ãããããŸãã
- ã¡ãã»ãŒãžãã¬ãŒã³ã¿ãŒã¯ãåã ã®ã¡ãã»ãŒãžã®ããžãã¹ããžãã¯ãæ åœããŸãã
- ViewHolderã¯ãã¡ãã»ãŒãžã®è¡šç€ºãæ åœããŸãã
ãããã®ã³ã³ããŒãã³ãã¯ãã¹ãŠççµåã§ãããäºãã«å¥ã ã«ãã¹ãã§ããŸãã
ããã«ãã¡ãã»ãŒãžãªã¹ãç»é¢ãšè©³çŽ°ç»é¢ãããå Žåãåãã¡ãã»ãŒãžãã¬ãŒã³ã¿ãŒãåå©çšãããã¬ãŒã³ããŒã·ã§ã³ã€ã³ã¿ãŒãã§ã€ã¹ã®2ã€ã®ç°ãªãå®è£ ïŒViewHolderãšãã©ã°ã¡ã³ãå ïŒã®ã¿ã䜿çšã§ããŸãã ããã«ãããDRYã³ãŒããä¿åãããŸãïŒ çŽ -ãç¥ããªã人ããŸãã¯ãç¹°ãè¿ããªããïŒã
åæ§ã«ãã«ã¹ã¿ã ãã¥ãŒã¯ãã¥ãŒã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ã§ããŸãã ããã«ãããã«ã¹ã¿ã ãŠã£ãžã§ããã§MVPã䜿çšããŠãã¢ããªã±ãŒã·ã§ã³ã®ããŸããŸãªéšåã§MVPãåå©çšããããè€éãªã€ã³ã¿ãŒãã§ã€ã¹ãåçŽãªãããã¯ã«åçŽã«åå²ãããã§ããŸãã
MVPãšæ§æã®å€æŽ
ãã°ããAndroidã§æžããŠããå Žåãæ¹åãšæ§æãå€æŽããããã®ãµããŒããããªãã«ã©ãã»ã©èŠçãäžãããç¥ã£ãŠããŸãã
- ãã©ã°ã¡ã³ã/ã¢ã¯ãã£ããã£ã¯ããã®ç¶æ ã埩å ã§ããã¯ãã§ãã ãã©ã°ã¡ã³ããæäœãããã³ã«ãåããå€æŽãããšãã«ãã®ããšãã©ã®ããã«å®è¡ããããsaveInstanceStateãã³ãã«ã«äœãé 眮ããããªã©ãèªåããå¿ èŠããããŸãã
- ããã¯ã°ã©ãŠã³ãã¹ã¬ããã§ã®é·æéã®æäœãæ£ããè¡ãã®ã¯éåžžã«å°é£ã§ãã æãããããééãã®1ã€ã¯ãå®äºåŸã«UIãæŽæ°ããå¿ èŠãããããããã©ã°ã¡ã³ã/ã¢ã¯ãã£ããã£ãžã®ãªã³ã¯ãããã¯ã°ã©ãŠã³ãã¹ã¬ããã«ä¿åããããšã§ãã ããã«ãããã¡ã¢ãªãªãŒã¯ãçºçãïŒãããããã¡ã¢ãªæ¶è²»ã®å¢å ã«ããã¢ããªã±ãŒã·ã§ã³ãã¯ã©ãã·ã¥ããŸãïŒãæ°ããã¢ã¯ãã£ããã£ãã³ãŒã«ããã¯ãåä¿¡ããªããããUIãæŽæ°ãããŸããã
MVPãé©åã«äœ¿çšããããšã§ããã®åé¡ããŸã£ããèæ ®ããããšãªã解決ã§ããŸãã ãã¬ãŒã³ã¿ãŒã¯çŸåšã®UIãžã®åŒ·åãªãªã³ã¯ãæããªããããéåžžã«è»œéã§ãããåããå€æŽããããšã§åŸ©å ã§ããŸãã ãã¬ãŒã³ã¿ãŒã¯ã¢ãã«ãšãã¥ãŒã®ç¶æ ãžã®ãªã³ã¯ãä¿åãããããåããå€æŽããåŸããã¥ãŒã®ç®çã®ç¶æ ã埩å ã§ããŸãã ãã®ãã¿ãŒã³ã䜿çšãããŠããå Žåãç»é¢ãå転ãããšãã«äœãèµ·ãããã«ã€ããŠã®äŸã®èª¬æããããŸãïŒ
- æåã«ãã¢ã¯ãã£ããã£ãäœæãããŸããïŒãæåã®ã€ã³ã¹ã¿ã³ã¹ããšåŒã³ãŸãããïŒã
- æ°ãããã¬ãŒã³ã¿ãŒãäœæãããŠããŸãã
- ãã¬ãŒã³ã¿ãŒã¯ã¢ã¯ãã£ããã£ã«ã¢ã¿ãããããŸãã
- ãŠãŒã¶ãŒãããŠã³ããŒããã¿ã³ãã¯ãªãã¯ããŸãã
- ãã¬ãŒã³ã¿ãŒã¯é·æéã®æäœãéå§ããŸãã
- æ¹åã¯å€åããŠããŸãã
- ãã¬ãŒã³ã¿ãŒã¯ãã¢ã¯ãã£ããã£ã®æåã®ã³ããŒãã解æŸãããŸã
- ã¢ã¯ãã£ããã£ã®æåã®ã€ã³ã¹ã¿ã³ã¹ã¯åç §ãããªããªããã¬ããŒãžã³ã¬ã¯ã¿ãŒã§äœ¿çšã§ããããã«ãªããŸããã
- ãã¬ãŒã³ã¿ãŒãä¿åãããããã¯ã°ã©ãŠã³ãæäœãç¶è¡ãããŸãã
- ã¢ã¯ãã£ããã£ã®2çªç®ã®ã€ã³ã¹ã¿ã³ã¹ãäœæãããŸãã
- ã¢ã¯ãã£ããã£ã®2çªç®ã®ã³ããŒããã¬ãŒã³ã¿ãŒã«æ·»ä»ãããŸãã
- ããŠã³ããŒããå®äºããŠããŸãã
- ãã¬ãŒã³ã¿ãŒã¯ãã¬ãŒã³ããŒã·ã§ã³ãæŽæ°ããŸãïŒã¢ã¯ãã£ããã£ã®2çªç®ã®ã€ã³ã¹ã¿ã³ã¹ïŒã
æ¹åã®å€æŽã®éã«ãã©ã°ã¡ã³ããä¿åããæ¹æ³ã¯ã
PresenterManager
ã¯ã©ã¹ã®ãªããžããªã§ç¢ºèªã§ããŸãã
ãŸãšã
ã¯ããããã§çµããã§ãã MVPã®ãããªå ±æã®è²¬ä»»ãããµããŒãããããã¹ãå¯èœãªã³ãŒããäœæããã®ã«ã©ã®ããã«åœ¹ç«ã€ããå®èšŒã§ãããšæããŸãã
èŠçŽãããšïŒ
- ãã¬ãŒã³ã¿ãŒã®ãã¢Javaãªããžã§ã¯ãã«ããžãã¹ããžãã¯ãé 眮ããŠãããžãã¹ããžãã¯ãåé¢ããŸãã
- ãã¬ãŒã³ã¿ãŒãšãã¬ãŒã³ããŒã·ã§ã³ã®éã®ããããªã€ã³ã¿ãŒãã§ã€ã¹ã«æéããããŸãã
- ã¢ã¯ãã£ããã£ããã©ã°ã¡ã³ããã«ã¹ã¿ã ãã¥ãŒã«ãã¬ãŒã³ããŒã·ã§ã³ã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ãããŸãã
- ãªã¹ãã®å ŽåãViewHolderã¯ãã¥ãŒã€ã³ã¿ãŒãã§ã€ã¹ãå®è£ ããå¿ èŠããããŸãã
- ãã¬ãŒã³ã¿ãŒã培åºçã«ãã¹ãããŸãã
- åããå€ãããšãã¯çºè¡šè ãç¶æããŸãã
äžèšã®å®è£ ã¯ArchExampleãªããžããªã§èŠã€ããããšãã§ããŸã ã
Mosby ã Flow and Mortar ããŸãã¯Nucleusãªã©ããã®ã¢ãããŒããæ¯æŽããå€ãã®ã©ã€ãã©ãªããããŸãã ããããæ€èšããããšããå§ãããŸãã