Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Quoting <a href="http://en.wikipedia.org/wiki/Composite_pattern" rel="nofollow">wikipedia:</a></p> <blockquote> <p>Composite pattern can be used when clients should ignore the difference between compositions of objects and individual objects.</p> </blockquote> <p>Since the idea is to treat a group of objects as a single one (of the same type), usually the composite object iterates through its group (of children) in order to propagate the same behavior. Keeping a list of children in the parent makes more sense from this aspect.</p> <p><strong>Update</strong>: I misunderstood your question. Let me try again: Usually the composite object keeps reference to its children, for the reason above. The parent link might (or not) be excess. Why do you want to keep a reference to the parent? Is there a shared state between the children?</p> <p><strong>Update 2</strong>: Allright, the parent link should be there. In this case, both ways you mention are functionally equivalent and this is essentially an API design question. A TDD-ish way of thinking could help you here. For example, since the scenegraph is essentially a tree, a simple scenario can be creating the first nodes. This is how you'd do it in both versions (as I understand):</p> <pre><code>// First way: // Shorter but still readable. Parent relationship established at construction, so you can count on that the parent property is always at its final state. Composite root = new Composite(null); Composite child = new Composite(root); // Second way: // Has an extra statement but more explicit. Also slightly more flexible since the parent relationship can be established _after_ construction. Composite root = new Composite(); Composite child = new Composite(); root.addChild(child); </code></pre> <p>Maybe re-parenting a node might be another interesting test, if you have that kind of a requirement. Determining your most important operations and designing for them will shape the API in the most appropriate way.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload