哪些设计模式是在Android上使用的?[英] Which design patterns are used on Android?

本文是小编为大家收集整理的关于哪些设计模式是在Android上使用的?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

例如在 iOS 中 Model-view-controller 与委托和其他模式一起使用非常广泛.

Android 使用哪些模式以及具体在哪里使用?

编辑

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

推荐答案

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

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

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

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

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

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

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

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

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

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

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

试试看.我个人觉得它非常适合 Android 开发.

本文地址:https://www.itbaoku.cn/post/627512.html

问题描述

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

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

What patterns and where in particular does Android use?

EDIT

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.

推荐答案

I tried using both the model–view–controller (MVC) and model–view–presenter design 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?

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.).

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.

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.

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:

  • Serving as a entry point
  • Rendering components
  • Routing user events to the presenter

This allows you to implement your model like so:

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

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 know 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.

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