Android MVP模式包结构 [英] Android MVP pattern package structure

查看:159
本文介绍了Android MVP模式包结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android上看过各种关于MVP模式的精彩教程,但是作者似乎在包装方面都有不同的实践.

I saw various great tutorials on MVP pattern in android, but the authors all seem to have different practice on packaging.

我看到的第一个教程按功能进行了打包.如登录",加入","UI"包.

The first tutorial I saw did the packaging by functionalities. Such as, "Login", "Join", "UI" package.

UI程序包仅具有活动,登录"程序包具有演示者和具体演示者的接口,并且此程序包包含子程序包模型",该子程序包包含有关登录模型的所有内容(与服务器通信). "Join"程序包与"Login"程序包具有相同的组成.

The UI package has only activities, the "Login" package has the interfaces for the presenter and the concrete presenter, and this package contains a sub package "Model" that contains everything about the login model(communications with the server). The "Join" package has the same composition as the "Login" package.

但是我看到的另一个人是按场景打包的,例如"Join","Login".

But the other one I saw did the packaging by scene, such as "Join", "Login".

"Join"包包含一个活动,以及三个名为"Model","View"和"Presenter"的子包.

"Join" package contains an activity, and three sub packages named "Model", "View", "Presenter".

最佳做法是什么?是否有任何文章可以解决此问题?

What is the best practice? Are there any articles that handles this issue?

推荐答案

我只是重新发布我的答案

I just repost my answer here

我经常将业务逻辑代码放在模型层中(不要与数据库中的模型混淆).我经常将其重命名为XManager以避免混淆(例如ProductManagerMediaManager ...),因此Presenter类仅用于保持工作流.

I often put business logic code in Model Layer (don't make confusion with model in database). I often rename as XManager for avoiding confusion (such as ProductManager, MediaManager ...) so presenter class just uses for keeping workflow.

经验法则是没有,或至少没有限制Presenter类中的 import android package .最佳实践可以帮助您更轻松地测试Presenter类,因为Presenter现在只是一个普通的Java类,因此我们不需要android框架来测试这些内容.

The rule of thumb is no or at least limit import android package in presenter class. This best practice supports you easier in testing presenter class because presenter now is just a plain java class, so we don't need android framework for testing those things.

例如,这是我的mvp工作流程.

For example here is my mvp workflow.

视图类:这是一个用于存储所有视图(如按钮,文本视图...)的位置,并在此层上为这些视图组件设置所有侦听器.同样在此View上,您可以为稍后的演示者实现定义Listener类.您的视图组件将在此侦听器类上调用方法.

View class: This is a place you store all your view such as button, textview ... and you set all listeners for those view components on this layer. Also on this View, you define a Listener class for presenter implements later. Your view components will call methods on this listener class.

class ViewImpl implements View {
   Button playButton;
   ViewListener listener;

   public ViewImpl(ViewListener listener) {
     // find all view

     this.listener = listener;

     playButton.setOnClickListener(new View.OnClickListener() {
       listener.playSong();
     });
   }

   public interface ViewListener {
     playSong();
   }
}

演示者类:这是您在其中存储视图和模型以供以后调用的地方.演示者类还将实现上面定义的ViewListener接口.演示者的要点是控制逻辑工作流.

Presenter class: This is where you store view and model inside for calling later. Also presenter class will implement ViewListener interface has defined above. Main point of presenter is control logic workflow.

class PresenterImpl extends Presenter implements ViewListener {
    private View view;
    private MediaManager mediaManager;

    public PresenterImpl(View, MediaManager manager) {
       this.view = view;
       this.manager = manager;
    }

    @Override
    public void playSong() {
       mediaManager.playMedia();
    }
}

经理类:这是核心业务逻辑代码.也许一个演示者将有很多经理(取决于视图的复杂程度).通常,我们是通过某些注入框架(例如Dagger)获得Context类的.

Manager class: Here is the core business logic code. Maybe one presenter will have many managers (depend on how complicate the view is). Often we get Context class through some injection framework such as Dagger.

Class MediaManagerImpl extends MediaManager {
   // using Dagger for injection context if you want
   @Inject
   private Context context;
   private MediaPlayer mediaPlayer;

   // dagger solution
   public MediaPlayerManagerImpl() {
     this.mediaPlayer = new MediaPlayer(context);
   }

   // no dagger solution
   public MediaPlayerManagerImpl(Context context) {
     this.context = context;
     this.mediaPlayer = new MediaPlayer(context);
   }

   public void playMedia() {
     mediaPlayer.play();
   }

   public void stopMedia() {
      mediaPlayer.stop();
   }
}

最后::将这些内容放到活动,片段..."中,这是您初始化视图,管理器并将其分配给演示者的地方.

Finally: Put those thing together in Activities, Fragments ... Here is the place you initialize view, manager and assign all to presenter.

public class MyActivity extends Activity {

   Presenter presenter;

   @Override
   public void onCreate() {
      super.onCreate();

      IView view = new ViewImpl();
      MediaManager manager = new   MediaManagerImpl(this.getApplicationContext());
      // or this. if you use Dagger
      MediaManager manager = new   MediaManagerImpl();
      presenter = new PresenterImpl(view, manager);
   }   

   @Override
   public void onStop() {
     super.onStop();
     presenter.onStop();
   }
}

您看到每个演示者,模型,视图都被一个界面包裹.这些组件将通过接口调用.这种设计将使您的代码更健壮,以后更容易修改.

You see that each presenter, model, view is wrapped by one interface. Those components will called through interface. This design will make your code more robust and easier for modifying later.

这篇关于Android MVP模式包结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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