2

I am reading the book "design patterns: elements of reusable object oriented software. And I am trying to understand the composite pattern. I need help in understanding this paragraph in the case study part of the book

Glyphs like Row that can have children should use Child internally instead of accessing the child data structure directly. That way you won't have to modify operations like Draw that iterate through the children when you change the data structure from, say, an array to a linked list. Similarly, Parent provides a standard interface to the glyph's parent, if any. Glyphs in Lexi store a reference to their parent, and their Parent operation simply returns this reference.

1
1

Glyphs like Row that can have children should use Child internally instead of accessing the child data structure directly.

A composite pattern exposes some operation that when called will be performed on the composite node and any and all sub nodes, and their sub nodes, and their ... you get the idea. This passage is saying that the client using the node (by calling this operation) shouldn't have to deal with traversing sub nodes. It shouldn't even know if there are sub nodes. It should just call the operation and expect the composite node to figure out the rest.

That way you won't have to modify operations like Draw that iterate through the children when you change the data structure from, say, an array to a linked list.

If the client doesn't do the traversing it won't need to change when the traversal needs change. That's important when there are many clients.

Similarly, Parent provides a standard interface to the glyph's parent, if any. Glyphs in Lexi store a reference to their parent, and their Parent operation simply returns this reference.

Parent, however, is making you deal a traversal detail. But at least it's a standardized traversal detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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