I'm using Java Eclipse EMF to model my Composite Pattern. What would be the right UML representation to model aa new class (Root) which implements a unique root directory. This is the original Composite pattern.
This is my representation:
Target representation would be:
root
|___ dir1
|___ dir2
|___ dir3
| |___ fileA
| |___ dir4
| |__ fileB
|
|___ file1
Best Answer
Your diagrams are wrong.
First, the
add
,remove
, andgetChild
SHOULD NOT be onComponent
. That is pretty obvious from image on wiki. Especially in your case, where all of those operations make no sense forFile
.The second issue is the
operation
method. The composite means that if you call operation on theComposite
, then it delegates it to it's children. And when client ofComponent
is using it, it doesn't care if it is using just single leaf, composite or complex tree of composites and leafs.