What is the difference between composition and aggregation?

24,098

Solution 1

Found here

"Both aggregation and composition are special kinds of associations. Aggregation is used to represent ownership or a whole/part relationship, and composition is used to represent an even stronger form of ownership. With composition, we get coincident lifetime of part with the whole. The composite object has sole responsibility for the disposition of its parts in terms of creation and destruction.

Moreover, the multiplicity of the aggregate end may not exceed one; i.e., it is unshared. An object may be part of only one composite at a time. If the composite is destroyed, it must either destroy all its parts or else give responsibility for them to some other object. A composite object can be designed with the knowledge that no other object will destroy its parts.

Composition can be used to model by-value aggregation, which is semantically equivalent to an attribute. In fact, composition was originally called aggregation-by-value in an earlier UML draft, with “normal” aggregation being thought of as aggregation-by-reference. The definitions have changed slightly, but the general ideas still apply. The distinction between aggregation and composition is more of a design concept and is not usually relevant during analysis." John Moore

Solution 2

Consider a student, the student's brain, and the school the student attends.

The brain is a part of the student. If the student is destroyed, so is the brain. This is composition.

The student has a school. The student survives the school's destruction, and vice versa. This is aggregation.

Share:
24,098
ariso
Author by

ariso

I am a senior full stack software developer with more 20+ years development experiences with expertise in J2EE/.Net/Dataware house projects. With ITIL certificate and long time CMMi5, Agile project experience,I am family with both software process and software quality. I am a quick learner, good trouble-shooter and friendly person. Currently I am working as a senior developer in TD Bank Canada ,Toronto downtown office and my previous job is a solution architect in HP GDCC(SH).

Updated on March 19, 2020

Comments

  • ariso
    ariso over 4 years

    What is the difference between composition and aggregation? can anybody give me a sample of this OOAD?