Anonymous user
Singleton: Difference between revisions
m
(D) header reorder & double checked lock
m ((D) header reorder & double checked lock) |
|||
Line 207:
</pre>
=={{header|E}}==▼
Since E uses closure-style objects rather than classes, a singleton is simply an object which is defined at the top level of the program, not inside any method. There are no thread-safety issues since the singleton, like every other object, belongs to some particular [http://www.erights.org/elib/concurrency/vat.html vat] (but can be remotely invoked from other vats).▼
def aSingleton {▼
# ...▼
}▼
=={{header|Objective-C}}==▼
===Non-Thread-Safe===▼
(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▼
+ (SomeSingleton*) sharedInstance▼
{▼
static sharedInstance = nil;▼
if(!sharedInstance) {▼
sharedInstance = [[SomeSingleton alloc] init];▼
}▼
return sharedInstance;▼
}▼
@end▼
</pre>▼
=={{header|D}}==
<d>module singleton ;
Line 254 ⟶ 215:
class Dealer {
private static Dealer me ;
static Dealer Instance() {
writefln(" Calling Dealer... ") ;
synchronized //
msleep(rand() & 2047) ;▼
}▼
return me ;
}
private static string[] str = ["(1)Enjoy", "(2)Rosetta", "(3)Code"] ;
private int state ;
private this() {
for(int i = 0 ; i < 3 ; i++) {
writefln("...calling Dealer... ") ;▼
writefln(">>Dealer is called to come in!") ;
state = str.length - 1 ;
}
Dealer nextState() {
synchronized(this)
state = (state + 1) % str.length ;
return this ;
}
string toString() { return str[state] ; }
}
class Coder : Thread {
private string name_ ;
Coder hasName(string name) { name_ = name ; return this ; }
override int run() {
msleep(rand() &
writefln(">>%s come in.", name_) ;
Dealer single = Dealer.Instance ;
msleep(rand() & 1023) ;
for(int i = 0 ; i < 3 ; i++) {
writefln("%9s got %-s", name_, single.nextState) ;
Line 296 ⟶ 257:
}
}
void main() {
Coder x = new Coder ;
Coder y = new Coder ;
Line 305 ⟶ 267:
z.hasName("Mary").start() ;
x.wait ; y.wait ; z.wait ;
}</d>
Sample Output:
<pre>>>Mary come in.
Calling Dealer...
...calling Dealer...
>>Peter come in.
>>Paul come in.
▲...calling Dealer...
...calling Dealer...
...calling Dealer...
>>Dealer is called to come in!
Peter got (2)Rosetta
Mary got (3)Code
Paul got (1)Enjoy
Paul got (3)Code
Mary got (2)Rosetta
Peter got (3)Code</pre>
▲=={{header|E}}==
▲Since E uses closure-style objects rather than classes, a singleton is simply an object which is defined at the top level of the program, not inside any method. There are no thread-safety issues since the singleton, like every other object, belongs to some particular [http://www.erights.org/elib/concurrency/vat.html vat] (but can be remotely invoked from other vats).
▲ def aSingleton {
▲ # ...
▲ }
▲=={{header|Objective-C}}==
▲===Non-Thread-Safe===
▲(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
▲+ (SomeSingleton*) sharedInstance
▲{
▲ static sharedInstance = nil;
▲ if(!sharedInstance) {
▲ sharedInstance = [[SomeSingleton alloc] init];
▲ }
▲ return sharedInstance;
▲}
▲@end
▲</pre>
=={{header|Java}}==
|