Android 应用程序架构 - 建议的模型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3320534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android application architecture - what is the suggested model?
提问by MalcomTucker
In the same way a web or desktop app might have three or n tiers - UI, Business, Data for example - what is the suggested structure for an Android application? How do you group classes together, what layers do you have etc?
以同样的方式,Web 或桌面应用程序可能具有三层或 n 层——例如 UI、业务、数据——Android 应用程序的建议结构是什么?你如何将班级分组,你有哪些层等?
I'm just starting Android dev (an internet-based app that must respond to incoming notifications) and have no real feel for the structure I'm aiming at. Suggestions appreciated.
我刚刚开始使用 Android 开发(一个必须响应传入通知的基于互联网的应用程序)并且对我所针对的结构没有真正的感觉。建议表示赞赏。
回答by louiscoquio
IMHO, Android "wants to" follow a MVC pattern, but view & controller are generally really coupled in activities.
恕我直言,Android“想要”遵循 MVC 模式,但视图和控制器通常在活动中真正耦合。
It makes unit test harder and it's hard to obey to the Single Responsibility Principle.
它使单元测试变得更加困难,并且难以遵守单一职责原则。
I found a really nice Android architecture presented here, there could be an idea. Everything is loosely coupled, so much easier to test and edit.
我发现这里展示了一个非常好的 Android 架构,可能会有一个想法。一切都是松散耦合的,因此更容易测试和编辑。
Obviously, I'm sure there are a lot of others possibilities (like the MVP pattern (Model View Presenter) - and here are answers talking about MVP in Android), but you should still take a look on it.
显然,我确信还有很多其他的可能性(比如 MVP 模式(模型视图演示者)——这里是关于 Android 中 MVP 的答案),但你仍然应该看看它。
回答by FinalFive
I've been working on Android for 9 months now from a server-side background where full unit testing and layered architectures are common and work well.
我已经在 Android 上工作了 9 个月,在服务器端背景下,完整的单元测试和分层架构很常见并且运行良好。
Through lots of trial and error and I would strongly suggest using the Model View Presenter
pattern, not Model View Controller.
通过大量的反复试验,我强烈建议使用该Model View Presenter
模式,而不是模型视图控制器。
A huge issue I've found is that Activities
/Fragments
have a lifecycle which is outside your control and can lead to unexpected issues.
我发现的一个大问题是Activities
/Fragments
的生命周期超出了您的控制范围,并可能导致意外问题。
For example, our main android app wants to be used in landscape mode on tablets. We do this in OnCreateView()
or OnCreate()
.
例如,我们的主要 android 应用程序希望在平板电脑上以横向模式使用。我们在OnCreateView()
或 中执行此操作OnCreate()
。
On a Nexus 7, the default view is portrait so what happens is that it starts the activity in portrait mode, our code then says go to landscape and android ultimately creates the activity
class 3 times!
在 Nexus 7 上,默认视图是纵向的,所以它以纵向模式启动 Activity,然后我们的代码说转到横向,而 android 最终创建了activity
3 次类!
We've hooked up network requests to onCreate
and they end up happening 3 times in this case.
我们已经连接了网络请求,onCreate
在这种情况下它们最终发生了 3 次。
Sure, we can add logic to look for duplicate calls but, in my opinion, it would be better, architecturally to try and divide the UI from the business logic.
当然,我们可以添加逻辑来查找重复调用,但在我看来,在架构上尝试将 UI 与业务逻辑分开会更好。
My recommendation would be to use the factory pattern to create presenters from the activity but make sure the factory only ever returns the same instance. The presenter can then contain logic to do network request, look for duplicates and return cached results and general business logic.
我的建议是使用工厂模式从活动创建演示者,但确保工厂只返回相同的实例。然后,演示者可以包含执行网络请求、查找重复项并返回缓存结果和一般业务逻辑的逻辑。
When results from network calls return, either post to a bus such as Otto which the activity (register for the event on onResume()
and deregister during onPause()
) has registered to, or make sure the callback interface implemented by the activity has been updated to the last activity in the presenter.
当网络调用的结果返回时,要么发布到活动(注册事件 ononResume()
并取消注册期间onPause()
)已注册到的总线(例如 Otto ),要么确保活动实现的回调接口已更新为中的最后一个活动主持人。
This way, code in the presenter
downwards is unit testable and not reliant on flaky UI layer testing.
这样,presenter
向下的代码是可单元测试的,而不依赖于片状的 UI 层测试。
回答by Pentium10
The actions, views and activies in Android are the baked in way of working with the Android UI and are an implementation of a model-view-viewmodel pattern, which is structurally similar (in the same family as) model view controller.
Android 中的动作、视图和活动是与 Android UI 一起工作的烘焙方式,并且是模型-视图-视图模型模式的实现,该模式在结构上与模型视图控制器相似(在同一家族中)。
To the best of my knoweledge, there is no way to break out of this model. It can probably be done, but you would likely lose all the benefit that the existing model has, and have to rewrite your own UI layer to make it work.
据我所知,没有办法打破这种模式。它可能可以完成,但您可能会失去现有模型所具有的所有好处,并且必须重写您自己的 UI 层以使其工作。
You can find MVC in the followings:
您可以在以下位置找到 MVC:
- You define your user interfacein various XML files by resolution/hardware etc.
- You define your resourcesin various XML files by locale etc.
- You store data in SQLiteor your custom data in /assets/ folder, read more about resources and assets
- You extend clases like ListActivity, TabActivityand make use of the XML file by inflaters
- You can create as many classes as you wish for your model, and have your own packages, that will act as a structure
- A lot of Utilshave been already written for you. DatabaseUtils, Html,
- 您可以通过分辨率/硬件等在各种 XML 文件中定义用户界面。
- 您可以按区域设置等在各种 XML 文件中定义资源。
- 您将数据存储在SQLite 中或将您的自定义数据存储在 /assets/ 文件夹中,阅读有关资源和资产的更多信息
- 您延长clases就像ListActivity,TabActivity并利用由XML文件的inflaters
- 您可以为模型创建任意数量的类,并拥有自己的包,这些包将充当结构
- 已经为您编写了很多实用程序。数据库实用程序,HTML,
There is no single MVC Pattern you could obey to. MVC just states more or less that you should not mingle data and view, so that e.g. views are responsible for holding data or classes which are processing data are directly affecting the view.
没有您可以遵守的单一 MVC 模式。MVC 只是或多或少声明你不应该混合数据和视图,例如视图负责保存数据或处理数据的类直接影响视图。
But nevertheless, the way Android deals with classes and resources, you're sometimes even forced to follow the MVC pattern. More complicated in my oppinion are the activites which are responsible sometimes for the view but nevertheless act as an controller in the same time.
但是,Android 处理类和资源的方式,有时甚至不得不遵循 MVC 模式。在我看来,更复杂的是有时负责视图但同时充当控制器的活动。
If you define your views and layouts in the xml files, load your resources from the res folder, and if you avoid more or less to mingle this things in your code, then your anyway following a MVC pattern.
如果你在 xml 文件中定义你的视图和布局,从 res 文件夹加载你的资源,并且如果你或多或少地避免在你的代码中混合这些东西,那么你无论如何都遵循 MVC 模式。
回答by Shivaraj Patil
MVPis the latest architecute most people are following Here is the small documentationAs Uncle Bob's clean architecturesays, “Architecture is About Intent, not Frameworks”
MVP是大多数人都在关注的最新架构 这是小文档正如鲍勃叔叔的简洁架构所说,“架构是关于意图,而不是框架”
Watch this videoit's just mindblowing good.
观看此视频,这真是令人兴奋。
回答by pcjuzer
Here is a dedicated project for Android Architecture blueprintswith well documented source codes. All of them are based on the MVP pattern with several twists. Also check the comparisonof the various solutions based on lines-of-code, testability, cost of learning, their support for increasing data complexity. It depends on the particularly developed app and the context (time to market, developers, future plans, etc.) which blueprint fits best.
这是一个专门用于Android 架构蓝图的项目,其中包含详细记录的源代码。所有这些都基于 MVP 模式,并经过了一些曲折。还要检查基于代码行、可测试性、学习成本、它们对增加数据复杂性的支持的各种解决方案的比较。这取决于特别开发的应用程序和蓝图最适合的背景(上市时间、开发人员、未来计划等)。