Composite
The Composite design pattern facilitates the composition of objects into tree structures, enabling the manipulation of these structures as if the

Composite Design Pattern
The Composite design pattern is a structural pattern used in software engineering to compose objects into tree-like structures to represent part-whole hierarchies. It allows clients to treat individual objects and compositions of objects uniformly.
Key Components:
Component: Defines the common interface for both leaf and composite objects. It typically declares methods for accessing and managing children and may include default implementations for common operations.
Leaf: Represents the individual objects in the composition. These are the building blocks that don't have children.
Composite: Contains other components, including both leaves and other composites. It implements methods to manipulate children's components.
Example:
Let's illustrate the Composite pattern with a file system representation consisting of directories and files.
// Component
interface FileSystemComponent {
void display();
}
// Leaf
class File implements FileSystemComponent {
private String name;
public File(String name) {
this.name = name;
}
public void display() {
System.out.println("File: " + name);
}
}
// Composite
class Directory implements FileSystemComponent {
private String name;
private List<FileSystemComponent> components;
public Directory(String name) {
this.name = name;
components = new ArrayList<>();
}
public void addComponent(FileSystemComponent component) {
components.add(component);
}
public void removeComponent(FileSystemComponent component) {
components.remove(component);
}
public void display() {
System.out.println("Directory: " + name);
for (FileSystemComponent component : components) {
component.display();
}
}
}
In this example:
FileSystemComponentis the component interface.Fileis a leaf component representing individual files.Directoryis a composite component representing directories. It can contain both files and other directories.
Now, let's create a file system structure:
public class Main {
public static void main(String[] args) {
Directory root = new Directory("Root");
Directory documents = new Directory("Documents");
documents.addComponent(new File("Resume.docx"));
documents.addComponent(new File("Presentation.pptx"));
root.addComponent(documents);
Directory pictures = new Directory("Pictures");
pictures.addComponent(new File("Family.jpg"));
pictures.addComponent(new File("Vacation.jpg"));
root.addComponent(pictures);
root.display();
}
}
Output:
Directory: Root
Directory: Documents
File: Resume.docx
File: Presentation.pptx
Directory: Pictures
File: Family.jpg
File: Vacation.jpg
In this example, we demonstrated the Composite pattern by representing a file system structure. The Directory class acts as a composite that can contain both files and directories, while the File class represents individual files. This pattern allows for a unified treatment of files and directories, making client code simpler and more manageable.
About the author
Android Developer With the experience of 9 plus years | Expert in Kotlin, Java, And KMM | Expert Android Jetpack, Coroutine, and MVVM | 30 plus Applications developed | 10 plus on Play stores.
Every positive action, no matter how small, contributes to the greater good of humanity.
Comments
Join the discussionNo comments yet. Be the first to comment.
Design Patters
Discover our Design Patterns series: Unlock the secrets of effective software design with insightful posts exploring various design patterns!





