Last active
July 3, 2024 17:29
-
-
Save dfinke/570e972a7297be0fd394be8386b87c2c to your computer and use it in GitHub Desktop.
Composite Pattern: Simplifies client code by treating individual objects and compositions uniformly, making it easier to work with complex structures
This file contains 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 characters
class Employee { | |
$name | |
$dept | |
$salary | |
hidden [Employee[]]$subordinates = @() | |
Employee($name, $dept, $salary) { | |
$this.name = $name | |
$this.dept = $dept | |
$this.salary = $salary | |
} | |
Add([Employee]$e) { | |
$this.subordinates += $e | |
} | |
[Employee[]] GetSubordinates() { | |
return $this.subordinates | |
} | |
[string] ToString() { | |
return "[{0}] {1}: {2:C0}" -f $this.dept, $this.name, $this.salary | |
} | |
} |
This file contains 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 characters
. .\Employee.ps1 | |
$CEO = [Employee]::new("John","CEO", 30000) | |
$HeadSales = [Employee]::new("Robert","Head Sales", 20000) | |
$HeadMarketing = [Employee]::new("Michel","Head Marketing", 20000) | |
$clerk1 = [Employee]::new("Laura","Marketing", 10000) | |
$clerk2 = [Employee]::new("Bob","Marketing", 10000) | |
$salesExecutive1 = [Employee]::new("Richard","Sales", 10000) | |
$salesExecutive2 = [Employee]::new("Rob","Sales", 10000) | |
$CEO.Add($HeadSales) | |
$CEO.Add($HeadMarketing) | |
$HeadSales.Add($salesExecutive1) | |
$HeadSales.Add($salesExecutive2) | |
$HeadMarketing.Add($clerk1) | |
$HeadMarketing.Add($clerk2) | |
"$CEO" | |
foreach($headEmployee in $CEO.GetSubordinates()) { | |
"`t$headEmployee" | |
foreach ($employee in $headEmployee.GetSubordinates()) { | |
"`t`t$employee" | |
} | |
} |
Author
dfinke
commented
May 16, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment