⚙️ POWERSHELL RUNE DIFFICULTY: Adept Behavioral

The State of the Service Lifecycle

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Sysadmin Telepathy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// POWERSHELL PATTERN //
class IServiceState {
    [void] HandleRequest([ServiceContext]$ctx) { throw "Not Implemented" }
}

class ServiceContext {
    [IServiceState]$State
    ServiceContext([IServiceState]$s) { $this.State = $s }
    [void] Request() { $this.State.HandleRequest($this) }
}

class StoppedState : IServiceState {
    [void] HandleRequest([ServiceContext]$ctx) {
        Write-Host "Service is stopped. Initiating startup sequence..." -ForegroundColor Yellow
        $ctx.State = [RunningState]::new()
    }
}

class RunningState : IServiceState {
    [void] HandleRequest([ServiceContext]$ctx) {
        Write-Host "Service is running perfectly. Emitting pulse." -ForegroundColor Green
    }
}

# Cycle through states
$context = [ServiceContext]::new([StoppedState]::new())
$context.Request() # Transitions to Running
$context.Request() # Remains Running

By utilizing the State pattern, a script acts dynamically based on the current essence of a service. It fluidly mutates its logic, transitioning between Stopped and Running forms effortlessly.

Runic Tags: #powershell #sysadmin #behavioral #state #service-management