Open In App

Composite Design Pattern

Last Updated : 26 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Composite Pattern is a structural design pattern that organizes objects into tree structures, enabling clients to treat individual and composite objects uniformly through a common interface.

Let's understand this with the help of diagram:

composite_pattern_
Composite Pattern

What are Part-Whole or Whole-Part Object hierarchies?

Part-Whole (Whole-Part) hierarchies represent complex objects (wholes) composed of simpler objects (parts). This structure allows both individual objects and groups of objects to be treated uniformly, often modeled using the Composite Pattern.

Example: In a graphic design application, you might have shapes as individual elements (like circles and rectangles), and you can combine these shapes to create more complex shapes (like a smiley face with eyes and a mouth). The Composite Pattern lets you work with both simple shapes and complex shapes using the same set of operations, making it easier to manage and manipulate them.

In this context:

  • The parts are individual shapes (like circles, rectangles).
  • The wholes are the complex shapes (like a smiley face composed of circles and rectangles).

Real Life Use of Composite Pattern

The Composite Pattern is useful in various scenarios, such as:

  1. Graphics and GUI Libraries: Building complex graphical structures like shapes and groups.
  2. File Systems: Representing files, directories, and their hierarchical relationships.
  3. Organization Structures: Modeling hierarchical organizational structures like departments, teams and employees.

Advantages of the Composite Design Pattern

  • Hierarchical Structure: Represents objects in tree-like hierarchies, treating individuals and composites uniformly.
  • Simplified Client Code: Clients interact with objects without distinguishing between single or composite ones.
  • Flexibility: Easily add or remove objects without affecting client code.
  • Scalability: Supports nesting of composites for complex structures.
  • Code Reusability: Same operations apply to both parts and wholes, reducing duplication.

Disadvantages of Composite Design Pattern

  • Complex Implementation: Requires a common interface for all objects, making code more intricate.
  • Performance Overhead: Traversing deep hierarchies can slow operations.
  • Limited Type Safety: Common interface may allow invalid operations, risking runtime errors.
  • Reduced Clarity: Code can become harder to understand in complex structures.
  • Extra Memory Usage: Storing child references increases memory consumption.

Potential Pitfalls

While the Composite Pattern is a powerful tool, it's important to be aware of some potential pitfalls:

  1. Complexity: Implementation can be harder due to hierarchy management.
  2. Performance Overhead: Deep or large hierarchies may slow operations.
  3. Type Safety: Risk of invalid operations in weakly typed languages.
  4. Design Complexity: May be overkill if not suited to the problem.
  5. Limited Use Cases: Best only for part-whole hierarchies.

Example for Composite Design Pattern

You are tasked with developing a software component to manage a hierarchical file system structure. The goal is to implement the Composite Pattern to seamlessly work with individual files and directories as part of a unified hierarchy.

Implementing the Composite Pattern for File System Hierarchy

Let's understand the components of the Composite Pattern using file system hierarchy example:

Key Component in the Composite Pattern

  1. Component: The Component is the common interface for all objects in the composition. It defines the methods that are common to both leaf and composite objects.
  2. Leaf: The Leaf is the individual object that does not have any children. It implements the component interface and provides the specific functionality for individual objects.
  3. Composite: The Composite is the container object that can hold Leaf objects as well as the other Composite objects. It implements the Component interface and provides methods for adding, removing and accessing children.
  4. Client: The Client is responsible for using the Component interface to work with objects in the composition. It treats both Leaf and Composite objects uniformly.

Step Wise Implementation of Composite Design Pattern

1. Component

In the file system hierarchy example, the Component is represented by the FileSystemComponenet interface. This interface defines the common interface for both leaf and composite objects. It declares a method, display(), which all classes in the hierarchy must implement.

C++
class FileSystemComponent {
public:
    virtual void display() const = 0;
};
Java
abstract class FileSystemComponent {
    public abstract void display();
}
Python
from abc import ABC, abstractmethod

class FileSystemComponent(ABC):
    @abstractmethod
    def display(self):
        pass
JavaScript
class FileSystemComponent {
    display() {
        throw new Error('Method not implemented.');
    }
}


