The Decorator of Cmdlet Wrappers
Attach additional responsibilities to an object dynamically, providing a flexible alternative to subclassing.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Decorator
ARCANUM SCHOOL: Transmutation // Sysadmin Telepathy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// POWERSHELL PATTERN //
class ITask {
[string] Run() { throw "Not Implemented" }
}
class BaseScriptTask : ITask {
[string] Run() {
return "Executing Base Script"
}
}
class TaskDecorator : ITask {
[ITask]$_wrappee
TaskDecorator([ITask]$task) { $this._wrappee = $task }
[string] Run() { return $this._wrappee.Run() }
}
class LoggingTaskDecorator : TaskDecorator {
LoggingTaskDecorator([ITask]$task) : base($task) {}
[string] Run() {
$result = base.Run()
return "[LOGGED] " + $result
}
}
class TimingTaskDecorator : TaskDecorator {
TimingTaskDecorator([ITask]$task) : base($task) {}
[string] Run() {
$start = Get-Date
$result = base.Run()
$end = Get-Date
return $result + " (Took $($end - $start))"
}
}
# Layering the enchantments
$task = [BaseScriptTask]::new()
$loggedTask = [LoggingTaskDecorator]::new($task)
$timedLoggedTask = [TimingTaskDecorator]::new($loggedTask)
Write-Host $timedLoggedTask.Run()
By means of the Decorator, a mundane PowerShell task can be wrapped in layers of mystical wards—logging, telemetry, timing—without permanently altering its core essence.
Runic Tags:
#powershell
#sysadmin
#structural
#decorator
#wrappers