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

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

问题描述

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



在iOS模型视图控制器中与委托和其他模式一起使用非常广泛。



Android使用什么样的模式和特定的位置?



编辑 p>

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

解决方案

我尝试使用 model-view-controller (MVC)和 model-view-presenter 进行Android开发的设计模式。我的发现是模型视图控制器工作正常,但有几个问题。这一切都归结于你如何看待Android 活动类。



实际的活动类不会扩展Android的 View class,但是它可以处理向用户显示一个窗口,并处理该窗口的事件(onCreate,onPause等)。



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



在MVC中,控制器应该是主要的入口点。由于活动是大多数应用程序的自然切入点,因此在Android开发中应用这种情况是有争议的。



正因为如此,我个人发现模型视图呈现者模式非常适合Android开发。由于观点在此模式中的作用是:




  • 作为入口点

  • 渲染组件

  • 将用户事件路由到演示者



这样可以实现您的模型: / p>

查看 - 这包含您的UI组件,并为他们处理事件。



演示者 - 这将处理您的模型和视图之间的通信,将其视为您的模型的门户。意思是说,如果你有一个复杂的领域模型代表,上帝知道什么,你的观点只需要这个模型的一小部分,演示者的工作是查询模型,然后更新视图。例如,如果您有一个包含文本段落,标题和字数的模型。但是在给定的视图中,您只需要在视图中显示标题。然后主持人将读取模型所需的数据,并相应更新视图。



模型 - 这应该是您的完整域模型。希望这将有助于使您的域模型更紧,因为您不需要特殊的方法来处理如上所述的案例。



通过解耦模型从所有的观点(通过使用演示者),它也变得更直观的测试你的模型。您可以对您的域模型进行单元测试,并为演示者进行单元测试。



尝试一下。我个人觉得很适合Android开发。


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.

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

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