Composite
Context: Compose objects into tree structures to represent part‑whole hierarchies. Composite lets clients treat individual objects and compositions uniformly.
public abstract class Component{ public abstract void Operation();}
public class Leaf : Component{ public override void Operation() => Console.WriteLine("Leaf");}
public class Composite : Component{ private List<Component> _children = new(); public void Add(Component c) => _children.Add(c); public override void Operation() { foreach (var child in _children) child.Operation(); }}Real-world usage example
Section titled “Real-world usage example”File system: Files (leaf) and directories (composite) both implement IFileSystemNode. Operations like GetSize() or Delete() work on both.
Example: In UI frameworks, Control can be a single button or a panel containing other controls. ASP.NET Core’s ICompositeMetadataDetailsProvider uses composite pattern.