Re: Followup - Re: Is there a pattern for creating an object with global scope?
Re: Followup - Re: Is there a pattern for creating an object with global scope?
- Subject: Re: Followup - Re: Is there a pattern for creating an object with global scope?
- From: Graham Cox <email@hidden>
- Date: Tue, 16 Apr 2013 10:18:04 +1000
On 15/04/2013, at 8:44 PM, Tom Davie <email@hidden> wrote:
> Wow, I really can't get my head around this one. You make bold statements like "GLOBALS ARE BAD NEWS" (which I 100% agree with), but then follow up with effectively "use singletons instead". Singletons bring with them 95% of the problems globals bring. They still break any attempts at threading, they still break any attempts at testing, they still break separation of concerns, and they're always avoidable. So I'd follow up with SINGLETONS ARE BAD NEWS TOO! In 95% of cases where you have a singleton, you should almost certainly be using dependancy injection instead, or some other method of avoiding it.
>
> Note also – simply using your app delegate as a store for things that aren't singletons, but only one of them is pointed at by your app delegate is also horrific – you're just substituting one singleton for another.
I'm not suggesting you use singletons all over the place, but judicious use of singletons is a reasonable and straightforward way to solve the global scope problem without directly using globals. Sometimes you need something with global scope, and the OP's use-case of preferences is a common example. Other things are a natural fit too, such as NSApplication.
I agree that they can bring many of the same problems with them, but some of the things you mention are not inherently a problem with singletons as long as you're aware of the issues. Threading for example, is easily taken care of. Are they a solution for every situation? No, I'm not suggesting they are, but I don't think they bring 95% of the same problems as globals. I don't know what you mean by "dependency injection", it's not a term I've heard of, but it's like anything - there are good and correct solutions to a problem and there are others which work but are non-optimal. The craftsman needs to be aware of the tools at his disposal and use the appropriate one for the task at hand. Globals are never required, singletons occasionally. Something else altogether usually.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden