Android MVP依赖规则 [英] Android MVP Dependency Rules

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

问题描述

我已经阅读并测试了很多有关Android中MVP模式的信息,但是我在这里想请教您有关我是否要同时遵守依赖关系规则"和MVP模式的最佳做法的看法.

I've read and tested much about MVP patterns in Android, but I'm here to ask your opinion about what could be the best practice if I want to respect both the "Dependency Rule" and the MVP pattern.

如何在许多文章中对其进行解释(请参阅此链接: http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/),我们的应用程序必须分为多个层,并且是一个外层可以实例化并使用一个内层.内层接收所有参数(变量,上下文,视图等)并返回响应或使用回调等.

How it is explained into many articles (see this link: http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/), our application must be divided into layers and only an outer layer can instantiate and use an inner layer. The inner layer receive all parameters (Variables, contexts, Views, etc.) and return response or use callbacks etc.

通过使用接口并直接调用接口(而不是直接使用类)来尽可能地隔离各层也是一种很好的做法. (请参阅链接: http://www .tinmegali.com/en/model-view-presenter-mvp-in-android-part-2/). 在上面的链接中,作者指定他不使用回调方法,而是使用接口从内层移动到外层(不遵守依赖关系规则).

It is also a good practice to isolate the layers the more you can, through using interfaces and calling the interfaces instead of classes directly. (see the link: http://www.tinmegali.com/en/model-view-presenter-mvp-in-android-part-2/). In the link above the author specifies he doesn't use callback methods but interfaces to move from an inner layer to an outer layer (not respecting the dependency rule).

我对此有两个疑问:

1)最好同时对调用类使用接口(外层是指被调用类的接口)和被调用类(内层是指还是仅在一个方向上是必需的,例如只有外部类是指内部一个的接口?

1) is it better to use interface for both the calling class (outer layer refers to the interface of the class called) and the called class (inner layer refers to the interface of the class calling), or is it only necessary in one direction, for instance only outer class refers to interface of inner one?

2)因为外部层(UI)创建了所有内部层,并且通过简单的屏幕旋转被破坏并重新创建,所以保存状态(在发生内存泄漏时)更好( (需要),销毁内部类和进程并重新创建它们,或以静态方式进行存储(通过Singleton或作为扩展Application类的类的实例),所有需要生存"的类转到旋转屏幕? 谢谢大家!

2) because the external layer (the UI) creates all inner layers and through a simple screen rotation is destroyed and recreated, is it better (for the memory leak) to save the state (when it's needed), destroy inner classes and processes, and recreate them, or to istantiate in a static way (through Singleton or as instances of a class which extends Application class) all the classes which need to "survive" to rotation screen? Thanks to everyone!

推荐答案

我认为MVP在某种程度上是Tuxedo开发的,这意味着您必须在实现它们之前分离所有内容并使用接口声明不同的方法.因此,我对您的拳头问题的回答是是"!

I believe that MVP is somehow a Tuxedo development which means you have to separate everything and use interfaces for declaring different methods before implementing them. So my answer to your fist question is YES!

通常,在这种情况下,我认为我是定义层的人,而另一位初级开发人员将使用我开发的核心,因此最好限制他的行为并减少他的错误,错误.

Usually, in this type of situation, I think that I am the one who define layers and another junior developer is about to use the core which I have developed, so, it is better to limit his action and decrease his faults and error.

关于重新创建,我建议以正确的方式使用Dagger,这实际上使您无法重新创建仅由于屏幕旋转而生成的对象!您可以将下层声明保留在内存中,并在旋转后将其附加到新视图中.

About recreating, I would suggest using Dagger in proper way, which really stops you from recreating objects that have been generated before only because of a screen rotation! You can keep your lower layers declarations in memory and attach them to your new view after rotation.

我建议您看一下我使用MVP,Dagger,RxJava和Retrofit开发的此示例存储库,以更熟悉最佳实践和新的Android开发方法:

I would suggest to take a look at this sample repo which I have developed using MVP, Dagger, RxJava and Retrofit to get more familiar with best practices and new Android development methods:

http://github.com/mmirhoseini/fyber_mobile_offers

我希望它会有所帮助:)

I hope it helps :)

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

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