简体   繁体   English

定义Android应用程序的MVP模式的问题

[英]Problems defining the MVP pattern for Android Apps

I've been developing on Android during the last couple of months, I've learned a few things but I think it's time to start doing it right, so I'm trying to implement the MVP/MVC pattern, which are in this case the most suitable patterns to handle UI. 我在过去的几个月里一直在开发Android,我已经学到了一些东西,但我认为是时候开始做正确的了,所以我试图实现MVP / MVC模式,在这种情况下最适合处理UI的模式。

I've read many threads and examples but I still have some doubts, very probably for my lack of experience with android. 我已经阅读了很多线程和示例,但我仍有一些疑问,很可能是因为我缺乏android的经验。

Some authors, defines Activities as presenters/controllers and some others as views, what I think is that activities should work as presenters instead of views, for its natural capabilities of keeping state and present a layout to the user, maybe I'm wrong and I hope someone can clarify this. 一些作者将活动定义为演示者/控制者,将其他一些定义为视图,我认为活动应该作为演示者而不是视图,因为它保持状态并向用户呈现布局的自然能力,也许我错了,我希望有人能澄清一下。

If I am correct, then Views should be different classes that uses the layout and bind events to communicate with the presenter (activity), this is where I get lost... 如果我是正确的,那么Views应该是使用布局和绑定事件与演示者(活动)通信的不同类,这是我迷路的地方......

I couldn't find a way to properly create a class that extends from view and uses a layout to bind the events and be able to communicate with the presenter. 我找不到一种方法来正确创建一个从视图扩展的类,并使用布局来绑定事件并能够与演示者进行通信。 The only way I did this was by using a Builder object, the builder creates the view through an inflater, and bind the events. 我这样做的唯一方法是使用Builder对象,构建器通过inflater创建视图,并绑定事件。 This works, but the view doesn't implement any Interface, which destroy my MVP pattern. 这可行,但视图没有实现任何破坏我的MVP模式的接口。

The other way I think of, is by using View as Proxies to the android view object, but I'm not sure if this is the best way to handle it... 我想到的另一种方式是使用View作为Proxies到android视图对象,但我不确定这是否是处理它的最佳方法...

I would really appreciate if someone can point me to the right direction. 如果有人能指出我正确的方向,我真的很感激。

Thanks and sorry for the long post! 谢谢,抱歉长篇大论!

I have some experience with MVC in other contexts, and after having developed quite a lot on Android, I'd say it's not straightforward at all. 我在其他环境中有一些MVC的经验,并且在Android上开发了很多之后,我会说它根本不是直截了当的。

You are likely to end up mixing Controller and View code in the Activity. 您最终可能会在Activity中混合使用Controller和View代码。 It should be a Controller yes, but it does deal with user interaction a lot, for menus, dialogs, etc... And it's not easy to get out of that. 它应该是一个控制器是的,但它确实处理用户交互很多,菜单,对话框等......并且它不容易摆脱它。

I think that Android development follows a different paradigm, which reminds me of Django and what they call MVT, Model-View-Template. 我认为Android开发遵循不同的范例,这让我想起了Django以及他们所谓的MVT,Model-View-Template。

So I'd advise not to try and adhere to strictly to MVC on Android. 所以我建议不要在Android上严格遵守MVC。 View and Controller code may mix, but your code can remain maintainable with a modular approach. 视图和控制器代码可能会混合,但您的代码可以通过模块化方法保持可维护性。 Custom views or other dedicated classes can help a lot in this regard. 在这方面,自定义视图或其他专用类可以提供很多帮助。

And you'll save a lot of time by following the natural logic of the framework instead of looking for academic MVC, in my opinion. 在我看来,你可以通过遵循框架的自然逻辑而不是寻找学术MVC来节省大量时间。

If you don't mind spaghetti code and perhaps potential memory leak. 如果你不介意意大利面条代码和潜在的内存泄漏。 You can do something like having one base layout/view tied to the activity. 您可以执行类似于将一个基本布局/视图绑定到活动的操作。 Then load your preferred view into a Layout. 然后将首选视图加载到布局中。 This custom class of the View will then take Activity into itself so it can refer back to the activity calling it and communicate with it. 然后,View的这个自定义类将把Activity带入自身,这样它就可以返回调用它并与之通信的活动。 When you want to switch view, you will then just replace the current view with another one. 如果要切换视图,则只需将当前视图替换为另一个视图。

The principle is the same with Fragment, but my way is just heap messier. Fragment的原理是一样的,但我的方式只是堆乱。

So after that one, I stick with the API & Fragment. 所以在那之后,我坚持使用API​​和片段。 It is close enough for MVC kinda structure while keeping things neat. 它足够接近MVC有点结构,同时保持整洁。

You can try my way, it's a good experience still as it may teach you a lot of what not to do. 你可以尝试我的方式,这仍然是一个很好的体验,因为它可以教你很多不该做的事情。 Also stuff like Map won't work on the view as well, just in case you haven't figured out yet :) 像Map这样的东西也不适用于视图,以防万一你还没弄明白:)

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

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