在MVP中是View或Presenter的onClick责任吗? [英] In MVP is onClick responsibility of View or Presenter?

查看:179
本文介绍了在MVP中是View或Presenter的onClick责任吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在负责处理用户界面点击的MVP模式中?

例如非MVP方法类似于:

In the MVP pattern who is responsible to handle clicks on the UI?
E.g. the non-MVP approach would be something like:

counterButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
      totalClicks++;
      counterTextView.setText("Total clicks so far: "+totalClicks);
    }
  });

使用MVP是 onClick 的责任 Presenter ?或者查看可以处理吗?

有人可以澄清一下吗?

Using MVP is the onClick the responsibility of the Presenter? Or the View can handle that?
Can someone please clarify this?

推荐答案

OnClick应该调用 Presenter 方法。您应该在演示者中开展业务,如果您需要更新ui,您应该在查看中定义一个方法,并从演示者处调用它。

OnClick should call a Presenter method. You should do your business in presenter and if you need to update the ui you should define a method in your View and call it from presenter.

您需要一种方法查看例如:

public void showCounterCount(final int totalClicks){
     counterTextView.setText("Total clicks so far: "+totalClicks);
}

您还需要中的方法和变量演示者

int totalClicks = 0;

public void onCounterButtonClicked(){
    totalClicks++;
    mView.showCounterCount(totalClicks);
}

并像这样重构您的代码:

And refactor your code like this:

counterButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
      mPresenter.onCounterButtonClicked();
    }
  });

对于更复杂和更干净的架构,您可以在交互者中进行用例业务。 (在您的示例中,递增计数器值是您的应用程序的用例)

For more complex and clean architecture you can do your use case business in interactors. (In your example incrementing a counter value is a use-case for your application)

您可以定义一个交互器并在那里增加计数器值。

You can define an interactor and increment your counter value there.

CounterInteractor:

CounterInteractor:

public CounterInteractor{
   public int incrementCounter(int currentCounter){
       return currentCounter+1;
   }
}

并重构您的演示者,如下所示:

And refactor your presenter like below:

int totalClicks = 0;
CounterInteractor mCounterInteractor = new CounterInteractor();

public void onCounterButtonClicked(){
    totalClicks = mCounterInteractor.incrementCounter(totalClicks);
    mView.showCounterCount(totalClicks);
}

使用这种方法,您可以将业务逻辑完全与演示者分开并重新使用-case概念,不会在演示者中复制代码。这是更干净的方法。

With this approach you separate your business logic totally from presenters and re use your use-case concepts without duplicating code in presenters. This is more clean approach.

你也可以检查这个git repo以获得不同的MVP方法。
https://github.com/googlesamples/android-architecture/tree/todo -mvp-clean /

You can also check this git repo for different MVP Approaches. https://github.com/googlesamples/android-architecture/tree/todo-mvp-clean/

祝你好运。

编辑:

这是我的轻量级维基百科客户端项目源:
https://github.com/savepopulation/wikilight

Here's my lightweight wikipedia client project source: https://github.com/savepopulation/wikilight

我正在尝试实施 MVP 。 (MVP + Dagger2 + RxJava)

I'm trying to implement MVP. (MVP + Dagger2 + RxJava)

这篇关于在MVP中是View或Presenter的onClick责任吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