Revisions
-
dfinke created this gist
May 10, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ <# Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. The State pattern puts each branch of the conditional in a separate class. This lets you treat the object's state as an object in its own right that can vary independently from other objects #> class CeilingFanState { Change([CeilingFan] $ceilingFan) { throw "not implemented" } } class CeilingFanOff : CeilingFanState { Change([CeilingFan] $context) { $context.State = [CeilingFanLow]::new() "Change state from Off to Low." | Out-Host } } class CeilingFanLow: CeilingFanState { Change([CeilingFan] $context) { $context.State = [CeilingFanMedium]::new() "Change state from Low to Medium." | Out-Host } } class CeilingFanMedium: CeilingFanState { Change([CeilingFan] $context) { $context.State = [CeilingFanHigh]::new() "Change state from Medium to High." | Out-Host } } class CeilingFanHigh: CeilingFanState { Change([CeilingFan] $context) { $context.State = [CeilingFanLow]::new() "Change state from High to Off." | Out-Host } } class CeilingFan { $State CeilingFan ([CeilingFanState] $state) { $this.State = $state } Pull() { $this.State.Change($this) } } $ceilingFan = [CeilingFan]::new([CeilingFanOff]::new()) $ceilingFan.Pull() $ceilingFan.Pull() $ceilingFan.Pull() $ceilingFan.Pull()