The Composite Pattern is a structural design pattern that allows you to compose objects into tree structures to represent part-whole hierarchies. This pattern lets clients treat individual objects and compositions of objects uniformly.
Imagine you have a file system where files and folders can contain other files and folders. You want to be able to perform operations (e.g., search) on both individual files and folders containing multiple files and subfolders. Without a unified interface, the client code has to handle files and folders differently, making the code complex and difficult to maintain.
The Composite Pattern helps solve this problem by defining a unified interface for both individual objects (files) and compositions of objects (folders). In this example, File
and Folder
classes are created to represent files and folders, respectively, and both implement a common interface.
- Define the Component Interface: This interface will have methods that both files and folders will implement.
- Create Leaf Class: Implement the
Component
interface for individual objects (e.g., File). - Create Composite Class: Implement the
Component
interface for compositions of objects (e.g., Folder). - Implement Operations: Implement operations (e.g., search) that can be performed on both individual objects and compositions.
- Component Interface: Defines the methods that both files and folders will implement.
- Leaf Class: File class implementing the
Component
interface. - Composite Class: Folder class implementing the
Component
interface.
The main function demonstrates how to use the File
and Folder
classes to create a file system and perform a search operation.
Searching for rose in file2
Searching for rose in file3
Searching for rose in file1
Searching for rose in file2
Searching for rose in file3
- Unified Interface: Provides a unified interface for both individual objects and compositions of objects.
- Simplified Client Code: Simplifies client code by allowing it to treat individual objects and compositions uniformly.
- Scalability: Easily extendable to include new types of components without changing the client code.
Tutorial Link: Refactoring Guru: Composite Pattern in Go
The tutorial at Refactoring Guru provides a great example of the Composite Pattern. I recommend readers to check their tutorial for better learning!