什么是设计模式的原则,在Android开发? [英] what's design pattern principle in the Android development?

查看:91
本文介绍了什么是设计模式的原则,在Android开发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个JaveEE开发商。最近我加入了一个Android开发团队。机器人的结构搞糊涂了。 MVC设计模式似乎并不适合于Android的发展。那么,有什么是设计模式的原则为Android开发?我的意思是有关于如何写一个干净的,易于阅读和有效的Andr​​oid code任何暗示。

I was a JaveEE developer. Recently I joined an Android development team. The structure of Android confused me. The MVC design pattern seems not suit for Android development. So what's is the design pattern principle for Android development? I means is there any hint about how to write a clean, easy reading and effective Android code.

推荐答案

Android的架构惹恼了我在第一,但我开始看到一个方法,他们的疯狂。它由Android的文件解释不清。我最大的抱怨一直是很难与你分享的活动,就像一个正常的应用程序对象一个集中的数据模型。 Android的似乎想要我成为一个游牧民族,因为我只能我的活动之间共享原语。而在一个数据库中落下的垃圾是不是一个模型,因为它不包含任何行为。所以,大多数人我的业务逻辑都结束了在我的活动使得它很难在其他活动共享业务逻辑。

Android's architecture annoyed me at first, but I beginning to see a method to their madness. It's poorly explained by the android documentation. My biggest gripe has always been that it's hard to have a centralized data model with objects that your Activities share just like a normal application. Android seemed to want me to be a nomad because I could only share primitives between my Activities. And dropping junk in a database is NOT a model because it contains no behavior. So as most people my business logic all ends up in my activity making it hard to share business logic in other activities.

我来看看我是缺少一些关键拼图。 Android是MVC。然而,它的耦合到视图相当严重。

I've come to find out I was missing some key puzzle pieces. Android is MVC. However, it's coupled to the View fairly heavily.

  1. 活动==控制器
  2. 型号==应用的子类
  3. 在任何的子类查看==查看

有趣的是,你可以创建应用程序的一个子类,并在你的清单文件中声明这一点,而Android将创建该对象,生活应用程序的长度,无论是什么活动被破坏,或创建的一个实例。这意味着你可以建立一个集中的数据模型,还有所有的活动有机会获得。

Interestingly you can create a subclass of Application and declare this in your Manifest file, and Android will create a single instance of this object that lives the length of your application no matter what Activity is destroyed or created. That means you can build a centralized data model there that all Activities have access to.

我看到这个问题的方法是这样一个原始的Spring容器,你可以初始化对象和解决它们之间的依赖关系。这样,你可以分离应用程序的模型部分离活动本身。而只是有活动做出的模型调用,并用手回调接受的结果,因此它可以更新UI。

The way I see this is something like a primitive Spring container that you can initialize objects and resolve dependencies between them. That way you can decouple the model portion of your application away from the Activity themselves. And just have the Activity make calls on the model, and hand callbacks to receive the results so it can update the UI.

与Android的问题是,它严重混合控制器和视图pretty的。例如,像TabActivity子类,ListActivity意味着被用于某些视图。所以,换出一个观点是参与pretty的。另外,控制器使得非常具体的假设的看法是什么,即使你使用的活动。他包含直接引用类似的TextView等UI对象和它注册为喜欢的点击低级别事件,键盘等。

The problems with Android is that it mixes controller and view pretty heavily. For example, subclasses like TabActivity, ListActivity imply a certain view being used. So swapping out a view is pretty involved. Also the Controller makes very specific assumptions about what the view is even if you use Activity. He contains direct references to UI objects like TextView, etc. And it registers for low level events like clicks, keyboard, etc.

这将是更好,如果活动可能注册,如登录,更新账户余额等,对此的看法将响应派遣一系列的点击,键盘,触摸事件的更多高水平的赛事。这样,控制器工作在你的水平可以描述的功能,而不是设计功能。

It would be better if Activity could register for more high level events like "Login", "Update Account Balance", etc which the view would dispatch in response to a series of clicks, keyboard, touch events. That way the controller works at the level you might describe features instead of design features.

我想我们会实现这个类型的设计,最终为我们更好地了解拿出更好的工具和技术。它看起来像Android这样可能具有可扩展性,以做到这一点,但它给社会以图表吧。

I think we'll reach this type of design eventually as we better understand come up with better tools and techniques. It seems like Android might have the extensibility to make this happen, but it's up to community to chart it.

这篇关于什么是设计模式的原则,在Android开发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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