// Component interface interface Component { public function operation(); } // Leaf component class Leaf implements Component { public function operation() { echo "Leaf operation\n"; } } // Composite component class Composite implements Component { private $children = []; public function add(Component $component) { $this->children[] = $component; } public function operation() { echo "Composite operation\n"; foreach ($this->children as $child) { $child->operation(); } } } // Usage $leaf1 = new Leaf(); $leaf2 = new Leaf(); $composite1 = new Composite(); $composite1->add($leaf1); $composite1->add($leaf2); $leaf3 = new Leaf(); $composite2 = new Composite(); $composite2->add($composite1); $composite2->add($leaf3); $composite2->operation();using System; using System.Collections.Generic; // Component interface public interface IShape { void Draw(); } // Leaf (individual) object public class Circle : IShape { public void Draw() { Console.WriteLine("Drawing a circle."); } } // Leaf (individual) object public class Square : IShape { public void Draw() { Console.WriteLine("Drawing a square."); } } // Composite (group) object public class Drawing : IShape { private readonly List<IShape> shapes = new List<IShape>(); public void AddShape(IShape shape) { shapes.Add(shape); } public void RemoveShape(IShape shape) { shapes.Remove(shape); } public void Draw() { Console.WriteLine("Drawing the drawing:"); foreach (var shape in shapes) { shape.Draw(); } } } class Program { static void Main() { // Client code IShape circle = new Circle(); IShape square = new Square(); Drawing drawing = new Drawing(); drawing.AddShape(circle); drawing.AddShape(square); IShape anotherCircle = new Circle(); drawing.AddShape(anotherCircle); drawing.Draw(); /* * Sample Output: * Drawing the drawing: * Drawing a circle. * Drawing a square. * Drawing a circle. */ } }3. Composite: The Composite pattern lets you compose objects into tree structures to represent part-whole hierarchies. It allows clients to treat individual objects and compositions of objects uniformly. Example in ReactJS: Suppose you're building a hierarchical navigation menu. // MenuItem.js const MenuItem = ({ title }) => <div>{title}</div>; // Menu.js const Menu = ({ items }) => ( <ul> {items.map((item, index) => ( <li key={index}> {item} </li> ))} </ul> ); // Usage const App = () => { const menuItems = [ <MenuItem title="Home" />, <MenuItem title="About" />, <MenuItem title="Services" />, ]; return ( <div> <h1>Menu</h1> <Menu items={menuItems} /> </div> ); }; import java.util.List; import java.util.ArrayList; /** "Component" */ interface Graphic { //Prints the graphic. public void print(); } /** "Composite" */ class CompositeGraphic implements Graphic { //Collection of child graphics. private final List<Graphic> childGraphics = new ArrayList<>(); //Adds the graphic to the composition. public void add(Graphic graphic) { childGraphics.add(graphic); } //Prints the graphic. @Override public void print() { for (Graphic graphic : childGraphics) { graphic.print(); //Delegation } } } /** "Leaf" */ class Ellipse implements Graphic { //Prints the graphic. @Override public void print() { System.out.println("Ellipse"); } } /** Client */ class CompositeDemo { public static void main(String[] args) { //Initialize four ellipses Ellipse ellipse1 = new Ellipse(); Ellipse ellipse2 = new Ellipse(); Ellipse ellipse3 = new Ellipse(); Ellipse ellipse4 = new Ellipse(); //Creates two composites containing the ellipses CompositeGraphic compositGraphic2 = new CompositeGraphic(); compositGraphic2.add(ellipse1); compositGraphic2.add(ellipse2); compositGraphic2.add(ellipse3); CompositeGraphic compositGraphic3 = new CompositeGraphic(); compositGraphic3.add(ellipse4); //Create another graphics that contains two graphics CompositeGraphic compositGraphic = new CompositeGraphic(); compositGraphic.add(compositGraphic2); compositGraphic.add(compositGraphic3); //Prints the complete graphic (Four times the string "Ellipse"). compositGraphic.print(); } }