-1

I have implemented this model with Composite pattern in Java:

enter image description here

I would like to know how I can access to the leafs of these tree using an index getter. For example if I have a List of Element, get the second leaf.

Thanks!

  • This should not be different from a normal tree. To find a leaf element, you would check if the node has any children. If you do get children (as a `List`), you can use `List.get(index)` – Pravin Sonawane Dec 02 '16 at 04:03

1 Answers1

0

In a typical composite pattern, you wouldn't care which is the second leaf etc. You would just call a method, say doSomething(), on the composite object. And it would in turn, call the same method on the children. There can be any number of types of elements. Each would implement doSomething in their own way. If you must find 2nd leaf node, specifically, I could suggest you add a method, say getChildren(), to Element. You could then iterate and use a condition like getChildren().isEmpty(), to reach objects you wish to find.

Sameer
  • 4,379
  • 1
  • 23
  • 23