单身主场迎战Android应用程序上下文? [英] Singletons vs. Application Context in Android?

查看:139
本文介绍了单身主场迎战Android应用程序上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

回顾这后用列举单身中的几个问题 而看过的Andr​​oid应用程序使用Singleton模式的几个例子,我不知道它是用单身,而不是通过全球应用国家共用的情况下是一个好主意(继承android.os.Application并通过context.getApplication得到它())。

Recalling this post enumerating several problems of using singletons and having seen several examples of Android applications using singleton pattern, I wonder if it's a good idea to use Singletons instead of single instances shared through global application state (subclassing android.os.Application and obtaining it through context.getApplication()).

有什么优势/缺点就这两种机制有哪些?

What advantages/drawbacks would both mechanisms have?

说实话,我希望在这个岗位<一个相同的答案href="http://stackoverflow.com/questions/2709071/singleton-pattern-with-web-application-not-a-good-idea">Singleton与Web应用程序,不是一个好主意格局!,但应用到Android。我对么?和以往不同的DalvikVM否则?

To be honest, I expect the same answer in this post Singleton pattern with Web application, Not a good idea! but applied to Android. Am I correct? What's different in DalvikVM otherwise?

编辑:我想对涉及到几个方面的意见:

I would like to have opinions on several aspects involved:

  • 同步
  • 可重用
  • 测试

推荐答案

我非常不同意戴安娜Hackborn(是的,我知道她是一个Android框架工程师,谢谢。)我们的点点去除我们的项目的所有单身位赞成重量轻,工作范围可easiliy重新创建当你真正需要它们的对象。

I very much disagree with Dianne Hackborn (and yes, I realize she's an Android framework engineer, thanks.) We are bit by bit removing all singletons from our project in favor of lightweight, task scoped objects which can easiliy be re-created when you actually need them.

单身是一个噩梦的测试,如果延迟初始化,将推出国家非决定论与微妙的副作用(从一个范围移动调用的getInstance()到另一个时可能突然面)。能见度已经提到另外一个问题,因为单身意味着全球(=随机)访问共享状态,微妙的错误可能出现在不并发应用正确同步。

Singletons are a nightmare for testing and, if lazily initialized, will introduce "state indeterminism" with subtle side effects (which may suddenly surface when moving calls to getInstance() from one scope to another). Visibility has been mentioned as another problem, and since singletons imply "global" (= random) access to shared state, subtle bugs may arise when not properly synchronized in concurrent applications.

我认为这是一种反模式,它常常被人们从程序语言如C,它是共同维护全球国家即将接受一个糟糕的面向对象的风格。

I consider it an anti-pattern, it's a bad object-oriented style often embraced by people coming from procedural languages like C, where it is common to maintain global state.

要回到你的问题: 尽管该应用上下文可以被认为是一个单本身,它是框架管理,并具有明确的生命周期,范围和访问路径。因此,我认为,如果你需要管理的应用程序全局状态,它应该在这里,别的地方。为别的,重新思考,如果你的真正的需要一个单独的对象,或者它也有可能重写你的单例类来代替实例执行手头的任务小,短期对象。

To come back to your question: Although the app context can be considered a singleton itself, it is framework-managed and has a well defined life-cycle, scope, and access path. Hence I believe that if you do need to manage app-global state, it should go here, nowhere else. For anything else, rethink if you really need a singleton object, or if it would also be possible to rewrite your singleton class to instead instantiate small, short-lived objects that perform the task at hand.

这篇关于单身主场迎战Android应用程序上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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