![]() |
VOOZH | about |
The Composite Pattern is a structural design pattern that organizes objects into tree structures, enabling clients to treat individual and composite objects uniformly through a common interface.
Example: In a file system, files (leaf nodes) and folders (composite nodes) share a common interface, allowing operations like display or delete to be performed uniformly on both individual items and groups.
In the Diagram
Part-Whole (Whole-Part) hierarchies represent complex objects (wholes) composed of simpler objects (parts). This structure allows both individual objects and groups of objects to be treated uniformly, often modeled using the Composite Pattern.
Example: In a graphic design application, you might have shapes as individual elements (like circles and rectangles), and you can combine these shapes to create more complex shapes (like a smiley face with eyes and a mouth). The Composite Pattern lets you work with both simple shapes and complex shapes using the same set of operations, making it easier to manage and manipulate them.
In this context:
The Composite Pattern is useful in various scenarios, such as:
The Composite Pattern consists of key elements that allow treating individual objects and groups uniformly.
The Composite Pattern works by defining a common interface that is shared by both individual objects and groups of objects.
The Composite Pattern is used when dealing with hierarchical structures.
You are tasked with developing a software component to manage a hierarchical file system structure. The goal is to implement the Composite Pattern to seamlessly work with individual files and directories as part of a unified hierarchy.
The practical application of the design pattern using code.
In the file system hierarchy example, the Component is represented by the FileSystemComponent interface. This interface defines the common interface for both leaf and composite objects. It declares a method, display(), which all classes in the hierarchy must implement.
The Component serves as the foundation for all objects within the hierarchy. Whether it's file or a directory, they all must adhere to this common interface.
In the context of our file system hierarchy example, Leaf objects are the individual files. These are the objects that do not have any children. Here is an implementation of a leaf object, a file:
Here, File is a leaf object. It implements the FileSystemComponent interface by providing a display method. It contains data specific to files, such as their name and size.
In the file system hierarchy example, Composite objects are directories. These are objects that contain other components, including both leaf objects (files) and other composite objects (subdirectories). Here's an implementation of a composite object, a directory:
The Client code interacts with the components through the Component interface, and it doesn't need to be aware of whether it's working with a leaf or a composite object.
In this client code, you can see how the client interacts with both leaf (file) and composite (directory) objects uniformly, without needing to know the specific type of each object.
Directory: My Documents File: document.txt (1024 bytes) File: image.jpg (2048 bytes)
The Composite Pattern offers several benefits when working with hierarchical object structures.
Despite its benefits, the Composite Pattern also has some drawbacks.