The Factory Method of Log Endpoints
Define an interface for creating a logging endpoint, but let subclasses decide which log to instantiate.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Factory Method
ARCANUM SCHOOL: Conjuration // Sysadmin Telepathy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// POWERSHELL PATTERN //
class ILogger {
[void] Write([string]$message) { throw "Not Implemented" }
}
class EventViewerLogger : ILogger {
[void] Write([string]$message) {
Write-Host "Writing to Event Viewer: $message" -ForegroundColor Cyan
}
}
class FileLogger : ILogger {
[void] Write([string]$message) {
Write-Host "Appending to Flat File: $message" -ForegroundColor Yellow
}
}
class LogCreator {
[ILogger] CreateLogger() { throw "Not Implemented" }
[void] LogEvent([string]$msg) {
$logger = $this.CreateLogger()
$logger.Write($msg)
}
}
class EventLogCreator : LogCreator {
[ILogger] CreateLogger() { return [EventViewerLogger]::new() }
}
class FileLogCreator : LogCreator {
[ILogger] CreateLogger() { return [FileLogger]::new() }
}
# Channeling the sysadmin telemetry
$creator = [EventLogCreator]::new()
$creator.LogEvent("System breach attempt detected in sector 7G.")
By utilizing the Factory Method, a sysadmin dynamically summons the appropriate telemetry endpoint without binding the central nervous system of their script to concrete logging structures.
Runic Tags:
#powershell
#sysadmin
#creational
#factory-method
#logging