On 3/9/2011 7:35 PM, TreKing wrote:
On Wed, Mar 9, 2011 at 4:30 PM, Kenny Riddile <[email protected]
<mailto:[email protected]>> wrote:
Assuming the singleton is modifiable via its interface, then for all
intents and purposes, yes, they are.
Again, no.
Singleton is a design pattern whose purpose is to simplify and control
access to a an object for which there is and will only be one instance
of. This instance is set once and used throughout. It does not vary or
change. It is not variable.
Of course you can change the *state* of the singleton object, if that's
what you mean by "modifiable", but if you change the *value* of the
object itself, as one would do with global *variables*, it is no longer
a singleton.
Even in the sense that the singleton state is modifiable, the whole
point of the singleton is to provide an interface through which this
globally accessible object, and its state, is manipulated. There is
controlled access. This is not the case with your run-of-mill, freely
accessible global variable.
On Wed, Mar 9, 2011 at 6:11 PM, David Williams
<[email protected] <mailto:[email protected]>> wrote:
Ok, trying to do this but struggling.
I created a class as follows:
If all you're doing is defining some *constants* that are not going to
change, extending Application is way overkill. Especially since casting
up to your Application type every time you need something makes your
code horrendous to look at.
You can just do this:
class Constants
{
public static final String KEY = "MyKey";
}
Then do Constants.Key where you need it. Done.
On Wed, Mar 9, 2011 at 6:11 PM, David Williams
<[email protected] <mailto:[email protected]>> wrote:
Sorry, this is just my lack of knowledge on Java here. I was hoping
it was something like globalVars.getApiKey(), but that doesn't seem
to work.
I highly recommend you brush up on Java, reviewing static and instance
level access of functions and data in particular.
-------------------------------------------------------------------------------------------------
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
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. Limiting my statement to stateful
singletons is kind of irrelevant though, since there's no point in
limiting a stateless type to one instantiation. 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. When I was
first starting my career, I thought singletons were great (after all, I
read about them in a book!), but experience has taught me that there is
almost always a better way than using mutable global state. Singletons
essentially get you two things:
- the guarantee that only one instance can exist
- global access to that single instance
The second is bad for all the reasons that global variables are
considered bad, and the first is unnecessary. 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.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en