0

In my document-based application, I have an AppController that deals with opening Prefs, the About panel, etc. I also have a singleton class; since it's basically a global object that gets passed around, who should own it? An object that uses it or my AppController? Some articles I read say that you should have one "central" place - like a Delegate, others say that it's bad design, and that only an object that uses class X should own class X. What's your take?

janeh
  • 3,734
  • 7
  • 26
  • 43

3 Answers3

2

Nothing really owns the singleton because it is stored in a static variable like this:

static Globals *sharedGlobals = nil;

@implementation Globals

+ (Globals *) sharedGlobals {
    if (!sharedGlobals) sharedGlobals = [[Globals alloc] init];
    return sharedGlobals;
}

The first time the sharedGlobals method gets called, the singleton will be created by whichever class called it. It doesn't really matter who calls it first.

Whit
  • 31
  • 1
  • 4
  • 2
    This singleton pattern lacks thread-safe access though. If you are targeting OS X 10.6+ or iOS 4.0+, then you can use `dispatch_once()` to provide thread-safe singleton access. Otherwise wrap the alloc-init in an `@synchronized` block. See Mike Ash on [The Care and Feeding of Singletons](http://www.mikeash.com/pyblog/friday-qa-2009-10-02-care-and-feeding-of-singletons.html). – FluffulousChimp Aug 30 '12 at 05:01
  • Yep. With this (or similar) implementation, the class object ("Globals" here) owns the singleton instance. Definitely see the Mike Ash link. – rickster Aug 30 '12 at 06:55
  • I am using ARC here, but in non-ARC- if nobody owns it, can anybody release it? – janeh Aug 30 '12 at 13:23
  • @alanduncan , I thought that `@synchronized` gives you a recursive lock, which is expansive for most apps? – janeh Aug 30 '12 at 13:37
  • @janeh - Yes. The `dispatch_once()` version of singleton instantiation is reputedly faster; but I haven't measured it. – FluffulousChimp Aug 30 '12 at 18:21
0

I think the singleton object is just existed in memory after alloced first time, it should not be owned by any object.

Mil0R3
  • 3,876
  • 4
  • 33
  • 61
  • I guess by "own" I meant - the object that should initially create an instance of it. – janeh Aug 30 '12 at 02:52
  • 2
    @janeh -- Generally, for a singleton, the first object to reference it is the one that creates the instance, via the first-reference detection logic in the singleton implementation. – Hot Licks Aug 30 '12 at 03:04
-1

You shouldn't use a singleton as a way to define a global object, the singleton pattern is intented to provide a way to assure an unique instance in one context. But the problem is that if you don't use a dependency injection framework and implement it through java, the static method used to implement the pattern lets all application classes access freely to the singlenton. So its a way to start destroying your own module/application design (if you don't control it correctly).

Take a look at this posts before you decide to use or not the singleton pattern (both against and pro):

Community
  • 1
  • 1
kothvandir
  • 2,111
  • 2
  • 19
  • 34
  • This isn't a "general-purpose" C++ or Java design patterns question; it's a question about a specific pattern common in Objective-C. – rickster Aug 30 '12 at 06:52
  • The last phrase "Some articles I read say that you should have one "central" place - like a Delegate, others say that it's bad design, and that only an object that uses class X should own class X. What's your take?" Ask for and opinion and implies a design pattern question, I think the -1 vote is not justified. – kothvandir Aug 30 '12 at 06:59