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

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

hisaichi5518
November 29, 2015

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

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

hisaichi5518

November 29, 2015
Tweet

More Decks by hisaichi5518

Other Decks in Programming

Transcript

  1. ! ?

  2. MVC

  3. MVP

  4. 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(...) { ... } }
  5. 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); } }); } }
  6. 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); } }