扩展应用程序或使用singletion? [英] Extending application or using singletion?

查看:173
本文介绍了扩展应用程序或使用singletion?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android项目中,我有一个或多个我的活动都需要接取,现在我想创建应用程序的一个子类的不同对象然而,在< A HREF =htt​​p://developer.android.com/reference/android/app/Application.html相对=nofollow>应用程序的文档其规定如下:

I have an android project where i have different objects that one or more of my activities need to acess now i was thinking of creating a subclass of Application however under the documentation of Application it states the following:

通常没有必要的子类的应用。在大多数情况下,静态单身可以提供在一个更模块化的方式相同的功能。如果你单身需要一个全球范围内(例如注册广播接收器),该函数检索它可以给内部使用Context.getApplicationContext()上下文时,首次构建了单。

There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

我的问题是相当简单的是使用静态单独的类来包含所有对象的最好的初步实践?还是我正确的假设,推广应用是一个更好的选择?

My question is fairly simple is it best pratice to use a static singleton class to contain all of your objects ? or am i right to assume that extending application is a better option?

推荐答案

要回答你的问题,我会用一个单独的容器来访问这些对象,初始化类与应用程序上下文的上下文(有你需要非常大的机会上下文);但你会看到它是一种很难维持这些和单容器。

To answer your question I would use a singleton container to access these objects, initialize that class with a context by application context (there are very big chances you will need a Context); but then you will see it's kind of hard to maintain these and the singleton container.

要解决这个对象图的问题,我会用一些IOC: RoboJuice ,的 AndroidAnnotations 匕首真的很酷,他们提供了更多。他们每个人处理这个问题不同,但你不必担心。

To solve this object graph issue, I would use some IoC: RoboJuice, AndroidAnnotations or Dagger are really cool and they provide much more. Each of them handles this issue different, but you don't have to worry about that.

我希望它能帮助!

这篇关于扩展应用程序或使用singletion?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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