Skip to content

Instantly share code, notes, and snippets.

View dfinke's full-sized avatar

Doug Finke dfinke

View GitHub Profile
@dfinke
dfinke / Singleton.ps1
Created June 28, 2024 13:50
PowerShell Singleton Pattern: Ensure single instance for shared resource management
# Singleton
class Product {
$Name
Product($name) {
$this.Name = $name
}
}

PowerShell Issues FAQ

General

Q: How frequently are issues in the PowerShell repository reviewed? A: The frequency of issue reviews can vary; however, issues are often reviewed when they are newly created. Regular review cycles might also be in place depending on the projectΓÇÖs maintenance policy.

Q: How can I determine if a cmdlet/module is maintained in the PowerShell repository? A: You can use the repository's search function, issue templates, and officially provided documentation to determine if a cmdlet/module is maintained in the powershell/powershell repository.

@dfinke
dfinke / ChatMediator.ps1
Created June 21, 2024 14:06
Demonstrates the implementation of the Mediator Design Pattern in PowerShell
<#
Define an object that encapsulates how a set of objects interact.
Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
#>
class ChatMediator {
$users
ChatMediator() {
$this.users = New-Object System.Collections.ArrayList
@dfinke
dfinke / StartComputer.ps1
Last active June 15, 2024 13:12
PowerShell example demonstrating the Facade Design Pattern implementation
class CPU {
freeze() { "$this freezing" | Out-Host }
jump($position) { "$this jump to $position" | Out-Host }
execute() { "$this execute" | Out-Host }
}
class Memory {
load($position, $data) {
"$this load $position $data" | Out-Host
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
}, {
"category": "fiction",
@dfinke
dfinke / FinanceReport.ps1
Last active July 3, 2024 17:24
PowerShell script using the Template Method pattern to generate financial reports in plain text and HTML formats. Extensible and flexible.
class ReportTemplate {
hidden $data
GenerateReport() {
$this.RetrieveFinancialData()
$this.FormatReport()
$this.SendToStakeholders()
}
RetrieveFinancialData() {
class Command {
execute() {
"[$(get-date)] Logging execute of command [$this]" | Out-Host # Logs the execution of the command
}
}
class Loony : Command {
execute() {
([Command]$this).execute() # Calls the execute method of the base class (Command)
"You're a loony." | Out-Host # Outputs "You're a loony."
@dfinke
dfinke / Logger-DecoratorPattern.ps1
Last active July 3, 2024 17:27
PowerShell Decorator Pattern: Enhance Logger with Timestamp and Uppercase
class Logger {
log($message) { # Define a method called "log" that takes a message as input
$message | Out-Host # Output the message to the console
}
}
class TimeStampingLogger : Logger { # Define a class called "TimeStampingLogger" that inherits from "Logger"
$logger # Declare a variable called "logger"
TimeStampingLogger($logger) { # Define a constructor that takes a "logger" as input
@dfinke
dfinke / ClockObserverPattern.ps1
Last active July 3, 2024 17:28
PowerShell implementation of the Observer Pattern with a Clock Timer example, featuring Digital and Analog clocks.
class Subject {
hidden [System.Collections.ArrayList]$observers
Subject() {
$this.observers = New-Object System.Collections.ArrayList
}
Attach([Observer]$o) { $this.observers.Add($o) }
@dfinke
dfinke / Try-CompositePattern.ps1
Last active July 3, 2024 17:29
Composite Pattern: Simplifies client code by treating individual objects and compositions uniformly, making it easier to work with complex structures
. .\Employee.ps1
$CEO = [Employee]::new("John","CEO", 30000)
$HeadSales = [Employee]::new("Robert","Head Sales", 20000)
$HeadMarketing = [Employee]::new("Michel","Head Marketing", 20000)
$clerk1 = [Employee]::new("Laura","Marketing", 10000)
$clerk2 = [Employee]::new("Bob","Marketing", 10000)
$salesExecutive1 = [Employee]::new("Richard","Sales", 10000)
$salesExecutive2 = [Employee]::new("Rob","Sales", 10000)