Skip to content

Instantly share code, notes, and snippets.

@dfinke
Created June 28, 2024 13:50
Show Gist options
  • Save dfinke/2c3d35d6072496b5956aa64fd64407e0 to your computer and use it in GitHub Desktop.
Save dfinke/2c3d35d6072496b5956aa64fd64407e0 to your computer and use it in GitHub Desktop.
PowerShell Singleton Pattern: Ensure single instance for shared resource management
# 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