Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MVPアーキテクチャ使いたい

4b7536b1ad854fa552a2cea0248bb5cc?s=47 hisaichi5518
November 29, 2015

 MVPアーキテクチャ使いたい

AndroidのMVPアーキテクチャについて

4b7536b1ad854fa552a2cea0248bb5cc?s=128

hisaichi5518

November 29, 2015
Tweet

Transcript

  1. UIςετݮΒ͍ͨ͠ =MVPΞʔΩςΫνϟ࢖͍͍ͨ hisaichi5518

  2. ஫ҙ

  3. ϩδοΫͱ͸ • ϏδωεϩδοΫ • ϓϨθϯςʔγϣϯϩδοΫ

  4. ϓϨθϯςʔγϣϯϩδοΫ Ϣʔβʔ͔ΒೖྗΛड͚ औΓɺϏδωεϩδοΫ ʹ౉͢

  5. ϏδωεϩδοΫ APIΛୟ͍ͯɺ஋Λฦ͢

  6. ϏδωεϩδοΫ σʔλϕʔεΛୟ͍ͯɺ ஋Λฦ͢

  7. ϓϨθϯςʔγϣϯϩδοΫ ͦΕ͔Βฦ͖ͬͯͨ஋Λ ੔ܗͯ͠දࣔ͢Δ

  8. ๻͕ʮϩδοΫʯͱݴ͏࣌͸ɺ ͜ͷ2छྨΛࢦ͠·͢ɻ

  9. ! ?

  10. ͳͥΞʔΩςΫνϟΛ࠾༻͢Δͷ͔?

  11. ݱঢ়ͱཧ૝ͷΪϟοϓΛ ຒΊΔͨΊͷํ๏ͱ͠ ͯɺΞʔΩςΫνϟΛ࠾ ༻͍ͨ͠

  12. ݱঢ়

  13. UIςετ͹͔Γॻ͍͍ͯ ͯർฐ͕ͪ͠

  14. ཧ૝

  15. ୯ମςετΛॻ͘ UIςετগͳΊ

  16. ͳͥUIςετ͹͔Γॻ͔͟ΔΛ͑ͳ͍͔?

  17. androidTestͰͳ͍ͱಈ ͔ͳ͍Ϋϥεͷதʹςε τ͍ͨ͠΋ͷ͕͋Δ

  18. ͳͥ୯ମςετͰಈ͔ͳ͍Ϋϥεͷதʹςετ͍ͨ͠΋ͷ͕͋Δ?

  19. ͻͱͭͷΫϥεͷ੹຿͕ େ͖͗͢ɻΫϥεͷ੹຿ Λখ͘͞ग़དྷ͍ͯͳ͍ɻ

  20. ͳͥ੹຿Λখ͘͞ग़དྷ͍ͯͳ͍ͷ͔?

  21. Ϋϥε͕ԿΛ୲౰͢΂͖ ͔ɺܾ·͍ͬͯͳ͍͔Β

  22. ͭ·Γ ཧ૝ͱݱ࣮ͷΪϟοϓΛ ຒΊΔʹ͸

  23. ֤Ϋϥεͷ੹຿Λখ͘͞ ͯ͋͛͠Δඞཁ͕͋Γ

  24. ͦͷͨΊʹ ܾ·ΓࣄΛ࡞Δࣄ͕ඞཁ

  25. ͦͷܾ·Γࣄ͕ ΞʔΩςΫνϟ

  26. ʮMVPΞʔΩςΫνϟʯͱ͸?

  27. MVCͷ೿ੜܥ

  28. MVC

  29. Model-View-Controller

  30. ݁࿦: Androidʹ޲͍ͯ ͳ͍

  31. Model = ;ͭ͏ͷΫϥε

  32. View = Activity

  33. Controller = Activity

  34. View + Controller = Activity

  35. ActivityʹϓϨθϯςʔ γϣϯϩδοΫ͕ೖͬͯ ͠·͏

  36. ݁࿦: Androidʹ޲͍ͯ ͳ͍

  37. MVP

  38. Model-View-Presenter

  39. Model

  40. Model • ϏδωεϩδοΫΛॻ͘ • Presenterʹґଘ͠·ͤΜ • Viewʹґଘ͠·ͤΜ

  41. View

  42. View • ը໘ͷදࣔ/ߋ৽ɾϢʔβʔͷೖྗͷड͚ޱ • Ϣʔβʔೖྗ͸ɺPresenterʹ౉͢ • ෳ਺ͷViewΛ·ͱΊΔ܅Ͱ͋Δ • TextView, Toolbar

    ...
  43. Presenter

  44. Presenter • ϓϨθϯςʔγϣϯϩδοΫΛॻ͘ • View͔Β౉͞ΕͨϢʔβʔ͔ΒͷೖྗΛModelʹ౉͢ • Model͔Βฦ͖ͬͯͨ஋Λ΋ͱʹViewͷߋ৽Λߦ͏ • Viewͷߋ৽͸ɺInterfaceΛܦ༝ͯ͠ߦ͏

  45. MVPΞʔΩςΫνϟ͸ɺ Activity͔ΒϓϨθʔς ʔγϣϯϩδοΫΛ Presenterʹൈ͖ग़ͤΔ

  46. จࣈ͡ΌΘ͔ΒΜ!ਤͰ!

  47. Ҿ༻: https://github.com/rallat/EffectiveAndroid/tree/mvp

  48. ਤ͡ΌΘ͔ΒΜ!ίʔυͰ!

  49. (ྫ)ϢʔβʔΛऔಘ໊ͯ͠લΛදࣔ͢Δ

  50. View interfaceΛఆٛ͢Δ public interface UserView { void showName(String name); void

    showError(Exception exception); }
  51. View PresenterΛݺͼग़͢ public class UserActivity extends AppCompatActivity implements UserView {

    @Override protected void onCreate(Bundle savedInstanceState) { int userId = ...; (new UserPresenter(this)).fetchUser(userId); } @Override public void showName(String name) { mTextView.setText(name); } @Override public void showError(...) { ... } }
  52. Presenter public class UserPresenter { UserView mView; public UserPresenter(UserView view)

    { mView = view; } public void fetchUser(int userId) { ApiClient.getInstance().create(UsersService.class).fetch(userId, new Callback<User>() { @Override public void success(User user, Response response) { mView.showName(user.getName()); } @Override public void failure(RetrofitError error) { mView.showError(error); } }); } }
  53. Model ࠓͷApiClient.javaͱ࣮૷͕ಉ͡ͳͷͰུ

  54. ͳͥɺMVPΞʔΩςΫνϟ͸ςετ͕͠΍͍͢ͷ͔?

  55. Viewͷߋ৽͸Interface Λ௨ͯ͡ߦΘΕΔ

  56. ͭ·ΓɺInterfaceΛܧ ঝ͍ͯ͠ΔView΋Ͳ͖ ͑͋͞Ε͹ɺPresenter ͷςετ͸Ͱ͖Δɻ

  57. Test public class UserPresenterTest { @Test public void testFetchUser() throws

    Exception { final AtomicBoolean called = new AtomicBoolean(); mMockClient.mock("/v4/users/\\d.json").to(200, "user.json"); class UserViewTest implements UserView { @Override public void showName(String name) { called.set(true); assertThat(name).isEqualTo("hisaichi5518"); } } UserPresenter presenter = new UserPresenter(new UserViewTest()); presenter.fetchUser(1); assertThat(called.get()).isEqualTo(true); } }
  58. Activityʹґଘ͠ͳ͘ͳ ͬͨͷͰɺϓϨθϯςʔ γϣϯϩδοΫΛ୯ମς ετͰॻ͚Δʂʂ̍

  59. UIςετͰർฐ͠ͳ͍!

  60. ͳͷͰMVPΞʔΩςΫν ϟΛ࢖͍͍ͨ

  61. ·ͱΊ

  62. ·ͱΊ • ݱঢ়ɺΫϥεͷ੹຿͕େ͖͗ͯ͢ɺϓϨθϯςʔγϣϯϩδ οΫΛUIςετͰςετͤ͟ΔΛ͑ͳ͍ • ཧ૝͸ɺΫϥεͷ੹຿Λখ͘͞͠ɺϩδοΫ͸୯ମςετͰ ςετ͍ͨ͠ • MVPΞʔΩςΫνϟΛ࠾༻͠ɺΫϥεͷ੹຿Λ໌֬ʹܾΊɺ ୯ମςετʹد͍ͤͯ͘

  63. ·ͱΊ • MVPΞʔΩςΫνϟ͸ɺActivity͔ΒϓϨθϯςʔγϣϯϩδ οΫΛ෼཭͢Δ • ϓϨθϯςʔγϣϯϩδοΫΛ୯ମςετͰॻ͚ΔΑ͏ʹͳ ΔͷͰɺUIςετΛॻ͘ྔ͸ݮΔ

  64. !!!!!!!!!!! ർฐͤͣʹྑ͍΋ͷ࡞Δκʂ !!!!!!!!!!!

  65. ࢀߟ • hisaichi5518/minne-android • rallat/EffectiveAndroid • AndroidΦʔϧελʔζͰClean Architectureʹ͍ͭͯൃද͠ ͖ͯͨ&ࢀߟϦϯΫू •

    AndroidͰ͸MVCΑΓMVPͷํ͕͍͍͔΋͠Εͳ͍
  66. ࣭ٙԠ౴

  67. ͲͷΑ͏ʹMVPΞʔΩςΫνϟΛར༻ ͍͔ͯ͘͠? • ཧ૝: ςετΛॻ͍͔ͯΒˠϦϑΝΫλϦϯά • ݱ࣮: PresenterΛར༻͢ΔΑ͏ʹϦϑΝΫλϦϯάˠςετ Λॻ͘ •

    ·ͣ͸ɺݴ͍ग़ͬ͠΃ͷ๻͕΍Δͭ΋Γ
  68. ΞʔΩςΫνϟΛ࠾༻͢Δ্Ͱͷํ਑ • ཧղ͍ͯ͠ͳ͍΋ͷ͸࢖Θͳ͍ • ཧղ͠ʹ͍͘΋ͷ͸࢖Θͳ͍ • ่յͦ͠͏ͳͷ͸࢖Θͳ͍ • ࣮ࡍʹίʔυΛॻ͍ͯ൑அ͢Δ •

    ݱ࣮త͔ɺ͕ॏཁ • ૣ͗͢Δ࠷దԽ͸͠ͳ͍
  69. Viewʹ͋ͨΔΫϥε͸? • Activity • Fragment • CustomView ͳͲɻద࣌൑அ͍ͯ͘͠ɻ

  70. UIςετ͸׬શʹͳ͘ͳΔʁ Μͳͨ͊͜ͳ͍

  71. ࣮ࡍͷίʔυͰઆ໌ͯ͠΄͍͠ (ུ)