System DesignLLD (Low-Level Design)

Composite Design Pattern | Build File System

M

Mostakin Mondal

February 21, 2026•5 min read·0% completed

When you work with files and folders, you notice something simple: whether it’s a single file or a whole folder, you often do the same actions on both. You can open them, delete them, or check their size.

The Composite Design Pattern helps in software design by treating individual objects (like files) and groups of objects (like folders) in the same way. This makes it easier to manage complex structures because you don’t need separate code for single items and collections.

What is the Composite Pattern?

The Composite Pattern lets you handle single objects and groups of objects in the same way.

  • A file is a single unit.

  • A folder is a collection that can hold files or even other folders.

Both files and folders can use the same interface, so your code doesn’t need to care whether it’s working with one item or many. This makes building structures like a file system simple and consistent.

Example: File Explorer

Think about your computer’s file explorer.

  • A file could be something like “Resume.pdf.”

  • A folder might hold several files or even more folders.

When you right-click on either a file or a folder, you see similar options: open, delete, or view properties. You don’t have to handle files and folders differently in your code. This is exactly what the Composite Pattern does: it lets the system treat single items and collections of items in the same way.

Example UML Diagram

UML Breakdown: File System Example

To represent a file system using the Composite Pattern, we typically have three main elements:

1. Component (Interface)

  • Defines common methods for both files and folders, like open(), getSize(), and display().

2. File (Leaf Node)

  • Represents a single file.

  • getSize() returns the size of the file.

  • display() shows the file name.

3. Folder (Composite Node)

  • Can contain files or other folders.

  • add(Component) and remove(Component) let you manage its children.

  • getSize() adds up the sizes of everything inside the folder.

This structure allows the system to treat files and folders in the same way, making it easier to work with part-whole hierarchies.

Why Use the Composite Pattern?

The Composite Pattern helps you handle files and folders in a consistent way. Here’s why it matters:

Consistency – You can treat a single file and a folder containing many files the same way.

Scalability – You can nest folders inside folders without changing how the system works.

Flexibility – Add or remove files and folders at runtime easily.

Encapsulation – Folders manage their internal contents without exposing unnecessary details.

When to Use It

  • When you have a tree-like structure, such as a file system, menu, or organizational chart.

  • When you want to apply the same operations to individual items and groups.

  • When you want to reduce duplicate code for handling single items versus collections.

When Not to Use It

  • If you need to restrict what types of components can go into a group.

  • If your hierarchy is very simple, the pattern may overcomplicate your design.

  • When strict type enforcement is required, because the Composite Pattern usually relies on runtime checks rather than compile-time restrictions.

This pattern makes working with hierarchical structures simple, uniform, and scalable while keeping the system easy to manage.

Sample Use Case: Recursive Folder Scanner

Using the Composite Pattern, you can handle files and folders in the same way. For example:

  • List all files – Get a full list of files inside a folder, no matter how deep the subfolders go.

  • Calculate total size – Sum up the size of a folder including all files and subfolders.

  • Search nested items – Find files or folders deep inside a structure without writing separate logic for single files and folders.

This is exactly how your computer’s file explorer works when you check folder properties. The Composite Pattern makes it easy to perform these operations in a uniform way.

UML Diagram

Best Practices

  • Define a common interface or abstract class – Make sure all components (files and folders) share the same basic methods.

  • Keep responsibilities clear – Let files handle their own behavior and folders handle their collections of files or subfolders.

  • Use recursion for nested structures – When a folder contains other folders, recursion makes it easy to process everything without complicated code.

These practices keep your design simple, clean, and easy to maintain.

Summary

The Composite Design Pattern lets you treat single objects and groups of objects the same way. In a file system, for example, files and folders share a common interface, so you can open, delete, or get the size of either without writing separate code. Folders can contain files or other folders, and operations can be applied recursively. This makes handling tree-like structures, like menus or organizational charts, consistent, flexible, and easy to manage, keeping your code clean and scalable.

Code

By Coder Army

Related Articles

Composite Design Pattern | Build File System | Coder Army