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 { // this part of code can only be executed by one thread a time
if(me is null) { // Double Checked Lock
synchronized // somethis delaypart soof thatcode othercan threadsonly hasbe chanceexecuted toby callone thisthread synchronizeda codetime
forif(intme iis = 0 ; i < 6 ; i++null) {
writefln("...callingme Dealer...= ")new Dealer ;
msleep(rand() & 2047) ;
}
me = new Dealer ;
}
}
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... ") ;
msleep(rand() & 2047) ;
}
writefln(">>Dealer is called to come in!") ;
state = str.length - 1 ;
}
Dealer nextState() {
synchronized(this) {// accessed to Object _this_ is locked ... is it necessary ???
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() & 20471023) ;
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.
...calling Calling Dealer...
>>Paul come in.
...calling Calling Dealer...
...calling Dealer...
...calling Dealer...
...calling Dealer...
>>Dealer is called to come in!
PaulMary got (1)Enjoy
Peter got (2)Rosetta
Mary got (3)Code
Paul got (1)Enjoy
MaryPeter got (2)Rosetta
Paul got (3)Code
Peter Paul got (1)Enjoy
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}}==