Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ronascentes/c26b15fc73d4b144aa53fe21c3a07448 to your computer and use it in GitHub Desktop.
Save ronascentes/c26b15fc73d4b144aa53fe21c3a07448 to your computer and use it in GitHub Desktop.
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."
}
}
class NewBrain : Command {
execute() {
([Command]$this).execute() # Calls the execute method of the base class (Command)
"You might even need a new brain." | Out-Host # Outputs "You might even need a new brain."
}
}
class Afford : Command {
execute() {
([Command]$this).execute() # Calls the execute method of the base class (Command)
"I couldn't afford a whole new brain." | Out-Host # Outputs "I couldn't afford a whole new brain."
}
}
class Macro {
hidden [Command[]]$commands = @() # Defines an array property to store commands
add([Command]$command) {
$this.commands += $command # Adds a command to the commands array
}
run() {
$this.commands | ForEach-Object { $_.execute() } # Executes each command in the commands array
}
}
$macro = [Macro]::new() # Creates a new instance of the Macro class
$macro.add([Loony]::new()) # Adds a new instance of the Loony class to the macro
$macro.add([NewBrain]::new()) # Adds a new instance of the NewBrain class to the macro
$macro.add([Afford]::new()) # Adds a new instance of the Afford class to the macro
$macro.run() # Runs the macro, executing all the commands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment