Singleton: Difference between revisions

Content added Content deleted
(Created Global Singleton class)
 
No edit summary
Line 40: Line 40:
// And any other protected methods.
// And any other protected methods.
}
}
</pre>


=={{header|Objective-C}}==
(Using Cocoa's NSObject as a base class)
<pre>
// SomeSingleton.h
@interface SomeSingleton : NSObject
{
// any instance variables
}

+ (SomeSingleton*)sharedInstance;

@end
</pre>

<pre>
// SomeSingleton.m
@implementation SomeSingleton

+ (GameController*) sharedInstance
{
static sharedInstance = nil;
if(!sharedInstance) {
sharedInstance = [[SomeSingleton alloc] init];
}
return sharedInstance;
}

@end
</pre>