Skip to content

Commit 170be3a

Browse files
committed
complete Composite Pattern
1 parent b706ff8 commit 170be3a

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

StructuralPatterns/Composite/README.md

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Composite Pattern
22

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.
44

55
## Problem
66

@@ -12,10 +12,35 @@ The intent of composite pattern is to *compose* objects into tree structures to
1212
* Define a unified `Component` interface for both part (`Leaf`) objects and whole (`Composite`) objects.
1313
* Individual `Leaf` objects implement the `Component` interface directly, and `Composite` objects forward requests to their child components.
1414

15+
## Common Structure
16+
17+
![Common structure of composite pattern](https://upload.wikimedia.org/wikipedia/commons/6/65/W3sDesign_Composite_Design_Pattern_UML.jpg)
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+
1537
## Benefits
1638

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).
1841
* 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.
1944

2045
## Drawbacks
2146

@@ -106,20 +131,10 @@ Output
106131
File 1 in Main Folder
107132
```
108133

109-
## Common Structure
110-
111-
![Common structure of composite pattern](https://upload.wikimedia.org/wikipedia/commons/6/65/W3sDesign_Composite_Design_Pattern_UML.jpg)
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.
134+
## Relations with Other Patterns
124135

125-
_[Source: http://www.dofactory.com/net/composite-design-pattern]_
136+
* **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

Comments
 (0)