Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Caballerog/4d897515e07eefb217643f9839bb1866 to your computer and use it in GitHub Desktop.

Select an option

Save Caballerog/4d897515e07eefb217643f9839bb1866 to your computer and use it in GitHub Desktop.
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