Geeks With Blogs
Server Error in Application An unhandled exception occurred during the execution of the current web request

 

Association, Aggregation, Composition update - Please read updated article on

Association, Aggregation, Composition

 

When we have only one relationship between objects, that is called Association. Aggregation and Composition both are specialized form of Association. Composition is again specialize form of Aggregation.

           

            Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

 

            Aggregation is a specialize form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let’s take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.

 

            Composition is again specialize form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete. Let’s take another example relationship between Questions and options. Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will automatically delete.

 

In next post I’ll try to define how they are represent in UML.

Feel free to post your comments and I hope your post will helps to understand better for me and as well as others.

 

Related Post:

Association, Aggregation, Composition object relationship

Talent vs Attitude – Harsha Bhogle

What we can learn from Babies

 

Posted on Friday, November 3, 2006 1:40 AM ASP.NET , C# , Web Services , Design Pattern , BizTalk , unhandled exception | Back to top


Comments on this post: Association, Aggregation, Composition

# re: Association, Aggregation, Composition
Requesting Gravatar...
so you mean to say Association is most weak and Composition is most strong relationship.
Left by Jamme on Nov 03, 2006 5:16 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Yes you are right...
Thanks for your comment.
Left by Mahesh on Nov 03, 2006 5:17 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Please provide link as well and also your email so we can talk :)
Left by Mahesh on Nov 06, 2006 8:22 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Hey....
I am waiting for your next post
Left by alloin on Nov 10, 2006 10:50 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
I was going through your article and found it interesting. I also found another interesting article that describes from a different angle
(Following are not my words, got them from another site)

Accosiation:
1. Create a folder called "Links"
2. create a shortcut inside this folder and link it to www.yahoo.com
3. create another shortcut instide this folder and link it to www.google.com
4. Ask your friend to do the same on another machine using same links (www.yahoo.com and www.google.com)
5. Delete the "Links" folder, and open your browser to check if www.yahoo.com and www.google.com still exist or not ;)

Briefly, Association is a relationship where all the objects have different lifecycles. There is no owner.

Aggregation:
1. Create a file called file.txt
2. make a simple application to open the file.txt (rw), but don't program it close the connection.
3. Run an instance of this application (it should work ok and can open the file for rw)
4. Keep the first instance, and run another instance of this application (In theory it should complain that it can't open the file in rw mode because it is already used by other application).
5. Close the 2 instances (make sure you close the connection).

From the above application, we knew that the Application and the file has a separate lifecycles, however this file can be opened only by one application simuletanously (there is only one parent at the same time, however, this parent can move the child to another parent or can make it orphan).


Composition:
1. Open a new Document name it as test.txt
2. Write this sentence in iside this document "This is a composition".
3. Save the document.
4. Now, delete this document.

This is what is called composition, you can't move the sentence "This is a composition" from the document because its lifecycle is linked to the parent (the document)

Left by shank on Nov 13, 2006 2:51 PM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Thanks PM shank for great comment. It also helps me to understand better about composition now I should add one more point in composition; parent object is also responsible for creating the child object. We can not create child object independently. Lets take an example, I am having questions and options. Question will be associate with options. But there is no existence of options without questions. So question object will be responsible for creation and deletion of option object.
Thanks
Mahesh
Left by mahesh on Nov 14, 2006 3:31 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Mahesh,

Great real world examples...
Left by Shank on Nov 14, 2006 11:43 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Thanks Shank...
Left by Mahesh on Nov 14, 2006 12:44 PM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Hi Mahesh,
I got good clarrification from your comments. good real time examples. never i cann't forget this.. really thanks to you

Regards
Amar
Left by amar on May 04, 2007 3:58 PM

# re: Association, Aggregation, Composition
Requesting Gravatar...
These are the best examples of aggregation and composition, Thanks for sharing...
Left by Teja on Jun 21, 2007 4:30 PM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Thanks for giveing such a simple great examples
Left by vignesh on Sep 16, 2007 8:50 PM

# re: Association, Aggregation, Composition
Requesting Gravatar...
There's a lot of theorie on the subject, but what about best practices.

For example consider a class: Group and Skills

Skills can only belong to one Group at a time,
and there can be no empty groups.

At any time you need to be able to list all the Skills in a group
and when you get a Skill you need to be able to determine the group.

You can solve this with an association:

Skill.Group

Or with a Composition/Aggregation

Group.Skills --> which might be List<Group>

1) I have no idea wether this is a composition or
aggregation. And I'm quite sure a Skill can exist without
a Group.

2) Which should I be using for something like this?
On what do I base my choise?


And how would I solve the problems above?
Left by Timothy on Sep 21, 2007 9:40 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Hi
I ve my oomd exam tomoro.It really helped..Thanks n yeah good explanation..
Left by Ela Vohra on Dec 11, 2007 7:55 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Great Example by Shank
Left by anand on Jan 31, 2008 12:34 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Thank you so much my friend
Left by frank on Mar 22, 2008 10:38 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Thanks a lot Shank.
Gr8 Real World Example.
I have lots of confusion with this kind of relationship since 2+ Years.
Now I am 100% clear.
Left by simal on Apr 01, 2008 6:49 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Thanks a lot Mahesh.
Gr8 Explanation of Association, Aggregation, Composition.
I have lots of confusion with this kind of relationships since 2+ Years.
Now I am 100% clear.
Left by simal on Apr 01, 2008 6:53 AM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Thanks a lot for clearing all my confusions regarding these relationships. Another question which I wanted to ask is:- What is the real difference between object model and class diagram? Kindly explain with examples. For example, If we have to make object model of an image editor then how object model will be different from it's class diagram?
Hopeful to get a quick response,
Left by HH on Apr 09, 2008 4:52 PM

# re: Association, Aggregation, Composition
Requesting Gravatar...
Thanks mahesh and shank... i was not clear in these relationships for a long time...now i am clear...
Left by Rakesh on Apr 10, 2008 4:40 PM

Your comment:
 (will show your gravatar)


Copyright © Mahesh Singh | Powered by: GeeksWithBlogs.net