它的设计模式上使用的Andr​​oid? [英] Which design patterns are used on Android?

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

问题描述

我在做移动平台的小型研究,我想知道哪些设计模式在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.

推荐答案

我试着用这两个模型 - 视图 - 控制器和模型 - 视图 - presenter设计模式做Android开发。我的发现是模型 - 视图 - 控制工作正常,但有几个问题。这一切都归结到你如何看待安卓活动类。它是一个控制器,还是一个看法?

I tried using both the Model-View-Control and Model-View-Presenter design patterns for doing android development. My findings are Model-View-Control works fine, but there is a couple of "issues". It all comes down to how you perceive android Activity class. Is it a controller, or is it a view?

实际活动类不会扩展Android的查看类,但它确实,但是,处理显示窗口向用户和也处理那个窗口(的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附加视图组件,并且也处理事件为ATLEAST上的各种活动的生命周期事件

This means, that when you are using a 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 atleast the various activity life cycle events.

在MVC中,应该将控制器的主入口点。这是有点值得商榷如果是这种情况时施加到机器人的开发中,由于活动是大多数应用的自然进入点

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.

,我personall发现的模型 - 视图 - presenter 模式是非常适合Android开发。因为在这种模式中查看角色有:

Because of this, i personall find that the Model-View-Presenter pattern is a perfect fit for android development. Since the Views role in this pattern are:

  • 作为切入点
  • 渲染组件
  • 在路由用户事件到presenter

这可以让你实现你的模型像这样:

This allows you to implement your model like so:

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

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

presenter - 这将处理您的模型和视图之间的沟通,看它作为一个网关到模型。含义,如果你有一个复杂的领域模型重新presenting,上帝知道是什么,和你的看法只需要这个模型的一个非常小的子集,在presenters工作是查询模型,然后更新视图。例如,如果有包含文本,标题和一个字计数的一个段落的模型。但是,在一个给定的视图,你只需要在视图中显示的标题。然后将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 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 wont need special methods to deal with cases as mentioned above.

通过分离视图中的模型一起(通过使用presenter),它也变得更加直观的测试模型。你可以有单元测试你的域模型,和单元测试你的presenters。

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 test for your domain model, and unit test for your presenters.

试试吧,我个人觉得这是一个非常适合Android的发展。

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

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

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