Skip to content

Instantly share code, notes, and snippets.

@ronascentes
Forked from dfinke/StartComputer.ps1
Created July 30, 2024 13:53
Show Gist options
  • Save ronascentes/c4a8033c959e4b5311689c9cdc2f40b8 to your computer and use it in GitHub Desktop.
Save ronascentes/c4a8033c959e4b5311689c9cdc2f40b8 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