Composite Design Pattern
Video Lecture
Section | Video Links |
---|---|
Composite Overview | |
Composite Use Case | |
Conditional Expressions |
Overview
The Composite design pattern is a structural pattern useful for hierarchical management.
The Composite design pattern,
- Allows you to represent individual entities(leaves) and groups of leaves as the same.
- Is a structural design pattern that lets you compose objects into a changeable tree structure.
- Is great if you need the option of swapping hierarchical relationships around.
- Allows you to add/remove components to the hierarchy.
- Provides flexibility of structure
Examples of using the Composite Design Pattern can be seen in a file system directory structure where you can swap the hierarchy of files and folders, and also in a drawing program where you can group, ungroup, transform objects and change multiple objects at the same time.
Terminology
- Component Interface: The interface that all leaves and composites should implement.
- Leaf: A single object that can exist inside or outside a composite.
- Composite: A collection of leaves and/or other composites.
Composite UML Diagram
Source Code
In this concept code, two leaves are created, LEAF_A
and LEAF_B
, and two composites are created, COMPOSITE_1
and COMPOSITE_2
.
LEAF_A
is attached to COMPOSITE_1
.
Then I change my mind and attach LEAF_A
to COMPOSITE_2
.
I then attach COMPOSITE_1
to COMPOSITE_2
.
LEAF_B
is not attached to composites.
./composite/composite_concept.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
Output
python ./composite/composite_concept.py
LEAF_A id:2050574298848
LEAF_B id:2050574298656
COMPOSITE_1 id:2050574298272
COMPOSITE_2 id:2050574298128
<Leaf> id:2050574298656 Parent: None
<Composite> id:2050574298128 Parent: None Components:2
<Leaf> id:2050574298848 Parent: 2050574298128
<Composite> id:2050574298272 Parent: 2050574298128 Components:0
SBCODE Editor
Composite Example Use Case
Demonstration of a simple in memory hierarchical file system.
A root object is created that is a composite.
Several files (leaves) are created and added to the root folder.
More folders (composites) are created, and more files are added, and then the hierarchy is reordered.
Composite Example UML Diagram
Source Code
./composite/client.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
./composite/file.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
./composite/folder.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
./composite/interface_component.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Output
python ./composite/client.py
<DIR> root id:2028913323984 Components: 4
..<FILE> abc.txt id:2028913323888 Parent: 2028913323984
..<FILE> 123.txt id:2028913323792 Parent: 2028913323984
..<DIR> folder_a id:2028913432848 Components: 1
....<FILE> xyz.txt id:2028913433088 Parent: 2028913432848
..<DIR> folder_b id:2028913433184 Components: 1
....<FILE> 456.txt id:2028913434432 Parent: 2028913433184
<DIR> root id:2028913323984 Components: 3
..<FILE> abc.txt id:2028913323888 Parent: 2028913323984
..<FILE> 123.txt id:2028913323792 Parent: 2028913323984
..<DIR> folder_b id:2028913433184 Components: 2
....<FILE> 456.txt id:2028913434432 Parent: 2028913433184
....<DIR> folder_a id:2028913432848 Components: 1
......<FILE> xyz.txt id:2028913433088 Parent: 2028913432848
SBCODE Editor
New Coding Concepts
Conditional Expressions (Ternary Operators).
In ./composite/composite_concept.py, there are two conditional expressions.
Conditional expressions an alternate form of if/else
statement.
id(self.reference_to_parent) if self.reference_to_parent is not None else None
If the self.reference_to_parent
is not None
, it will return the memory address (ID) of self.reference_to_parent
, otherwise it returns None
.
This conditional expression follows the format
value_if_true if condition else value_if_false
e.g.,
1 2 3 |
|
or
1 2 3 |
|
or
1 2 3 4 |
|
Visit https://docs.python.org/3/reference/expressions.html#conditional-expressions for more examples of conditional expressions.
Summary
- The Composite design pattern allows you to structure components in a manageable hierarchical order.
- It provides flexibility of structure since you can add/remove and reorder components.
- File explorer on Windows is a very good example of the composite design pattern in use.
- Any system where you need to offer at runtime the ability to group, ungroup, modify multiple objects at the same time, would benefit from the composite design pattern structure. Programs that allow you to draw shapes and graphics will often also use this structure as well.