0
Відповідь надано

Composition Definition

Martin d'Anjou 4 роки тому оновлено Martin d'Anjou 4 роки тому 4

Is it true to say that in a composition class B does not exist without class A or cannot exist outside of class A (B being contained inside A)?

Відповідь

Відповідь
Відповідь надано

That's a nice catch! Here's a difference, though. In the book, I'm talking about objects—I meant that a component object can't act on its own, so when you create objects you will most likely create both the container and the component. There's no reason to create an instance of a component (make it exist) on its own. But when we're talking about classes, there's a slight distinction that I pointed out in the previous comment. I'll make a note to myself to revise this part in the book to eliminate the confusion.

Розглядається

Hi Martin!


Thanks for a good question. I don't think it's a requirement of the composition. There are certainly cases when both the container and the component don't make sense without each other. But I can also imagine the composition, where the component can be used in several different containers, and it's the container that can't exist without a component.

The confusion for me comes from the first 2 lines on page 24 of "Diving": "the component can only exist as a part of the container".

Відповідь
Відповідь надано

That's a nice catch! Here's a difference, though. In the book, I'm talking about objects—I meant that a component object can't act on its own, so when you create objects you will most likely create both the container and the component. There's no reason to create an instance of a component (make it exist) on its own. But when we're talking about classes, there's a slight distinction that I pointed out in the previous comment. I'll make a note to myself to revise this part in the book to eliminate the confusion.