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
Copy file name to clipboardExpand all lines: StructuralPatterns/Composite/README.md
+33-18Lines changed: 33 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Composite Pattern
2
2
3
-
The intent of composite pattern is to *compose* objects into tree structures to represent part-whole hierarchies.
3
+
The intent of composite pattern is to *compose* objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
4
4
5
5
## Problem
6
6
@@ -12,10 +12,35 @@ The intent of composite pattern is to *compose* objects into tree structures to
12
12
* Define a unified `Component` interface for both part (`Leaf`) objects and whole (`Composite`) objects.
13
13
* Individual `Leaf` objects implement the `Component` interface directly, and `Composite` objects forward requests to their child components.
14
14
15
+
## Common Structure
16
+
17
+

18
+
19
+
* Component (IFileSystemComponent)
20
+
* declares an interface for objects in the composition.
21
+
* implements default behaviour for the interface common to all classes, as appropriate.
22
+
* Leaf (File)
23
+
* A leaf has no children.
24
+
* defines behavior for primitive objects in the composition.
25
+
* Composite (Folder)
26
+
* stores child components
27
+
* defines/implements behaviour for components having children.
28
+
* Client (FileSystem)
29
+
* manipulates objects in the composition through the Component interface.
30
+
31
+
## Collaboration
32
+
33
+
* Clients use the Component class interface to interact with objects in the composite structure.
34
+
* If the recipient is a Leaf, then the request is handled directly.
35
+
* If the recipient is a Composite, then it usually forwards requests to its child components, possibly performing additional operations before and/or after forwarding.
36
+
15
37
## Benefits
16
38
17
-
* We can apply the same operations over both composites and individual objects (leaves).
39
+
* Makes the client simple.
40
+
* It can apply the same operations over both composites and individual objects (leaves).
18
41
* In most cases we can ignore the differences between the composition of objects and leaves.
42
+
* Makes it easier to add new kinds of components
43
+
* Newly defined Composite or Leaf subclasses work automatically with existing structures and client code.
19
44
20
45
## Drawbacks
21
46
@@ -106,20 +131,10 @@ Output
106
131
File 1 in Main Folder
107
132
```
108
133
109
-
## Common Structure
110
-
111
-

112
-
113
-
* Component (IFileSystemComponent)
114
-
* declares an interface for objects in the composition.
115
-
* implements default behaviour for the interface common to all classes, as appropriate.
116
-
* Leaf (File)
117
-
* A leaf has no children.
118
-
* defines behavior for primitive objects in the composition.
119
-
* Composite (Folder)
120
-
* stores child components
121
-
* defines/implements behaviour for components having children.
122
-
* Client (FileSystem)
123
-
* manipulates objects in the composition through the Component interface.
***Chain of Responsibility** - Often the component-parent link is used for a Chain of Responsibility.
137
+
***Decorator** is often used with Composite. When decorators and composites are used together, they will usually have a common parent class. So decorators will have to support the Component interface with operations like Add, Remove, and GetChild.
138
+
***Flyweight** lets you share components, but they can no longer refer to their parents.
139
+
***Iterator** can be used to traverse composites.
140
+
***Visitor** localizes operations and behavior that would otherwise be distributed across Composite and Leaf classes.
0 commit comments