The Component serves as the foundation for all objects within the hierarchy. Whether it's file or a directory, they all must adhere to this common interface.

2. Leaf

In the context of our file system hierarchy example, Leaf objects are the individual files. These are the objects that do not have any children. Here is an implementation of a leaf object, a file:

C++
class File : public FileSystemComponent {
public:
    File(const std::string& name, int size) : name(name), size(size) {}

    void display() const override {
        std::cout << "File: " << name << " (" << size << " bytes)" << std::endl;
    }
private:
    std::string name;
    int size;
};
Java
public class File extends FileSystemComponent {
    private String name;
    private int size;

    public File(String name, int size) {
        this.name = name;
        this.size = size;
    }

    @Override
    public void display() {
        System.out.println("File: " + name + " (" + size + " bytes)");
    }
}
Python
class File(FileSystemComponent):
    def __init__(self, name, size):
        self.name = name
        self.size = size

    def display(self):
        print(f'File: {self.name} ({self.size} bytes)')
JavaScript
class File extends FileSystemComponent {
    constructor(name, size) {
        super();
        this.name = name;
        this.size = size;
    }

    display() {
        console.log(`File: ${this.name} (${this.size} bytes)`);
    }
}


Here, File is a leaf object. It implements the FileSystemComponent interface by providing a display method. It contains data specific to files, such as their name and size.

3. Composite

In the file system hierarchy example, Composite objects are directories. These are objects that contain other components, including both leaf objects (files) and other composite objects (subdirectories). Here's an implementation of a composite object, a directory:

C++
class Directory : public FileSystemComponent {
public:
    Directory(const std::string& name) : name(name) {}

    void display() const override {
        std::cout << "Directory: " << name << std::endl;
        for (const auto& component : components) {
            component->display();
        }
    }

    void addComponent(FileSystemComponent* component) {
        components.push_back(component);
    }
private:
    std::string name;
    std::vector<FileSystemComponent*> components;
};
Java
import java.util.ArrayList;
import java.util.List;

abstract class FileSystemComponent {
    abstract void display();
}

class Directory extends FileSystemComponent {
    private String name;
    private List<FileSystemComponent> components = new ArrayList<>();

    public Directory(String name) {
        this.name = name;
    }

    @Override
    void display() {
        System.out.println("Directory: " + name);
        for (FileSystemComponent component : components) {
            component.display();
        }
    }

    public void addComponent(FileSystemComponent component) {
        components.add(component);
    }
}
Python
from abc import ABC, abstractmethod

class FileSystemComponent(ABC):
    @abstractmethod
    def display(self):
        pass

class Directory(FileSystemComponent):
    def __init__(self, name):
        self.name = name
        self.components = []

    def display(self):
        print(f'Directory: {self.name}')
        for component in self.components:
            component.display()

    def add_component(self, component):
        self.components.append(component)
JavaScript
class FileSystemComponent {
    constructor() {}
    display() {
        throw new Error('Method not implemented.');
    }
}

class Directory extends FileSystemComponent {
    constructor(name) {
        super();
        this.name = name;
        this.components = [];
    }

    display() {
        console.log(`Directory: ${this.name}`);
        this.components.forEach(component => component.display());
    }

    addComponent(component) {
        this.components.push(component);
    }
}


In this example, Directory is a composite object. Like the File class, it also implements the FileSystemComponent interface by providing a display method. Additionally it, contains a vector of FileSystemComponent to store child components (files or subdirectories). The addComponent method allows adding components to the directory.

4. Client

The Client code interacts with the components through the Component interface, and it doesn't need to be aware of whether it's working with a leaf or a composite object.

C++
int main() {
    // Create leaf objects (files)
    FileSystemComponent* file1 = new File("document.txt", 1024);
    FileSystemComponent* file2 = new File("image.jpg", 2048);

    // Create a composite object (directory)
    Directory* directory = new Directory("My Documents");

    // Add leaf objects to the directory
    directory->addComponent(file1);
    directory->addComponent(file2);

    // Display the directory (including its contents)
    directory->display();

    return 0;
}
Java
import java.util.ArrayList;

interface FileSystemComponent {
    void display();
}

