Created
June 11, 2024 10:55
-
-
Save Caballerog/4d897515e07eefb217643f9839bb1866 to your computer and use it in GitHub Desktop.
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 characters
| import { Component } from "./component"; | |
| export class Composite implements Component { | |
| private children: Component[] = []; | |
| // Add a child to the list of children | |
| addChild(child: Component): void { | |
| this.children.push(child); | |
| } | |
| // Remove a child from the list of children | |
| removeChild(child: Component): void { | |
| const index = this.children.indexOf(child); | |
| if (index !== -1) { | |
| this.children.splice(index, 1); | |
| } | |
| } | |
| // Implement the operation defined in the Component interface | |
| operation(): void { | |
| console.log("Composite operation."); | |
| this.children.forEach(child => child.operation()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment