Skip to content

Instantly share code, notes, and snippets.

@dfinke
Last active June 15, 2024 13:12
Show Gist options
  • Save dfinke/c76bbc6b90b537f03507a1e6b9ecebb4 to your computer and use it in GitHub Desktop.
Save dfinke/c76bbc6b90b537f03507a1e6b9ecebb4 to your computer and use it in GitHub Desktop.
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
}
}
class HardDrive {
[object] read($lba, $size) {
"$this read $lba $size" | Out-Host
return "(some data)"
}
}
# Façade
class Computer {
hidden [CPU]$cpu
hidden [Memory]$memory
hidden [HardDrive]$hardDrive
Computer() {
$this.cpu = New-Object CPU
$this.memory = New-Object Memory
$this.hardDrive = New-Object HardDrive
}
StartComputer() {
$this.cpu.freeze()
$this.memory.load(0, $this.hardDrive.read(0, 1024))
$this.cpu.jump(10)
$this.cpu.execute()
}
}
$façade = [Computer]::new()
$façade.StartComputer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment