DEV Community

Taki
Taki

Posted on • Edited on

Composite Pattern

The Composite Pattern with real-world scenarios you already know, then see how it works in software.


πŸ“– What Is the Composite Pattern (Simple Explanation)

The Composite Pattern lets you treat individual objects and groups of objects the same way.

It builds tree-like structures where:

  • Leaf nodes are individual things (like a button, a file, a single employee)
  • Composite nodes are collections of other things (like a panel with multiple buttons, a folder with files, or a manager with employees)

🎨 Real-World Examples (Easy to Visualize)

πŸ“ 1. File System (Folders & Files)

  • File β€” an individual document (leaf)
  • Folder β€” a container holding files or other folders (composite)

You can open, move, or delete both files and folders β€” even though a folder contains more things inside.

➑️ File and Folder both follow the same interface:

  • open()
  • delete()
  • getSize()

But a folder recursively calls those methods on its contents.


πŸ§‘β€πŸ’Ό 2. Company Organizational Chart

  • Employee β€” an individual staff member (leaf)
  • Manager β€” has multiple subordinates (composite)

Both employees and managers can:

  • getName()
  • getSalary()
  • showDetails()

But a manager does this by delegating to its team.


🧱 3. UI Layout

  • Button, Label, TextBox β€” UI elements (leaves)
  • Panel, Form, Page β€” contain other UI elements (composites)

You can render, move, or hide both individual components and entire panels in the same way.


πŸ–₯️ Code Concept Behind This

Let’s pick the File System case and model it in TypeScript using the Composite Pattern.

interface FileSystemItem { show(indent?: string): void; } class File implements FileSystemItem { constructor(private name: string) {} show(indent: string = '') { console.log(`${indent}- ${this.name}`); } } class Folder implements FileSystemItem { private items: FileSystemItem[] = []; constructor(private name: string) {} add(item: FileSystemItem) { this.items.push(item); } show(indent: string = '') { console.log(`${indent}+ ${this.name}`); this.items.forEach(item => item.show(indent + ' ')); } } // Usage const file1 = new File('Resume.pdf'); const file2 = new File('CoverLetter.docx'); const folder = new Folder('Job Applications'); folder.add(file1); folder.add(file2); const mainFolder = new Folder('Documents'); mainFolder.add(folder); mainFolder.add(new File('Photo.jpg')); mainFolder.show(); 
Enter fullscreen mode Exit fullscreen mode

Console Output:

+ Documents + Job Applications - Resume.pdf - CoverLetter.docx - Photo.jpg 
Enter fullscreen mode Exit fullscreen mode

🎯 Why Is This Useful?

βœ… You can treat both a file and a folder the same way (show())

βœ… Easy to build hierarchies

βœ… Great for recursive structures like:

  • Filesystem
  • UI Layouts
  • Organization Trees
  • Nested Menus
  • Multi-step Forms βœ… It lets you write simpler, cleaner, and scalable code

βœ… Summary

Concept Leaf Composite
File System File Folder
Company Employee Manager
UI Layout Button / Text Panel / Form
Task List Simple Task Grouped Tasks
Menu Single Menu Item Submenu

πŸ‘‰ Composite Pattern makes hierarchical structures uniform and easy to manage

Top comments (0)