Singleton: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 199:
retrieved = PlayFetchWithDog( 3.1);
...</lang>
 
=={{header|C sharp|C#}}==
===First attempt at thread-safety using locking.===
Performance suffers because the lock is acquired every time Instance is accessed.<br />
This implementation is extremely slow and should not be used (but is seen often).
<lang csharp>public sealed class Singleton1 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: Yes
{
private static Singleton1 instance;
private static readonly object lockObj = new object();
public static Singleton1 Instance {
get {
lock(lockObj) {
if (instance == null) {
instance = new Singleton1();
}
}
return instance;
}
}
}</lang>
 
===Fixes excessive locking by double-checking for null.===
Still uses locking and implementation is ugly and verbose.
<lang csharp>public sealed class Singleton2 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: Yes, but only once
{
private static Singleton2 instance;
private static readonly object lockObj = new object();
 
public static Singleton2 Instance {
get {
if (instance == null) {
lock(lockObj) {
if (instance == null) {
instance = new Singleton2();
}
}
}
return instance;
}
}
}</lang>
 
===Really simple implementation without locking.===
It still is not completely lazy. If there are other static members, accessing any of those will still cause initialization.
<lang csharp>public sealed class Singleton3 //Lazy: Yes, but not completely ||| Thread-safe: Yes ||| Uses locking: No
{
private static Singleton3 Instance { get; } = new Singleton3();
static Singleton3() { }
}</lang>
 
===Truly lazy by using an inner class.===
This version is completely lazy but the code looks more complicated than it needs to be.
<lang csharp>public sealed class Singleton4 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: No
{
public static Singleton4 Instance => SingletonHolder.instance;
private class SingletonHolder
{
static SingletonHolder() { }
internal static readonly Singleton4 instance = new Singleton4();
}
}</lang>
 
===Using Lazy<T>===
C# has a dedicated type for lazy initialization: Lazy<T>.<br />
It makes implementing a Singleton really easy. Recommended.
<lang csharp>public sealed class Singleton5 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: No
{
private static readonly Lazy<Singleton5> lazy = new Lazy<Singleton5>(() => new Singleton5());
public static Singleton5 Instance => lazy.Value;
}</lang>
 
=={{header|C++}}==
Line 304 ⟶ 379:
}
</lang>
 
=={{header|C sharp|C#}}==
===First attempt at thread-safety using locking.===
Performance suffers because the lock is acquired every time Instance is accessed.<br />
This implementation is extremely slow and should not be used (but is seen often).
<lang csharp>public sealed class Singleton1 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: Yes
{
private static Singleton1 instance;
private static readonly object lockObj = new object();
public static Singleton1 Instance {
get {
lock(lockObj) {
if (instance == null) {
instance = new Singleton1();
}
}
return instance;
}
}
}</lang>
 
===Fixes excessive locking by double-checking for null.===
Still uses locking and implementation is ugly and verbose.
<lang csharp>public sealed class Singleton2 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: Yes, but only once
{
private static Singleton2 instance;
private static readonly object lockObj = new object();
 
public static Singleton2 Instance {
get {
if (instance == null) {
lock(lockObj) {
if (instance == null) {
instance = new Singleton2();
}
}
}
return instance;
}
}
}</lang>
 
===Really simple implementation without locking.===
It still is not completely lazy. If there are other static members, accessing any of those will still cause initialization.
<lang csharp>public sealed class Singleton3 //Lazy: Yes, but not completely ||| Thread-safe: Yes ||| Uses locking: No
{
private static Singleton3 Instance { get; } = new Singleton3();
static Singleton3() { }
}</lang>
 
===Truly lazy by using an inner class.===
This version is completely lazy but the code looks more complicated than it needs to be.
<lang csharp>public sealed class Singleton4 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: No
{
public static Singleton4 Instance => SingletonHolder.instance;
private class SingletonHolder
{
static SingletonHolder() { }
internal static readonly Singleton4 instance = new Singleton4();
}
}</lang>
 
===Using Lazy<T>===
C# has a dedicated type for lazy initialization: Lazy<T>.<br />
It makes implementing a Singleton really easy. Recommended.
<lang csharp>public sealed class Singleton5 //Lazy: Yes ||| Thread-safe: Yes ||| Uses locking: No
{
private static readonly Lazy<Singleton5> lazy = new Lazy<Singleton5>(() => new Singleton5());
public static Singleton5 Instance => lazy.Value;
}</lang>
 
=={{header|Caché ObjectScript}}==
Line 714:
 
s := (create{SINGLETON_ACCESS}).singleton -- in some routine</lang>
 
=={{header|Elena}}==
Stateless singleton
Line 1,503 ⟶ 1,504:
 
In other words, here the class object serves as the singleton object. The "singleton class" is the metaclass of the class. The downside of this approach is that the "singleton class" (the metaclass of the class) cannot be made to explicitly inherit from a class of the user's choice, or implement a protocol of the user's choice. Also, there is no way to prevent subclasses of the class from being made, thus effectively creating "multiple instances" of the singleton class. Also, one cannot declare properties on the singleton (the class object).
 
 
=={{header|Oforth}}==
Line 1,638:
my $s2 = Singleton->new;
printf "name: %s, ref: %s\n", $s2->name, $s2;</lang>
 
=={{header|Perl 6}}==
<lang perl6>class Singleton {
# We create a lexical variable in the class block that holds our single instance.
my Singleton $instance = Singleton.bless; # You can add initialization arguments here.
method new {!!!} # Singleton.new dies.
method instance { $instance; }
}</lang>
 
=={{header|Phix}}==
Line 1,728 ⟶ 1,720:
This is method 2 on +Singleton
-> +Singleton</pre>
 
=={{header|PureBasic}}==
===Native version===
Thread safe version.
<lang PureBasic>Global SingletonSemaphore=CreateSemaphore(1)
 
Interface OO_Interface ; Interface for any value of this type
Get.i()
Set(Value.i)
Destroy()
EndInterface
 
Structure OO_Structure ; The *VTable structure
Get.i
Set.i
Destroy.i
EndStructure
 
Structure OO_Var
*VirtualTable.OO_Structure
Value.i
EndStructure
 
Procedure OO_Get(*Self.OO_Var)
ProcedureReturn *Self\Value
EndProcedure
 
Procedure OO_Set(*Self.OO_Var, n)
*Self\Value = n
EndProcedure
 
Procedure CreateSingleton()
If TrySemaphore(SingletonSemaphore)
*p.OO_Var = AllocateMemory(SizeOf(OO_Var))
If *p
*p\VirtualTable = ?VTable
EndIf
EndIf
ProcedureReturn *p
EndProcedure
 
Procedure OO_Destroy(*Self.OO_Var)
FreeMemory(*Self)
SignalSemaphore(SingletonSemaphore)
EndProcedure
 
DataSection
VTable:
Data.i @OO_Get()
Data.i @OO_Set()
Data.i @OO_Destroy()
EndDataSection</lang>
===Simple OOP extension===
Using the open-source precompiler [http://www.development-lounge.de/viewtopic.php?t=5915 SimpleOOP].
<lang PureBasic>Singleton Class Demo
BeginPrivate
Name$
X.i
EndPrivate
Public Method Init(Name$)
This\Name$ = Name$
EndMethod
Public Method GetX()
MethodReturn This\X
EndMethod
Public Method SetX(n)
This\X = n
EndMethod
Public Method Hello()
MessageRequester("Hello!", "I'm "+This\Name$)
EndMethod
EndClass</lang>
 
=={{header|Python}}==
Line 1,823 ⟶ 1,892:
pass
</lang>
 
=={{header|PureBasic}}==
===Native version===
Thread safe version.
<lang PureBasic>Global SingletonSemaphore=CreateSemaphore(1)
 
Interface OO_Interface ; Interface for any value of this type
Get.i()
Set(Value.i)
Destroy()
EndInterface
 
Structure OO_Structure ; The *VTable structure
Get.i
Set.i
Destroy.i
EndStructure
 
Structure OO_Var
*VirtualTable.OO_Structure
Value.i
EndStructure
 
Procedure OO_Get(*Self.OO_Var)
ProcedureReturn *Self\Value
EndProcedure
 
Procedure OO_Set(*Self.OO_Var, n)
*Self\Value = n
EndProcedure
 
Procedure CreateSingleton()
If TrySemaphore(SingletonSemaphore)
*p.OO_Var = AllocateMemory(SizeOf(OO_Var))
If *p
*p\VirtualTable = ?VTable
EndIf
EndIf
ProcedureReturn *p
EndProcedure
 
Procedure OO_Destroy(*Self.OO_Var)
FreeMemory(*Self)
SignalSemaphore(SingletonSemaphore)
EndProcedure
 
DataSection
VTable:
Data.i @OO_Get()
Data.i @OO_Set()
Data.i @OO_Destroy()
EndDataSection</lang>
===Simple OOP extension===
Using the open-source precompiler [http://www.development-lounge.de/viewtopic.php?t=5915 SimpleOOP].
<lang PureBasic>Singleton Class Demo
BeginPrivate
Name$
X.i
EndPrivate
Public Method Init(Name$)
This\Name$ = Name$
EndMethod
Public Method GetX()
MethodReturn This\X
EndMethod
Public Method SetX(n)
This\X = n
EndMethod
Public Method Hello()
MessageRequester("Hello!", "I'm "+This\Name$)
EndMethod
EndClass</lang>
 
=={{header|Racket}}==
Line 1,922 ⟶ 1,914:
(super-new))))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>class Singleton {
# We create a lexical variable in the class block that holds our single instance.
my Singleton $instance = Singleton.bless; # You can add initialization arguments here.
method new {!!!} # Singleton.new dies.
method instance { $instance; }
}</lang>
 
=={{header|Ruby}}==
10,333

edits