Singleton: Difference between revisions
Content added Content deleted
(Add Racket example) |
|||
Line 860: | Line 860: | ||
} |
} |
||
+ (SomeSingleton*)sharedInstance; |
+ (SomeSingleton *)sharedInstance; |
||
@end</lang> |
@end</lang> |
||
Line 867: | Line 867: | ||
@implementation SomeSingleton |
@implementation SomeSingleton |
||
+ (SomeSingleton*) sharedInstance |
+ (SomeSingleton *) sharedInstance |
||
{ |
{ |
||
static sharedInstance = nil; |
static SomeSingleton *sharedInstance = nil; |
||
if(!sharedInstance) { |
if (!sharedInstance) { |
||
sharedInstance = [[SomeSingleton alloc] init]; |
sharedInstance = [[SomeSingleton alloc] init]; |
||
} |
} |
||
Line 891: | Line 891: | ||
} |
} |
||
- (void)release |
- (oneway void)release |
||
{ |
{ |
||
// prevent release |
// prevent release |
||
Line 902: | Line 902: | ||
@end</lang> |
@end</lang> |
||
===Thread-Safe=== |
|||
Same as above except: |
|||
<lang objc>+ (SomeSingleton *) sharedInstance |
|||
{ |
|||
static SomeSingleton *sharedInstance = nil; |
|||
@synchronized(self) { |
|||
if (!sharedInstance) { |
|||
sharedInstance = [[SomeSingleton alloc] init]; |
|||
} |
|||
} |
|||
return sharedInstance; |
|||
}</lang> |
|||
===With GCD=== |
|||
Same as above except: |
|||
<lang objc>+ (SomeSingleton *) sharedInstance |
|||
{ |
|||
static SomeSingleton *sharedInstance = nil; |
|||
static dispatch_once_t onceToken; |
|||
dispatch_once(&onceToken, ^{ |
|||
sharedInstance = [[SomeSingleton alloc] init]; |
|||
}); |
|||
return sharedInstance; |
|||
}</lang> |
|||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |