Composition vs. Aggregation

Composition

Instance A keeps instance B as a member. B’s only purpose and capability is to serve for A. It’s irrelevant in any other context. B was probably created because A had become too complex and trying solve too any problems.

Aggregation

Instance A keeps instance B as a member again. But B could serve its purpose in another class as well.

Why would the differentiation matter?

For many thing it could matter. One thing I just notice having the distinction in mind would be useful is that: If A and B has composition relationship and you want to have Unit Tests for A, you don’t have to isolate B.
Why? [I will write it later.]

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments