Association → Whenever two object are related with each other the relationship is called association between objects.
According to wiki
“Association represents the static relationship shared among the objects of two classes.”
So any relationship between object of two classes is called association.
Example - “University offers courses” where university have association with courses.
Aggregation → Aggregation is specialized form of association. In aggregation objects have their own life-cycle but there is ownership and child object can not belongs to another parent object. But this is only an ownership not the life-cycle control of child control through parent object.
According to wiki
aggregation is an association that represents a part-whole or part-of relationship.
Most of the time aggregation used when a class is a collection or container of other classes, but where the contained classes do not have a strong life cycle dependency on the container—essentially, if the container is destroyed, its contents are not.
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 life-cycle. Both can create and delete independently.
Composition → Composition is again specialize form of aggregation and we can call this as a “life and death” relationship. It is a strong type of aggregation. Child object dose not have their life-cycle 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.
Another example; think about relationship between questions and options. Single questions can have multiple options and an option can not belong to multiple questions. If we delete questions options will automatically delete.
According to wiki
“Composition is a way to combine simple objects or data types into more complex ones”
To remember composition you can think of “has a” relationship between parent object and child object.
Look at a bicycle as a object which is a composition of wheel, handle and other building block of bicycle. So bicycle “has a” composite type of wheel.
The basic difference between aggregation and composition is the life cycle of object.
During implementation of composition parent object is also responsible for creating the child object. We can not create child object independently. If 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.
Some other examples from different article (Following are not my words, got them from another site)
1. Create a folder called “Links”.
2. create a shortcut inside this folder and link it to www.yahoo.com
3. create another shortcut inside 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 life cycles. There is no owner.
1. Create a file called file.txt
2. make a simple application to open the file.txt, but don’t close the connection.
3. Run an instance of this application (it should work OK and can open the same file)
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 read-write 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 life-cycles, however this file can be opened only by one application simultaneously (there is only one parent at the same time, however, this parent can move the child to another parent or can make it orphan).
1. Open a new Document name it as test.txt
2. Write this sentence in inside 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 life-cycle is linked to the parent (the document)
Book recommendation → Code Complete is an excellent book which I Recommend to every programmer. If you don’t have please have it.