Java – Modeling Composite Design Pattern

design-patternsjavauml

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.

enter image description here

This is my representation:

enter image description here

Target representation would be:

root
  |___ dir1
  |___ dir2
  |___ dir3
  |      |___ fileA
  |      |___ dir4
  |             |__ fileB
  |    
  |___ file1

Best Answer

Your diagrams are wrong.

First, the add, remove, and getChild SHOULD NOT be on Component. That is pretty obvious from image on wiki. Especially in your case, where all of those operations make no sense for File.

The second issue is the operation method. The composite means that if you call operation on the Composite, then it delegates it to it's children. And when client of Component is using it, it doesn't care if it is using just single leaf, composite or complex tree of composites and leafs.

Related Topic