Last active
September 16, 2023 02:33
-
-
Save guitarrapc/2fde990d166286459c309b7cab03938b to your computer and use it in GitHub Desktop.
Singleton for PowerShell class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Main() { | |
$item = [Singleton]::Instance | |
$item -eq $null #false | |
$item.SingletonTarget # Singleton object GUID will return | |
$item.SingletonTarget # Same object | |
$item2 = [Singleton]::Instance # same instance | |
$item -eq $item2 # true | |
} | |
# There is no documentation for static field "Thread-safeness" but if it is safe, then this Singleton guaranteed thread-safety. | |
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-5.1#static-attribute | |
# Also PowerShell Class doesn't have readonly or getter only keyword to protect field to be overwrite after instantiate. | |
# Make sure user MUST NOT set [Singleton]::Instance field. | |
class Singleton { | |
hidden static [Singleton] $_instance = [Singleton]::new() | |
static [Singleton] $Instance = [Singleton]::GetInstance() | |
[Guid] $SingletonTarget = [Guid]::NewGuid() | |
hidden Singleton() { | |
} | |
hidden static [Singleton] GetInstance() { | |
return [Singleton]::_instance | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Main() { | |
$item = [Singleton]::Instance | |
$item -eq $null # true | |
$item.SingletonTarget # $null | |
} | |
# PowerShell static field initialize as it's order, therefore this always return $null for "_instance" | |
class Singleton { | |
static [Singleton] $Instance = [Singleton]::GetInstance() | |
hidden static [Singleton] $_instance = [Singleton]::new() | |
[Guid] $SingletonTarget = [Guid]::NewGuid() | |
hidden Singleton() { | |
} | |
hidden static [Singleton] GetInstance() { | |
return [Singleton]::_instance | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Thread-safe and full lazy-ness singleton. | |
void Main() | |
{ | |
Singleton.Instance.target.Dump(); | |
} | |
public class Singleton | |
{ | |
private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton()); | |
public static Singleton Instance { get { return lazy.Value; } } | |
private Singleton() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Thread-safe Singleton, but much less lazyness for all properties/fields. | |
void Main() | |
{ | |
Singleton.Instance.target.Dump(); | |
} | |
class Singleton | |
{ | |
public static Singleton Instance | |
{ | |
get | |
{ | |
return instance; | |
} | |
} | |
private static Singleton instance = new Singleton(); | |
public Guid target = Guid.NewGuid(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment