News for hackers. Stuff that matters.
Re:Google Singleton Detector by Jasber on Friday July 08, 2011 @05:06PM (#2744103)
As a new Objective-C programmer, I recently ran into the global state problem with an iPhone game I'm working on. I needed some settings that could be accessed/changed from many different classes. I was told the AppDelegate isn't the right place to put this.

A singleton SettingsManager was suggested, but singletons always get a bad wrap.

So is this a good use of a singleton? What would be the correct way to handle this?

Re:Google Singleton Detector by glassx on Friday July 08, 2011 @05:19PM (#2744149)
YMMV, but the way I prefer doing it is by telling the settings explicitly to the different classes, instead of having all those classes rely on getting it by themselves from the singleton (this is where the problem lies).

You can pass the settings in the constructors, with setter methods or have a "state object" that contains the configuration.

For "updating" the states on every class in real time I'd rather rely on explicit state change using the Observer pattern.

EDIT: I'd also like to know what other developers do, or if there's anything wrong with my methods, so I second your question.

Re:Google Singleton Detector by mirkules on Friday July 08, 2011 @06:32PM (#2744354)
I have been wondering the same, about other developers.

I am working on an application that periodically pulls information from a server about a physical object (sorry, I can't be more specific than this, so please don't ask). This information needs to be accessed by several different classes, and displayed in view controllers on different tabs. A view controller will pull the information and make it available to all the other view controllers.

A singleton provided an effective solution to this because only one data model of my physical object's state can exist at any given time, with the added benefit that I can access it from any view controller without much effort.

I may be wrong, but I thought the purpose of a singleton was for models where there can be one and only one instance of your class? Singletons (like other design patterns) were designed to solve a specific problem. But I concede that they are probably overused.

Re:Google Singleton Detector by rbarooah on Friday July 08, 2011 @06:33PM (#2744360)
Letting individual classes get settings from NSUserDefaults (which itself effectively presents a singleton) directly seems to work pretty well for me.

One advantage is that the classes are then reusable in other projects because they aren't coupled to an application specific settings provider.

Re:Google Singleton Detector by ardit33 on Friday July 08, 2011 @06:21PM (#2744330)
hey Jaber. Singletons are fine for that purpose, actually they are probably the best solution.

iOS uses singletons everywhere, so feel free to use them.

Don't fall the trap of the cargo culture thinking. Google has a completely different problem than you do. Their code spans hundreds of thousands (or millions), of code, in distributed systems, etc..

your iOS app probably has different constrains.

Re:Google Singleton Detector by mirkules on Friday July 08, 2011 @06:46PM (#2744403)
Don't fall the trap of the cargo culture thinking. Google has a completely different problem than you do. Their code spans hundreds of thousands (or millions), of code, in distributed systems, etc..

This is probably the single most useful quote in this entire discussion. Singletons are not inherently bad -- they were created for a reason, but because people abuse them, they have a bad rep.

Re:Google Singleton Detector by misterbee on Sunday July 10, 2011 @02:27AM (#2747186)
If you aren't writing an actual hardware device driver for a specific peripheral port, you are abusing Singleton.
Re:Google Singleton Detector by mirkules on Sunday July 10, 2011 @02:42AM (#2747206)
There are many instances where you need just one instance of a class, not just a hardware device driver (for instance, when you need GPS location coordinates on a mobile device across several different classes).
Re:Google Singleton Detector by misterbee on Sunday July 10, 2011 @02:26AM (#2747185)
No, Settings/Configuration is a classic example of where using a Singleton horribly breaks testability, because you can't run multiple "application runs" in the same "test run" without completely exiting and rebuilding the app runtime environment.
Re:Google Singleton Detector by Confusion on Sunday July 10, 2011 @04:01PM (#2748479)
Testing frameworks exit and rebuild the app runtime environment all the time, to isolate the effects of one test case from the next. When your singleton instantiation is paramatrized, for instance with a configuration file, than there is no testability problem whatsoever.

I would go as far as to say that if you have to do multiple "application runs" in the same "test run", where these "application runs" require different configurations to have the test cover some specific scenario, then your code has larger problems than having the configuration in a singleton.

Re:Google Singleton Detector by hoopadoop on Friday July 08, 2011 @06:33PM (#2744357)
Where does IOS use Singletons?
Re:Google Singleton Detector by rbarooah on Friday July 08, 2011 @06:48PM (#2744411)
Depends on your definition of singleton.

In java, singletons are enforced by the type system - i.e. There's no public way to make more of the object. Usually that's what people mean by 'singleton' in java. If you don't have the source and you outgrow the single instance, there's nothing you can do except stop using the class.

In Objective-C, people do occasionally simulate that by mungng init and alloc, but that is definitely a bad idea. More often a singleton class will have a conventional init and alloc, but have a class method like +sharedInstance which lazily instantiates and dispenses a single instance of the object during the lifetime of the application. That's what people most often mean by 'singleton' in objective-c. It's basically a global variable, but when you outgrow it, it's often not hard to switch to allocating more than one.

Also, partly due to what many would see as a deficiency - the fact that it's much harder to produce architecture independent libraries in objective-c than java, the source is available more of the time, so evolving past a singleton is more often straightforward.

Re:Google Singleton Detector by ardit33 on Friday July 08, 2011 @06:40PM (#2744385)
any method that has 'shared' or 'current' in front.

http://developer.apple.com/library/ios/#documentation/uikit/...

It is almost everwhere.

[MyClass sharedMyclass] is the Objective-C equivalent of getInstance()

Also, singletons make good sense in mobile apps, as it is a very different type of a beast than a server side app.

Re:Google Singleton Detector by mirkules on Friday July 08, 2011 @06:43PM (#2744397)
Re:Google Singleton Detector by rbarooah on Friday July 08, 2011 @06:36PM (#2744372)
If you're using NSUserDefaults, why bother with a SettingsManager? If the SettingsManager is an NSUserDefaults replacement for whatever reason, then it's probably fine for it to be a singleton.
Re:Google Singleton Detector by tszming on Saturday July 09, 2011 @01:26AM (#2745088)
Google also use singleton, it really depends on the context and how it's used.

e.g. http://code.google.com/mobile/analytics/docs/iphone/

Re:Google Singleton Detector by hoopadoop on Friday July 08, 2011 @05:46PM (#2744241)
The correct way to deal with it is to realise that if you need some settings that can be accessed/changed from many different classes then the design of your app is severely lacking. However, this doesn't necessarily that you need a more complex/sophisticated 'design'. ie. if you stick to TDD this situation is highly unlikely.

Hopefully this will stem the tide of questions on Stack Overflow about different Objective-c singleton implementations.

Re:Google Singleton Detector by equark on Friday July 08, 2011 @06:00PM (#2744265)
The correct way to deal with this is to realize you have a problem? I think the above comment is willing to admit a problem and is asking for a solution. What does TDD have to do with it?

I'm also interested in hearing what is considered the best practice for things like passing around config options, an eventloop, or message bus.

Re:Google Singleton Detector by saraid216 on Friday July 08, 2011 @06:13PM (#2744302)
The solution is essentially to take a hard look at your design and see why you have to pass around config options, event loops, or message buses to begin with. The need to access global state is a code smell.

When you DO need global state, then a singleton object is a reasonable way to achieve it. But chances are fairly high that you don't.

Re:Google Singleton Detector by ebiester on Friday July 08, 2011 @08:14PM (#2744618)
You can look for days, and if you don't know any of the alternatives, none will pop out.

As I'm not in the mobile sphere, I wouldn't know how to avoid a singleton for an app that is constrained by speed and space requirements. I've already shown an example above, what's your solution in this environment, with the constraints of the questioner?

Re:Google Singleton Detector by Deestan on Friday July 08, 2011 @06:43PM (#2744398)
> if you need some settings that can be accessed/changed from many different classes then the design of your app is severely lacking.

If he's making a game, this is completely acceptable design. Games programming is wildly different from application or server programming. Also, strict TDD is much, much less useful in games programming than anywhere else. Writing unit tests for central data structures and algorithms is useful, of course, but the majority of modules/classes/objects are so small, uncritical, and change completely so often that it's not worth bothering.