The Composite Pattern is a structural design pattern that enables you to compose
objects into tree structures to represent part-whole hierarchies. This pattern
simplifies the management of complex object structures by allowing clients to
treat individual objects and compositions of objects uniformly. It is
particularly useful for scenarios involving recursive or hierarchical data.
typeComponent={operation():void;add(component: Component):void;getComposite():Component|undefined;};classLeafimplementsComponent{operation():void{console.log("Leaf operation");}add(_component: Component):void{thrownewError("Cannot add to a leaf");}getComposite():Component|undefined{returnundefined;}}classCompositeimplementsComponent{privatereadonlylist: Component[]=[];operation():void{console.log("Composite operation:");for(constcomponentofthis.list){component.operation();// Delegate to children
}}add(component: Component):void{this.list.push(component);}getComposite():Component{returnthis;}}constclient={run():void{constcomposite=newComposite();constleaf=newLeaf();composite.add(leaf);composite.operation();constcomposite2=newComposite();composite.add(composite2);composite.operation();constcomposite3=newComposite();constleaf2=newLeaf();constleaf3=newLeaf();composite3.add(leaf2);composite3.add(leaf3);composite.add(composite3);// Add a more complex composite
composite.operation();}};client.run();