Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Component {
- protected parent: Component | null = null;
- public setParent(parent: Component | null) {
- this.parent = parent;
- }
- public getParent(): Component | null {
- return this.parent;
- }
- public add(component: Component): void { }
- public remove(component: Component): void { }
- public isComposite(): boolean {
- return false;
- }
- public abstract operation(): string;
- }
- class Leaf extends Component {
- public operation(): string {
- return 'Лист';
- }
- }
- class Composite extends Component {
- protected children: Component[] = [];
- public add(component: Component): void {
- this.children.push(component);
- component.setParent(this);
- }
- public remove(component: Component): void {
- const componentIndex = this.children.indexOf(component);
- this.children.splice(componentIndex, 1);
- component.setParent(null);
- }
- public isComposite(): boolean {
- return true;
- }
- public operation(): string {
- const results = [];
- for (const child of this.children) {
- results.push(child.operation());
- }
- return `Ветка(${results.join('+')})`;
- }
- }
- const tree = new Composite();
- const branch1 = new Composite();
- branch1.add(new Leaf());
- branch1.add(new Leaf());
- const branch2 = new Composite();
- branch2.add(new Leaf());
- tree.add(branch1);
- tree.add(branch2);
- console.log(tree.operation());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement