You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Stay tuned, this section will be completed shortly !
3
+
**Compose objects** into tree structures to represent part-whole hierarchies.
4
+
5
+
It **lets clients treat individual objects and compositions uniformly**.
6
+
7
+
You should use the _Composite_ pattern in the following cases :
8
+
- The core model of your app can be **represented as a tree**.
9
+
- You want (or you want the Client) to perform operations on composite objects as if they were standard objects - that is interacting with them using a **common interface**.
10
+
11
+
# How it's done
12
+
13
+

14
+
15
+
**Participants**
16
+
17
+
-_Component_ :
18
+
- Declares the **interface** for the objects in the composition.
19
+
- Implements the **default behaviour** for the **common interface** to all classes.
20
+
- Declares an **interface** for **managing childs components**.
21
+
-_Leaf_ :
22
+
- An object of the composition that has no children.
23
+
- Defines the **default behaviour** of **primitive objects**.
24
+
-_Composite_ :
25
+
- Defines behaviour for components having children.
26
+
-**Stores** the child components.
27
+
- Implements the **child-related operations** in the _Component_ interface.
28
+
-_Client_ : Manipulates the objects in the composition through the _Component_ interface.
29
+
30
+
**How to implement**
31
+
32
+
1.**Make sure** the model of your app can be represented as a Tree.
33
+
2.**Declare** the _Component_ interface with the methods that make sense for both _Leafs_ and _Composites_.
34
+
3.**Create**_Leaf_ classes to represent simple elements that do not have children.
35
+
4.**Create**_Composite_ classes with an underlying container for children, methods to add/remove children.
36
+
5.**Implement** the _Component_ methods in the _Composite_ delegating the hard work to children.
37
+
38
+
Note : UML class diagram taken from [**here**](https://upload.wikimedia.org/wikipedia/commons/3/39/W3sDesign_Composite_Design_Pattern_Type_Safety_UML.jpg)
39
+
40
+
# Pros & cons
41
+
42
+
**Pros**
43
+
44
+
- You and your clients can work easily with complex structures.
45
+
-**Open/Closed principle**: It is easy to introduce new elements in the apps without breaking the existing code.
46
+
- Reduce code complexity by eliminating many loops over the homogeneous collection of objects.
47
+
48
+
**Cons**
49
+
50
+
- It might be difficult to provide a common interface for classes whose functionality differs too much. In certain scenarios, you’d need to overgeneralize the component interface, making it harder to comprehend.
51
+
52
+
# Notes
53
+
54
+
Here are some _usefull ressources_ :
55
+
- A great article on [**Source making**](https://sourcemaking.com/design_patterns/composite)
56
+
- A [**blog article**](http://www.vishalchovatiya.com/composite-design-pattern-in-modern-cpp/) showing an example using [**CRTP**](https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern)
57
+
- A [**Refactoring guru**](https://refactoring.guru/design-patterns/composite) article.
0 commit comments