I have implemented this model with Composite pattern in Java:
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!
I have implemented this model with Composite pattern in Java:
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!
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.