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:

  |___ dir1
  |___ dir2
  |___ dir3
  |      |___ fileA
  |      |___ dir4
  |             |__ fileB
  |___ file1
  • If Root class will only implement a new instance of Directory I guess the relationship should be connected to Directory not the Component.
    – gogasca
    Commented Jan 4, 2017 at 7:55
  • 1
    Isnt the point of Composite pattern is to provide the Client a unique interface which could be either a Composite or Leaf? "The Composite composes objects into tree structures and lets clients treat individual objects and compositions uniformly."
    – cowgirl
    Commented Jan 4, 2017 at 8:12
  • What's that lonely open triangle in the middle? That's no UML element at all.
    – user188153
    Commented Jan 4, 2017 at 12:15

2 Answers 2


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.

  • 1
    This is a debate raised initially at the GoF book (Page 167) which is a decision of a trade-off between transparency and safety. wiki.c2.com/?CompositePattern "Defining the child management interface at the root of the class hierarchy gives you transparency, because you can treat all components uniformly. It cost you safety, however, because clients may try to do meaningless things like add and remove from leaves"
    – gogasca
    Commented Jan 6, 2017 at 8:33

Your current diagram indicates that FileSystem uses either a single file (without any directories) or a directory tree.

I understand that the actual requirement is that a FileSystem always consists of a directory tree of at least one directory (root) and contents of that root directory, which can be sub-directories.
This can best be modeled by having a composition arrow from FileSystem to Directory.

The fact that FileSystem directly links to Directory does not mean or imply in any way that the other classes in the composite pattern can not be used by FileSystem. The arrow only indicates that the root is a directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.