【问题标题】:android MVP - Presenter with multiple model'sandroid MVP - 具有多个模型的演示者
【发布时间】:2017-03-20 23:16:07
【问题描述】:

计划为 MVC 类型的 Android 应用实施 MVP 架构。我担心如何制作一个拥有多个 模型。

通常,演示者的构造函数如下所示:

MyPresenter(IView 视图, IInteractor 模型);

这样我可以在测试和模拟视图和模型时轻松交换依赖项。但是想象一下,我的演示者与必须是多个网络调用的活动相关联。因此,例如,我有一项活动为登录执行 API 调用,然后为安全问题执行另一项,然后为GetFriendsList 执行第三项。所有这些电话都在同一个活动主题中。如何使用我上面展示的构造函数来做到这一点?或者做这种事情的最好方法是什么?还是我仅限于只有一个模型并在该模型中调用服务?

【问题讨论】:

  • 如果你看谷歌自己的一些开源项目,他们只使用 1-1-1 的方法(这意味着 1 个视图,1 个演示者 1 个模型)。每个事务都有一个特定的(枚举和捆绑参数)。 Enum 决定你要使用什么 api,而 Bundle 代表输入。我并不是说这是正确的,但来源足够可靠。
  • 有道理

标签: android android-mvp


【解决方案1】:

Presenter 构造函数只需要视图。你不需要依赖模型。定义您的演示者和类似的视图。

 public interface Presenter{
  void getFriendList(Model1 model);
  void getFeature(Model2 model2);

    public interface View{
      void showFriendList(Model1 model);
      void showFeature(Model2 model2)
    }
  }

现在您的实现类仅依赖于视图部分。

让你的方法处理你的模型

class PresenterImpl implements Presenter{
    View view;  
    PresenterImpl(View view){
     this.view = view;
    }
  void getFriendList(Model1 model){
   //Do your model work here
   //update View
   view.showFriendList(model);
  }
  void getFeature(Model2 model2) {
   //Do your model work here
   //updateView
   view.showFeature(model2)

  } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2018-12-03
    • 2019-09-27
    • 1970-01-01
    • 2021-12-31
    相关资源
    最近更新 更多