0
已回答

Composition Definition

Martin 2 月 前 更新人: Martin d'Anjou 2 月 前 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.

UserEcho 的客户支持