class File implements FileSystemComponent {
    private String name;
    private int size;

    public File(String name, int size) {
        this.name = name;
        this.size = size;
    }

    @Override
    public void display() {
        System.out.println("File: " + name + " Size: " + size);
    }
}

class Directory implements FileSystemComponent {
    private String name;
    private ArrayList<FileSystemComponent> components;

    public Directory(String name) {
        this.name = name;
        this.components = new ArrayList<>();
    }

    public void addComponent(FileSystemComponent component) {
        components.add(component);
    }

    @Override
    public void display() {
        System.out.println("Directory: " + name);
        for (FileSystemComponent component : components) {
            component.display();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        // Create leaf objects (files)
        FileSystemComponent file1 = new File("document.txt", 1024);
        FileSystemComponent file2 = new File("image.jpg", 2048);

        // Create a composite object (directory)
        Directory directory = new Directory("My Documents");

        // Add leaf objects to the directory
        directory.addComponent(file1);
        directory.addComponent(file2);

        // Display the directory (including its contents)
        directory.display();
    }
}
Python
from abc import ABC, abstractmethod

class FileSystemComponent(ABC):
    @abstractmethod
    def display(self):
        pass

class File(FileSystemComponent):
    def __init__(self, name, size):
        self.name = name
        self.size = size

    def display(self):
        print(f'File: {self.name} Size: {self.size}')

class Directory(FileSystemComponent):
    def __init__(self, name):
        self.name = name
        self.components = []

    def add_component(self, component):
        self.components.append(component)

    def display(self):
        print(f'Directory: {self.name}')
        for component in self.components:
            component.display()

if __name__ == '__main__':
    # Create leaf objects (files)
    file1 = File('document.txt', 1024)
    file2 = File('image.jpg', 2048)

    # Create a composite object (directory)
    directory = Directory('My Documents')

    # Add leaf objects to the directory
    directory.add_component(file1)
    directory.add_component(file2)

    # Display the directory (including its contents)
    directory.display()
JavaScript
class FileSystemComponent {
    display() {
        throw new Error('Method not implemented.');
    }
}

class File extends FileSystemComponent {
    constructor(name, size) {
        super();
        this.name = name;
        this.size = size;
    }

    display() {
        console.log(`File: ${this.name} Size: ${this.size}`);
    }
}

class Directory extends FileSystemComponent {
    constructor(name) {
        super();
        this.name = name;
        this.components = [];
    }

    addComponent(component) {
        this.components.push(component);
    }

    display() {
        console.log(`Directory: ${this.name}`);
        this.components.forEach(component => component.display());
    }
}

// Create leaf objects (files)
const file1 = new File('document.txt', 1024);
const file2 = new File('image.jpg', 2048);

// Create a composite object (directory)
const directory = new Directory('My Documents');

// Add leaf objects to the directory
directory.addComponent(file1);
directory.addComponent(file2);

// Display the directory (including its contents)
directory.display();

In this client code, you can see how the client interacts with both leaf (file) and composite (directory) objects uniformly, without needing to know the specific type of each object. The display method of the FileSystemComponent interface is used to display the entire hierarchy.

Diagrammatic representation of the Composite Design Pattern

Composite-Design-Pattern
UML Diagram: Composite Design Pattern

Code Implementation of the above Problem:

C++
#include <bits/stdc++.h>
class FileSystemComponent {
public:
    virtual void display() const = 0;
};
class File : public FileSystemComponent {
public:
    File(const std::string& name, int size)
        : name(name)
        , size(size)
    {
    }

    void display() const override
    {
        std::cout << "File: " << name << " (" << size
                  << " bytes)" << std::endl;
    }

private:
    std::string name;
    int size;
};
class Directory : public FileSystemComponent {
public:
    Directory(const std::string& name)
        : name(name)
    {
    }

    void display() const override
    {
        std::cout << "Directory: " << name << std::endl;
        for (const auto& component : components) {
            component->display();
        }
    }

