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

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

问题描述

我是一名JaveEE开发人员。最近我加入了一个Android开发团队。 Android的结构让我感到困惑。 MVC设计模式似乎不适合Android开发。那么Android开发的设计模式原理是什么?我的意思是有没有提示如何写一个干净,简单的阅读和有效的Android代码。

解决方案

Android的架构首先让我烦恼,但是我开始看到一种疯狂的方法。这是Android文档很难解释的。我最大的抱怨一直是,很难有一个集中的数据模型与您的活动共享的对象就像一个普通的应用程序。 Android似乎希望我成为游牧民族,因为我只能在我的活动之间共享原文。在数据库中放垃圾并不是一个模型,因为它不包含任何行为。所以就像大部分人一样,我的业务逻辑都在我的活动中结束,很难在其他活动中分享业务逻辑。



我来了解我是否缺少一些关键拼图Android是MVC。但是,它相当重要的是查看。


  1. 活动==控制器

  2. 模型==应用程序的子类

  3. 任何子类视图==查看

有趣的是,您可以创建一个子类的应用程序,并在您的清单文件中声明这一点,Android将创建一个该对象的单个实例,生活在您的应用程序的长度,无论什么活动被销毁或创建。这意味着您可以在那里建立所有活动都可以访问的集中式数据模型。



我看到的方式就像一个原始的Spring容器,可以初始化对象并解决它们之间的依赖关系。这样,您可以将应用程序的模型部分与活动本身分离。只需让Activity在模型上进行调用,并且手回调接收结果,以便更新UI。



Android的问题是它将控制器和看起来很重。例如,像TabActivity,ListActivity这样的子类意味着使用某个视图。所以交换一个观点是相当涉及的。而且,即使您使用活动,Controller也会对该视图进行非常具体的假设。他包含对诸如TextView等UI对象的直接引用。它注册了诸如点击,键盘等的低级事件。



如果Activity可以注册诸如登录,更新帐户余额等更高级别的事件,视图将针对一系列点击,键盘和触摸事件进行调度。这样一来,控制器就可以在这个级别上进行工作,而不是设计功能。



我想我们最终将会遇到这种类型的设计,更好的工具和技术。似乎Android可能具有可扩展性来实现这一点,但由社区制定图表。


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

解决方案

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.

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. Activity == Controller
  2. Model == Subclass of Application
  3. Anything that subclasses View == View

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.

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.

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.

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天全站免登陆