Android 上使用了哪些架构模式? [英] Which Architecture patterns are used on Android?

查看:19
本文介绍了Android 上使用了哪些架构模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对移动平台进行小型研究,我想知道 Android 中使用了哪些设计模式?

I'm doing a small research of mobile platforms and I would like to know which design patterns are used in Android?

例如在 iOS 中,模型-视图-控制器与委托等模式一起使用非常广泛.

e.g. in iOS Model-view-controller is very widely used together with delegation and other patterns.

Android 使用哪些模式以及具体使用哪些地方?

What patterns and where in particular does Android use?

编辑

我不是要求在内核、dalvik 等中深入使用的设计模式,而是应用程序开发人员在开发应用程序时会遇到的模式.

I'm not asking for design patterns used deep in kernel, dalvik and so on, but about patterns which an application developer will meet while developing an application.

推荐答案

我尝试同时使用 model–view–controller (MVC) 和 model–view–presenter 用于进行 android 开发的架构模式.我的发现是模型-视图-控制器工作正常,但有几个问题".这一切都取决于您如何看待 Android Activity 类.是控制器还是视图?

I tried using both the model–view–controller (MVC) and model–view–presenter architectural patterns for doing android development. My findings are model–view–controller works fine, but there are a couple of "issues". It all comes down to how you perceive the Android Activity class. Is it a controller, or is it a view?

实际的Activity 类并没有扩展Android 的View 类,但它确实处理向用户显示一个窗口并处理该窗口的事件(onCreate、onPause 等).

The actual Activity class doesn't extend Android's View class, but it does, however, handle displaying a window to the user and also handle the events of that window (onCreate, onPause, etc.).

这意味着,当您使用 MVC 模式时,您的控制器实际上将是一个伪视图控制器.由于它正在处理向用户显示窗口,以及您使用 setContentView 添加的附加视图组件,并且至少还处理各种活动生命周期事件的事件.

This means, that when you are using an MVC pattern, your controller will actually be a pseudo view–controller. Since it is handling displaying a window to the user, with the additional view components you have added to it with setContentView, and also handling events for at least the various activity life cycle events.

在 MVC 中,控制器应该是主要的入口点.如果在将其应用于 Android 开发时是这种情况,这有点值得商榷,因为 Activity 是大多数应用程序的自然切入点.

In MVC, the controller is supposed to be the main entry point. Which is a bit debatable if this is the case when applying it to Android development, since the activity is the natural entry point of most applications.

正因如此,我个人认为model-view-presenter模式非常适合Android开发.由于视图在此模式中的作用是:

Because of this, I personally find that the model–view–presenter pattern is a perfect fit for Android development. Since the view's role in this pattern is:

  • 作为切入点
  • 渲染组件
  • 将用户事件路由到演示者

这允许您像这样实现模型:

This allows you to implement your model like so:

View - 这包含您的 UI 组件,并为它们处理事件.

View - this contains your UI components, and handles events for them.

Presenter - 这将处理模型和视图之间的通信,将其视为通往模型的网关.意思是,如果你有一个复杂的域模型表示,天知道是什么,并且你的视图只需要这个模型的一个非常小的子集,演示者的工作是查询模型然后更新视图.例如,如果您有一个包含一段文本、一个标题和一个字数统计的模型.但是在给定的视图中,您只需要在视图中显示标题.然后 Presenter 会从模型中读取需要的数据,并相应地更新视图.

Presenter - this will handle communication between your model and your view, look at it as a gateway to your model. Meaning, if you have a complex domain model representing, God knows what, and your view only needs a very small subset of this model, the presenters job is to query the model and then update the view. For example, if you have a model containing a paragraph of text, a headline and a word-count. But in a given view, you only need to display the headline in the view. Then the presenter will read the data needed from the model, and update the view accordingly.

模型 - 这基本上应该是您的完整域模型.希望它也有助于使您的域模型更加紧密",因为您不需要特殊方法来处理上述情况.

Model - this should basically be your full domain model. Hopefully it will help making your domain model more "tight" as well, since you won't need special methods to deal with cases as mentioned above.

通过将模型与视图完全分离(通过使用演示器),测试模型也变得更加直观.您可以对领域模型进行单元测试,对演示者进行单元测试.

By decoupling the model from the view all together (through use of the presenter), it also becomes much more intuitive to test your model. You can have unit tests for your domain model, and unit tests for your presenters.

试试看.我个人认为它非常适合 Android 开发.

Try it out. I personally find it a great fit for Android development.

这篇关于Android 上使用了哪些架构模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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