πComposite Pattern β File System Example
π§ Concept
The Composite Pattern lets you treat individual objects (files) and compositions of objects (folders) in a uniform way, forming a tree structure.
π‘ Think of it like your computerβs file explorer β you can βopen,β βdelete,β or βrenameβ both files and folders the same way.
π§± Key Roles
Component
Common interface for both files and folders.
Leaf (File)
Represents indivisible objects β no children.
Composite (Folder)
Represents containers that can hold files or other folders.
Client
Works with all components through the same interface.
π» Java Example β File System
// ----- Component -----
interface FileSystemComponent {
void showDetails(String indent);
}
// ----- Leaf -----
class FileItem implements FileSystemComponent {
private String name;
public FileItem(String name) {
this.name = name;
}
@Override
public void showDetails(String indent) {
System.out.println(indent + "π File: " + name);
}
}
// ----- Composite -----
class Folder implements FileSystemComponent {
private String name;
private List<FileSystemComponent> children = new ArrayList<>();
public Folder(String name) {
this.name = name;
}
public void add(FileSystemComponent component) {
children.add(component);
}
public void remove(FileSystemComponent component) {
children.remove(component);
}
@Override
public void showDetails(String indent) {
System.out.println(indent + "π Folder: " + name);
for (FileSystemComponent child : children) {
child.showDetails(indent + " ");
}
}
}
// ----- Client -----
public class Main {
public static void main(String[] args) {
// Create individual files
FileSystemComponent file1 = new FileItem("resume.pdf");
FileSystemComponent file2 = new FileItem("photo.jpg");
FileSystemComponent file3 = new FileItem("notes.txt");
// Create folders
Folder docsFolder = new Folder("Documents");
Folder picsFolder = new Folder("Pictures");
Folder rootFolder = new Folder("Home");
// Compose the structure
docsFolder.add(file1);
picsFolder.add(file2);
rootFolder.add(docsFolder);
rootFolder.add(picsFolder);
rootFolder.add(file3);
// Display full structure
rootFolder.showDetails("");
}
}Output:
π§ Flow Summary
1οΈβ£
Create FileItem objects
Represent leaf nodes (no children)
2οΈβ£
Create Folder objects
Composite nodes that can contain other components
3οΈβ£
Add files/folders into folders
Build a tree hierarchy
4οΈβ£
Call showDetails() on root folder
Recursively traverses and displays structure
π§© Both files and folders share the same interface (
FileSystemComponent), so the client can treat them uniformly.
πͺ Summary
Builds partβwhole tree structures (folders inside folders).
Treats leaf and composite objects the same.
Makes recursion and traversal simple β each component handles its own behavior.
Promotes open/closed principle β easily add new component types (like shortcuts).
π§Ύ Real-world Analogy
π» Operating System File Explorer
File β Cannot contain anything (Leaf)
Folder β Can contain files and folders (Composite)
Both can be opened, renamed, or deleted using the same commands.
π‘ The system doesnβt care if itβs dealing with a file or folder β same interface!
π§ TL;DR
Composite Pattern = Treat individual and group objects uniformly in a tree structure. βFolders contain files and other folders β and both can be handled the same way.β
Last updated