Android应用程序架构 - MVVM或MVC? [英] Android application architecture - MVVM or MVC?

查看:159
本文介绍了Android应用程序架构 - MVVM或MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个Android项目,我开始工作,我想它的结构要尽可能强大的。

I got an android project I'm beginning to work on, and I want its structure to be as robust as possible.

我是从一个WPF MVVM背景快到了,我一直在读一些关于Android应用程序架构,但我就是无法找到我应该使用的建筑的直线明确的答案。

I'm coming from a WPF MVVM background and I've been reading a little about android applications architecture, but I just couldn't find a straight clear answer about which architecture I should use.

有些人使用MVVM建议 - <一个href="http://vladnevzorov.com/2011/04/30/android-application-architecture-part-ii-architectural-styles-and-patterns/">http://vladnevzorov.com/2011/04/30/android-application-architecture-part-ii-architectural-styles-and-patterns/

Some people suggested using MVVM - http://vladnevzorov.com/2011/04/30/android-application-architecture-part-ii-architectural-styles-and-patterns/

和其他人则建议使用MVC,但没有具体说明它究竟应该实施。

and others suggested using MVC, but didn't specify how exactly it should be implemented.

正如我所说的,我从一个WPF的MVVM背景的,所以我知道它在很大程度上依赖于它,据我了解,没有在Android的支持,默认绑定。

As I said I'm coming from a WPF-MVVM background, and therefore I know it heavily relies on bindings which as far as I understand, are not supported by default in Android.

好像有一个第三方的解决方案 - HTTP://$c$c.google。 COM / P / Android的结合/ 但我不知道我是否想依靠这一点。如果它的发展会停止,也不会被未来的API支持等。

It seems like there is a 3rd party solution - http://code.google.com/p/android-binding/ But I don't know if I'd like to rely on that. What if its development would stop and it will not be supported by future APIs and etc..

基本上我寻找的是一个全面的教程,教我的最佳实践构建应用程序的结构。文件夹和类别结构,等等。我只是找不到任何深入的教程,我本来预计,谷歌将提供这样一个教程的开发人员。我只是不认为这种文档处理技术方面不够好 - <一个href="http://developer.android.com/guide/topics/fundamentals.html">http://developer.android.com/guide/topics/fundamentals.html

Basically what I'm looking for is a thorough tutorial that will teach me the best practices for building the application's structure. Folders and classes structure and etc. I just couldn't find any thorough tutorial, and I would have expected that Google would supply such a tutorial for its developers. I just don't think that this kind of documentation handles the technical aspect good enough - http://developer.android.com/guide/topics/fundamentals.html

我希望我已经够清楚,我不要求太多,我只是想确保我的应用程序的结构,在我的code会变成面条怪物。

I hope I've been clear enough and that I'm not asking for too much, I just want to be sure about my application's structure, before my code will turn into a spaghetti monster.

谢谢!

推荐答案

首先,Android不强迫你使用任何架构。不仅如此,它也使得它有些难以尽量遵守任何。这需要你是一个聪明的开发商,以避免造成意大利面条codeBase的:)

First of all, Android doesn't force you to use any architecture. Not only that but it also makes it somewhat difficult to try to follow to any. This will require you to be a smart developer in order to avoid creating a spaghetti codebase :)

您可以尝试以适应您知道任何方式和你喜欢的。我觉得最好的办法会以某种方式进入你的勇气,你开发更多的应用程序(很抱歉,但一如既往,你必须做出很多错误,直到你开始做是正确的)。

You can try to fit in any pattern you know and you like. I find that the best approach will in some way get into your guts as you develop more and more applications (sorry about that but as always, you'll have to make lots of mistakes until you start doing it right).

关于你知道的方式,让我做错了什么:我会混合使用三种不同的模式,所以你得到什么做什么在机器人的感觉。我相信,presenter /模型视图应在片段或活动的地方。适配器有时会做这个工作,因为他们照顾投入名单。也许活动应该像控制器了。模型应该是普通的Java文件,而视图应该躺在布局资源和一些自定义组件,您可能必须实现。

About the patterns you know, let me do something wrong: I'll mix three different patterns so you get the feeling of what does what in android. I believe the Presenter/ModelView should be somewhere in the Fragment or Activity. Adapters might sometimes do this job as they take care of inputs in lists. Probably Activities should work like Controllers too. Models should be regular java files whereas the View should lay in layout resources and some custom components you might have to implement.

我可以给你一些提示。 这是一个社会维基答案,所以希望其他人可能包括其他建议。

I can give you some tips. This is a community wiki answer so hopefully other people might include other suggestions.

我认为主要有两种合理的可能性:

I think there are mainly two sensible possibilities:

  • 将组织一切的键入的 - 创建一个文件夹中的所有活动,另一个文件夹中所有适配器,另一个文件夹中的所有片段等
  • 的(也许不是最好的词)组织一切。这意味着一切都与ViewPost将是相同的文件夹中 - 在活动中,片段,适配器等一切与ViewPost将在另一个文件夹。同样为EditPost等我猜活动,将要求你创建的文件夹,然后会有一些更多的普通的人的基类的例子。
  • organize everything by type - create a folder for all activities, another folder for all adapters, another folder for all fragments, etc
  • organize everything by domain (maybe not the best word). This would mean everything related to "ViewPost" would be inside the same folder - the activity, the fragment, the adapters, etc. Everything related to "ViewPost" would be in another folder. Same for "EditPost", etc. I guess activities would mandate the folders you'd create and then there would be a few more generic ones for base classes for example.

就个人而言,我只参与了项目使用第一种方式,但我真的想尝试一下以后,我相信它可以使事情更有条理。我看不出有什么优势,有30个不相关的文件的文件夹,但是这就是我得到的第一种方法。

Personally, I have only been involved in projects using the first approach but I really would like to try the later as I believe it could make things more organized. I see no advantage in having a folder with 30 unrelated files but that's what I get with the first approach.

  • 在创建布局和风格,始终名称(或标识它们)使用preFIX为他们在那里用的活动(/片段)。

所以,在ViewPost的情况下使用应该开始的所有字符串,样式,IDS是@ ID / view_post_heading(对于一个TextView为例),@款式/ view_post_heading_style,@字符串/ view_post_greeting。

So, all strings, styles, ids used in the context of "ViewPost" should start be "@id/view_post_heading" (for a textview for example), "@style/view_post_heading_style", "@string/view_post_greeting".

这将优化自动完成,组织,避免名称colision等。

This will optimize autocomplete, organization, avoid name colision, etc.

我想你会想使用基类为您做pretty的每样东西:适配器,活动,碎片,服务等,这些可能是有用的,至少出于调试的目的,让你知道哪些事件发生在所有的活动。

I think you'll want to use base classes for pretty much everything you do: Adapters, Activities, Fragments, Services, etc. These might be useful at least for debugging purposes so you know which events are happening in all your activity.

  • 我从来没有使用匿名类 - 这些都是丑陋的,并会带动你的注意力,当你试图读取code
  • 有时候我preFER使用内部类(比创建一个专用的类) - 如果一个类是不会被其他任何地方使用(和它的小),我认为这是非常方便
  • 想想从一开始你的日志系统 - 您可以使用Android的日志记录系统,但做它的一个很好的使用

这篇关于Android应用程序架构 - MVVM或MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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