loader

Ever looked at your company’s org chart and thought, “Wow, it’s like one big family where everyone plays a role”? That’s exactly what the Composite Pattern is all about. It lets you treat individual employees and entire teams the same way, so your code can handle both a lone contributor and a full department without missing a beat.

Why Bother with It?

  • Uniform Treatment: Whether you’re dealing with a single employee or a whole team, you can interact with them using the same methods. No need to write separate code for lone wolves and managers with their squads.
  • Simplified Hierarchy: Organize your objects into a tree-like structure—just like an org chart. This makes complex relationships easier to manage and navigate.
  • Flexibility: Need to add a new team or restructure your company? With Composite, you can add, remove, or modify parts of the hierarchy without rewriting everything.

How It Works (In Plain English)

  1. The Common Interface: Define an interface (or abstract class) that all employees follow. This includes both individual contributors and managers.
  2. Leaf Nodes: These are your individual employees—simple objects with no subordinates.
  3. Composite Nodes: These represent managers or teams. They not only follow the common interface but also hold a collection of other employees (which could be either individuals or other managers).
  4. Uniform Operations: When you perform an operation (like displaying details), the composite (manager) will do its thing and then delegate the same action to each of its subordinates. Everyone gets a turn on stage, just like in your company’s org chart.

C# Code Example

C#
using System;
using System.Collections.Generic;

// Common interface for all employees
public interface IEmployee
{
    void DisplayDetails();
}

// Leaf: Represents an individual employee
public class Employee : IEmployee
{
    private string _name;
    private string _position;
    
    public Employee(string name, string position)
    {
        _name = name;
        _position = position;
    }
    
    public void DisplayDetails()
    {
        Console.WriteLine($"{_position}: {_name}");
    }
}

// Composite: Represents a manager or a team
public class Manager : IEmployee
{
    private string _name;
    private string _position;
    private List<IEmployee> _subordinates = new List<IEmployee>();
    
    public Manager(string name, string position)
    {
        _name = name;
        _position = position;
    }
    
    public void Add(IEmployee employee)
    {
        _subordinates.Add(employee);
    }
    
    public void Remove(IEmployee employee)
    {
        _subordinates.Remove(employee);
    }
    
    public void DisplayDetails()
    {
        Console.WriteLine($"{_position}: {_name}");
        foreach (var subordinate in _subordinates)
        {
            // Indent subordinate details for clarity
            Console.Write("   ");
            subordinate.DisplayDetails();
        }
    }
}

// Client Code
public class Program
{
    public static void Main()
    {
        // Create individual employees
        IEmployee emp1 = new Employee("Alice", "Developer");
        IEmployee emp2 = new Employee("Bob", "Designer");
        IEmployee emp3 = new Employee("Charlie", "QA Engineer");
        
        // Create a manager and add employees to their team
        Manager manager1 = new Manager("Diana", "Team Lead");
        manager1.Add(emp1);
        manager1.Add(emp2);
        
        // Create a higher-level manager and add subordinate teams
        Manager generalManager = new Manager("Edward", "General Manager");
        generalManager.Add(manager1);
        generalManager.Add(emp3);
        
        // Display the full organizational hierarchy
        generalManager.DisplayDetails();
    }
}

The Takeaway

The Composite Pattern is like your company’s org chart: whether you’re an individual contributor or a whole team led by a manager, everyone is treated uniformly. This pattern makes your code more flexible, easier to manage, and perfectly organized—just like a well-structured company. So, next time you need to handle a hierarchy of objects, think of your company’s org chart and let Composite work its magic!

Happy coding, and may your team always work in harmony!