Wednesday, February 02, 2011

Difference between Association, Aggregation and Composition

This has been a haunting question to me for some time. I had been trying to get a concrete understanding on differences among these three terms. If you google on differences among these three terms, you will get all sorts of explanation. I was tempted to link couple of those explanations here but that would have just increased their relevancy in google’s eyes (so I won’t do that).

If you google for Aggregation, you will find innumerous guys (in-fact Wikipedia) out there who would define aggregation for you like this:

Aggregation differs from ordinary 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 owns various departments (e.g., chemistry), 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.

Isn’t this definition useless? If my cell phone “has a” camera, without cell phone this camera is useless on its own. Camera might be atomic unit on its own however without the parent object it does not reveal its behavior. People try to define this same scenario in other words that camera continues to have its existence and might be fully functional within another cell phone. This is where I have problem. I see this that once parent object is not in existence, contained object has lost its value.

Gang of Four in their book “Design Patterns Elements of Reusable Object Oriented” has handled it pretty good and in a clear way:

Aggregation implies that one object owns or is responsible for another object. Generally we speak of an object having or being part of another object. Aggregation implies that an aggregate object and its owner have identical lifetimes.

This is what we know as composition. They go further and try to define association:

Acquaintance implies that an object merely knows of another object. Sometimes acquaintance is called "association" or the "using" relationship. Acquainted objects may request operations of each other, but they aren't responsible for each other. Acquaintance is a weaker relationship than aggregation and suggests much looser coupling between objects.

This is fine. In association we are not talking about containing each other and controlling the life time. All we are doing is requesting the operations i.e File.IsExists or Path.Combine.

On Composition vs Aggregation, in his book “UML Distilled, third edition”, Martin Fowler says:

Composition is a good way of showing properties that own by value, properties to value objects, or properties that have a strong and somewhat exclusive ownership of particular other components. Aggregation is strictly meaningless; as a result, I recommend that you ignore it in your own diagrams.

And when Martin Fowler is discouraging us to use Aggregation, he meant messed up Aggregation.

Hence forward my stand on Aggregation and Composition will be the union of what GoF says and what Martin Fowler says. GoF calls Composition by the name of Aggregation, so I say that Aggregation and Composition are the same things where there is a “has a” relationship and lifetime is identical for Owner and contained object (Aggregator and Aggregatee). Going forward there are only two things for me, Association and Composition (which is also called Aggregation by GoF).

2 comments:

Anonymous said...

Hmm does not sound convincing .... flower said it is bad but didn't say aggregation does not exist

Pradeep Gururani said...

He did say, "It is strictly meaningless".