2

In Composite Design Pattern, from its recursive nature, I might end up with an endless loop and create a cycle. How can I enforce some kind of validity-checks in my code to secure that I haven't created any cycles when calling the operation method?

SDJ
  • 4,083
  • 1
  • 17
  • 35

1 Answers1

1

In the general sense cycle avoidance is not specific to the Composite pattern. There is nothing about this concern in the Gang of Four book, for example. It's really up to the client code to ensure the composite structure is acyclic.

This being said, in practice for the Composite pattern, it's commonly not necessary to share Composite objects. So a constraint that's much easier to check is to prevent the addition of a Component that's a composite and already contained in the structure.

SDJ
  • 4,083
  • 1
  • 17
  • 35