Design Patterns Mastery

Composite Pattern: Managing tree structures & hierarchies

1 Views Updated 5/4/2026

The Composite Pattern

The Composite Pattern allows you to treat individual objects and "Groups of Objects" (Composites) identically. This is the standard pattern for building File Systems (Files vs Folders), Organizational Charts (Employee vs Manager), or UI Component Trees.

1. Scenario: File System Size

If you ask for the size of a File, it returns its bytes. If you ask for the size of a Folder, it internally asks all its children for their sizes and sums them up. To the user, they both look like an IFileSystemItem.

public interface IComponent { void Display(); }

public class Leaf : IComponent { ... } // Individual item

public class Composite : IComponent 
{
    private List<IComponent> _children = new();
    
    public void Display() 
    {
        foreach(var child in _children) child.Display();
    }
}

2. Recursive Logic

The Composite pattern relies heavily on Recursion. A Composite can contain another Composite, allowing for infinitely deep "Tree" structures.

4. Interview Mastery

Q: "Is the Composite pattern a violation of the Interface Segregation Principle (ISP)?"

Architect Answer: "It can be. If you put `Add(child)` and `Remove(child)` methods on the top-level `IComponent` interface, the 'Leaf' node is forced to implement those methods even though it cannot have children. This is a trade-off. You lose **Safety** (because a Leaf might throw an exception if you try to add a child), but you gain **Transparency** (because you can treat every node exactly the same without checking if it is a list or an object). Senior architects usually choose transparency for cleaner client code."

Design Patterns Mastery
1. Introduction to Design Patterns
Introduction to GoF Patterns: Why patterns matter? SOLID Principles: The foundation of all patterns DRY, KISS, and YAGNI (Architectural Philosophy)
2. Creational Patterns
Singleton Pattern: Thread-safety & Captive Dependencies Factory Method: Abstracting complex object creation Abstract Factory: Creating families of related objects Builder Pattern: Constructing complex fluents Prototype Pattern: Cloning high-cost objects
3. Structural Patterns
Adapter Pattern: Bridging incompatible interfaces Bridge Pattern: Decoupling abstraction from implementation Composite Pattern: Managing tree structures & hierarchies Decorator Pattern: Enhancing behavior without inheritance Facade Pattern: Simplifying complex library subsystems Flyweight Pattern: Drastically reducing memory footprint Proxy Pattern: Interception, Lazy-Loading, and Security
4. Behavioral Patterns
Chain of Responsibility: Middleware & Pipeline Architecture Command Pattern: Implementing Undo/Redo & Queueing Interpreter Pattern: Building domain-specific languages Iterator Pattern: Unified traversal of collections Mediator Pattern: Decoupling components with MediatR Memento Pattern: Capturing and restoring object state Observer Pattern: Pub/Sub & Event-driven architecture State Pattern: Managing complex object lifecycles Strategy Pattern: Swappable algorithms at runtime Template Method: Defining skeleton algorithms Visitor Pattern: Separating operations from data structures
5. Modern Enterprise & Cloud Patterns
Repository & Unit of Work (The EF Core Standard) CQRS Pattern (Command Query Responsibility Segregation) Circuit Breaker & Retry Patterns (Resilience with Polly) Dependency Injection Pattern (The Modern King)