On Thu, Mar 10, 2011 at 9:11 AM, Kenny Riddile <kfridd...@gmail.com> wrote:

> Perhaps my blanket statement was to simplistic.  Not ALL singletons "are"
> global variables.  Also, not all globals are variables (globally accessible
> static constants for example).  However, all STATEFUL singletons are
> globals, and all stateful singletons with mutable state (via methods or
> whatever) are essentially global variables as they create the same
> architectural issues.
>

Hmm, perhaps this is semantics at this point. I don't think a singleton is
"a global", as it's usually a private member that is statically accessible.
However, there is "global access", of course.


> Just because a singleton's state is only mutable by using its interface
> doesn't change the fact that you are varying globally accessible state.
>

No, but in terms of clarity, readability, and ease of debugging, there is a
*huge* difference in using a singleton to modify some globally accessible
object versus a "classic global".


> If you only need one instance of a class, then just make one.  It really is
> that simple. Then give that instance to whomever needs it, instead of
> letting them magically pull it from the ether.
>

Normally, I wholeheartedly agree. However, the Android model and intricacies
of the lifecycle of an Android app present some unique challenges with
regard to managing and passing around objects that are needed across
multiple Activities. So it's not always that simple. Sometimes, when
considering ease and speed of implementation, readability,
and maintainability, a Singleton really is the best option.

-------------------------------------------------------------------------------------------------
TreKing <http://sites.google.com/site/rezmobileapps/treking> - Chicago
transit tracking app for Android-powered devices

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to