Created
June 28, 2024 13:50
-
-
Save dfinke/2c3d35d6072496b5956aa64fd64407e0 to your computer and use it in GitHub Desktop.
PowerShell Singleton Pattern: Ensure single instance for shared resource management
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
# Singleton | |
class Product { | |
$Name | |
Product($name) { | |
$this.Name = $name | |
} | |
} | |
class Creator { | |
static [Product]$product | |
static [Product] GetProduct() { | |
if ($null -eq [Creator]::product) { | |
[Creator]::product = [Product]::new((Get-Date)) | |
} | |
return [Creator]::product | |
} | |
} | |
$p = [Creator]::GetProduct() | |
$p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your Design Patterns serie using PowerShell is amazing. Thanks for sharing.