"A composite is an agregate with the additional constraints that an object may be part of only one composite and that the composite object has responsability for the disposition of all its parts, that is, for their creation and destruction".
Originally posted by Ken Blair:
Is this still composition?
Originally posted by Ilja Preuss:
As far as I can tell, it is *more* than composition. Composition, as defined by UML (and as explained by Uncle Bob) doesn't hold an object from being exposed to the outside. In fact it doesn't even require that it is created and destroyed by the same parent object, as far as I know.
A form of agregation with strong ownership and coincident lifetime of parts by the whole. A part may belong only to one composite. Parts with non-fixed multiplicity may be created after the composite itselt. But once created, they live and die with it (that is, they share lifetimes). Such parts can also be explicitly removed before the death of the composite. Composition may be recursive.
There is a strong form of aggregation called composition. A composite is an agregate association with the additional constraints that an object may be part of only one composite at a time and that the composite object has sole responsibility for the disposition of all its parts. As a consequence of the first constraint, the set of all composition relationships (over all associations with the composition property) forms a forest of trees made of objects and composition links. A composited part may not be shared by two composite objects. This accords with the normal intuition of physical composition parts (one part cannot be a direct part of two objects, although it can indirectly be part of multiple objects at different levels of granularity in the tree).
By having responsibility for the disposition of its parts, we mean that the composite is responsible for their creation and destruction. In implementation terms, it is responsible fro their memory allocation. During its instantiation, a composite must ensure that all its parts have been instantiated and correctly attached to it. It can create a part itself or it can assume responsibility for an existing part. But during the life of the composite, not other object can have responsibility for it. This means that the behavior for a composite class can be designed with knowledge that no other class will destroy or deallocate the parts. A composite may add additional parts during its life (if multiplicieties permit), provided it assumes sole responsibility for them. It may remove parts, provided the multiplicities permit and responsibilites for them is assumed by another object. If the composition is destroyed, it must either destroy all its parts or else give responsibility for them to other objects.
This definition of composition works well with garbage collection. If the composite itself is destroyed, the only pointer to the part is destroyed and the part becomes inaccessible and subject to garbage collection.
An object may be part of only one composite at a time. This does not preclude a class from being a composite part of more than one class at different times or in different instances, but only one composition link must exist at one time for one object.
Originally posted by Edwin Dalorzo:
I hope this helps to set this clear.
Not really. For example, "but only one composition link must exist at one time for one object" doesn't say whether there can be non-composition links of an object beside the one composition link.