    void addComponent(FileSystemComponent* component)
    {
        components.push_back(component);
    }

private:
    std::string name;
    std::vector<FileSystemComponent*> components;
};
int main()
{
    // Create leaf objects (files)
    FileSystemComponent* file1
        = new File("document.txt", 1024);
    FileSystemComponent* file2
        = new File("image.jpg", 2048);

    // Create a composite object (directory)
    Directory* directory = new Directory("My Documents");

    // Add leaf objects to the directory
    directory->addComponent(file1);
    directory->addComponent(file2);

    // Display the directory (including its contents)
    directory->display();

    return 0;
}
Java
import java.util.ArrayList;
import java.util.List;

// Abstract class for FileSystemComponent
abstract class FileSystemComponent {
    public abstract void display();
}

// Concrete class for File
class File extends FileSystemComponent {
    private String name;
    private int size;

    public File(String name, int size) {
        this.name = name;
        this.size = size;
    }

    @Override
    public void display() {
        System.out.println("File: " + name + " (" + size + " bytes)");
    }
}

// Concrete class for Directory
class Directory extends FileSystemComponent {
    private String name;
    private List<FileSystemComponent> components;

    public Directory(String name) {
        this.name = name;
        this.components = new ArrayList<>();
    }

    @Override
    public void display() {
        System.out.println("Directory: " + name);
        for (FileSystemComponent component : components) {
            component.display();
        }
    }

    public void addComponent(FileSystemComponent component) {
        components.add(component);
    }
}

public class Main {
    public static void main(String[] args) {
        // Create leaf objects (files)
        FileSystemComponent file1 = new File("document.txt", 1024);
        FileSystemComponent file2 = new File("image.jpg", 2048);

        // Create a composite object (directory)
        Directory directory = new Directory("My Documents");

        // Add leaf objects to the directory
        directory.addComponent(file1);
        directory.addComponent(file2);

        // Display the directory (including its contents)
        directory.display();
    }
}
Python
from abc import ABC, abstractmethod

# Abstract class for FileSystemComponent
class FileSystemComponent(ABC):
    @abstractmethod
    def display(self):
        pass

# Concrete class for File
class File(FileSystemComponent):
    def __init__(self, name, size):
        self.name = name
        self.size = size

    def display(self):
        print(f'File: {self.name} ({self.size} bytes)')

# Concrete class for Directory
class Directory(FileSystemComponent):
    def __init__(self, name):
        self.name = name
        self.components = []

    def display(self):
        print(f'Directory: {self.name}')
        for component in self.components:
            component.display()

    def add_component(self, component):
        self.components.append(component)

# Main function
if __name__ == '__main__':
    # Create leaf objects (files)
    file1 = File('document.txt', 1024)
    file2 = File('image.jpg', 2048)

    # Create a composite object (directory)
    directory = Directory('My Documents')

    # Add leaf objects to the directory
    directory.add_component(file1)
    directory.add_component(file2)

    # Display the directory (including its contents)
    directory.display()
JavaScript
'use strict';

// Abstract class for FileSystemComponent
class FileSystemComponent {
    constructor() {
        if (new.target === FileSystemComponent) {
            throw new TypeError('Cannot construct FileSystemComponent instances directly');
        }
    }

    display() {
        throw new Error('Method display() must be implemented.');
    }
}

// Concrete class for File
class File extends FileSystemComponent {
    constructor(name, size) {
        super();
        this.name = name;
        this.size = size;
    }

    display() {
        console.log(`File: ${this.name} (${this.size} bytes)`);
    }
}

// Concrete class for Directory
class Directory extends FileSystemComponent {
    constructor(name) {
        super();
        this.name = name;
        this.components = [];
    }

    display() {
        console.log(`Directory: ${this.name}`);
        for (const component of this.components) {
            component.display();
        }
    }

    add_component(component) {
        this.components.push(component);
    }
}

// Main function
(function () {
    // Create leaf objects (files)
    const file1 = new File('document.txt', 1024);
    const file2 = new File('image.jpg', 2048);

    // Create a composite object (directory)
    const directory = new Directory('My Documents');

    // Add leaf objects to the directory
    directory.add_component(file1);
    directory.add_component(file2);

    // Display the directory (including its contents)
    directory.display();
})();



Output
Directory: My Documents
File: document.txt (1024 bytes)
File: image.jpg (2048 bytes)

Explore