Aggregation vs Composition so what is the difference and who cares

Well I care, as part of my job I get to review code and help IT departments with their .net solutions. Lately I have noticed there is quite a bit of confusion over the difference between Composition and Aggregation, so I though time for a post.

Aggregation differs from composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true.

For example, a university has various departments (e.g., IT, Humanities, etc.), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.

I hope this has clarified the difference, and or if you have a better explanation let me know, but saying that it’s not always easy to decide when you should use Composition or Aggregation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s