From the definition of composition - the “included (composable) object can exist only as part of the container. If the container is destroyed, then the included object will also be destroyed.

The question is as follows. We have a Family class in which three more classes of Husband, Wife and Child are composited.

enter image description here

  • Can Family exist with an incomplete set of composable classes, or should they all be compulsory (for example, only Husband and Wife or only Wife and Child)?
  • Can an object of some of the composable classes be destroyed before the destruction of the container object, or are the composable objects required to exist as long as the container object exists? To be created not at the time of creation of the container object, but later, for example, when some condition occurs?
  • "Compose" pointers, they can be null. Or implement the pattern NullObject - vp_arth
  • @vp_arth see what the problem is, I’m not so concerned about the implementation issue, I’ll deal with it. Me interests as correctly from the point of view of canonical OOP. We drew a small holivar here, I believe that the container object can exist without composable objects, but my colleague thinks that it does not. - torokhkun
  • canonical OOP / D is not about implementation details, but about interfaces and interaction of objects. And you argue about implementation details. - Mikhail Vaysman
  • So I ask about the interaction of objects. Can there be a container without composites objects? How exactly to do this (via pointers or a null object is already a minor) - torokhkun

1 answer 1

Can Family exist with an incomplete set of composable classes, or should they all be compulsory (for example, only Husband and Wife or only Wife and Child)?

It depends on the logic of your subject area and, accordingly, the code of your application. If the application allows the existence of incomplete families, then of course it can, and if not, then no. But by itself, the use of the composition does not impose any restrictions on it.

Can an object of some of the composable classes be destroyed before the destruction of the container object, or are the composable objects required to exist as long as the container object exists? To be created not at the time of creation of the container object, but later, for example, when some condition occurs?

Here again, the restrictions are only in the subject area (and not in composition). Is it possible to change the family? Is the death of its individual members possible? Will an incomplete family be considered a family in your application?

In the family example, it is more appropriate to use aggregation and not composition. In other words, aggregated objects (family members) should be created outside the object of the aggregate (family). They must be passed to it from outside (constructors, assignment, adding an item to the collection). Accordingly, they can (and should) exist outside the object of the aggregate. But, once again, I need to focus on the subject area and its logic.

Summarizing: in a container, a container controls the life cycle of objects being composted and can create / not create / destroy / dynamically replace them depending on their logic. When the composition "container" can exist without content but the contents (family members in the examples) without the container can not and "die" with it.

  • I read this article. And my colleague read and not only her. Perhaps my example is not quite correct. The point is that he (a colleague) claims that the container object must be created with composable objects, and with the entire set, and they can be destroyed only at the time the container is destroyed, that is, they live exactly as much as he himself. I see the only harsh condition of the composition in the fact that composite objects belong only to the container and must be destroyed when the container is destroyed, but they can be created and destroyed before the container is destroyed. - torokhkun
  • @torokhkun in a dispute with your colleague you are right. In the commentary, you quite clearly explained the essence of the composition (and on the issue, I thought that you did not fully understand). That is, the container manages the life cycle of the objects being composable, and can create / not create / destroy them as you please. - Artur Panteleev
  • correct the answer and I will accept it - torokhkun
  • Corrected. Corrected. - Artur Panteleev
  • one
    @torokhkun your colleague is wrong - rjhdby