Design pattern "Composite"
Real world example
Letβs suppose that we have a file structure within a directory and we are interested in determining the size of the root directory. To accomplish this, we need to traverse each subdirectory and recursively calculate the size of each one.
While it might seem straightforward to use a foreach loop for this task, itβs actually quite complex. This is where the Composite design pattern comes into play and proves to be very useful.
Loading graph...
In the Composite Pattern:
- Leaf represents the end object of a composition. In our case, a file can be considered a leaf.
- Composite is an element that has sub-elements (leaves or other composites). In our case, a directory is a composite.
- Component is a common interface for all elements in a composition (i.e., it is implemented by both Leaf and Composite).
In plain words
Composite pattern lets clients treat the individual objects in a uniform manner.
Wikipedia definition
In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects is to be treated in the same way as a single instance of an object. The intent of a composite is to βcomposeβ objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.
Programmatic example
Diagram
Loading graph...
Some other examples
- File System: As mentioned earlier, a file system on a computer consists of files and directories. Each directory can contain several files and directories. Here, the Composite pattern is very useful to treat files and directories in the same way.
- Graphics Rendering: In graphics editors, shapes can be simple (like a line or a circle) or complex (a combination of multiple shapes). The Composite pattern allows treating simple and complex shapes uniformly.
- Organizational Structures: In an organization, an employee can be a manager or a general worker. A manager might have other managers or workers reporting to them. The Composite pattern allows treating all employees uniformly.
- Menu Systems: In graphical applications, menus that contain menu items are common. Each menu item could be a simple action or a submenu. The Composite pattern allows treating all menu items uniformly.
- HTML DOM: In a web page, an HTML element can contain other HTML elements. The Composite pattern allows treating individual and composite elements uniformly.
- Networking: In a network, a node could be a terminal node or a composite node that contains other nodes. The Composite pattern allows treating all nodes uniformly.
- Game Programming: In a game, a game object could be a simple entity (like a bullet) or a composite entity (like a vehicle). The Composite pattern allows treating all game objects uniformly.
- GUI Widgets: In a graphical user interface, a widget could be a simple widget (like a button) or a composite widget (like a panel). The Composite pattern allows treating all widgets uniformly.
- Geometric Shapes: In a vector graphics editor, a shape can be simple (like a circle) or complex (a combination of multiple shapes). The Composite pattern allows treating simple and complex shapes uniformly.
- Product Assemblies: In manufacturing, a product could be a simple part or an assembly of multiple parts. The Composite pattern allows treating all products uniformly.
Design pattern "Strategy"
Let's learn what is the "Strategy" design pattern
Design pattern "Visitor"
Let's learn what is the "Visitor" design pattern π€ππΆπ§³
Design pattern "Iterator"
Let's learn what is the "Iterator" design pattern π
Design pattern "Observer"
Let's learn what is the "Observer" design pattern ππππ
Design pattern "Memento"
Let's learn what is the "Memento" design pattern πΈοΈπΌοΈπΌοΈππ°οΈπ
Design pattern "Facade"
Let's learn what is the "Facade" design pattern πΉοΈποΈπ¨
Design pattern "Decorator"
Let's learn what is the "Decorator" design pattern πͺβββ
Design pattern "State"
Let's learn what is the "State" design pattern ππ»ββοΈπΆπ»ππ»