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

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

问题描述

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

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

Android使用什么模式,特别是在什么地方使用?

编辑

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

解决方案

我尝试同时使用Activity类的看法.是控制器还是视图?

实际的Activity类没有扩展Android的View类,但是可以处理向用户显示窗口以及处理该窗口的事件(onCreate,onPause等)./p>

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

在MVC中,控制器应该是主要的入口点.如果将其应用到Android开发中,情况确实值得商bat,因为该活动是大多数应用程序的自然切入点.

因此,我个人发现模型–视图–演示者模式非常适合Android开发.由于视图在此模式中的作用是:

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

这使您可以像这样实现模型:

视图-它包含您的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 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?

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

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

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

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