Reputation: 3804
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?
Upvotes: 0
Views: 113
Reputation: 2161
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):
Upvotes: -1
Reputation: 31
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.
Upvotes: 2
Reputation: 3956
I think the singleton object is just existed in memory after alloced first time, it should not be owned by any object.
Upvotes: 0