简体   繁体   English

Android MVP 依赖规则

[英]Android MVP Dependency Rules

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.我已经阅读并测试了很多关于 Android 中 MVP 模式的内容,但我在这里询问您的意见,如果我想同时尊重“依赖规则”和 MVP 模式,什么是最佳实践。

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.很多文章里是怎么解释的(见这个链接: http : //ferndocejas.com/2014/09/03/architecting-android-the-clean-way/ ),我们的应用必须分层,只有外层可以实例化和使用内层。 The inner layer receive all parameters (Variables, contexts, Views, etc.) and return response or use callbacks etc.内层接收所有参数(变量、上下文、视图等)并返回响应或使用回调等。

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/ ). (见链接: 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).在上面的链接中,作者指定他不使用回调方法,而是使用接口从内层移动到外层(不遵守依赖规则)。

My questions about this are two:我对此的问题有两个:

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 ? 1)调用类(外层是指被调用类的接口)和被调用类(内层是指被调用的类的接口)都使用接口好,还是只需要一个方向,例如只有外部类指的是内部类的接口

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? 2)因为外部层(UI)创建了所有内部层,并且通过简单的屏幕旋转被破坏和重新创建,保存状态(在需要时),破坏内部类和进程,是否更好(对于内存泄漏),并重新创建它们,或者以静态方式(通过 Singleton 或作为扩展 Application 类的类的实例)所有需要“生存”到旋转屏幕的类? Thanks to everyone!谢谢大家!

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.我相信 MVP 在某种程度上是一种 Tuxedo 开发,这意味着您必须在实现它们之前将所有内容分开并使用接口来声明不同的方法。 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.通常,在这种情况下,我认为我是定义层的人,另一个初级开发人员即将使用我开发的核心,因此,最好限制他的行为,减少他的失误和错误。

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!关于重新创建,我建议以正确的方式使用 Dagger,这确实可以阻止您重新创建之前仅因为屏幕旋转而生成的对象! You can keep your lower layers declarations in memory and attach them to your new view after rotation.您可以将较低层的声明保留在内存中,并在旋转后将它们附加到您的新视图中。

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:我建议查看我使用 MVP、Dagger、RxJava 和 Retrofit 开发的这个示例存储库,以更熟悉最佳实践和新的 Android 开发方法:

http://github.com/mmirhoseini/fyber_mobile_offers http://github.com/mmirhoseini/fyber_mobile_offers

I hope it helps :)我希望它有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM