Title | Notes - Aggregation |
---|---|
Course | Object Oriented Programming |
Institution | Universiti Tun Hussein Onn Malaysia |
Pages | 3 |
File Size | 175.7 KB |
File Type | |
Total Downloads | 112 |
Total Views | 145 |
Download Notes - Aggregation PDF
In Object Oriented Programming, there are many different types of relationships which can exist between two or more classes. The most common two types are:
Inheritance — an “is a” relationship Association — a “has a” relationship
This blog is going to do a deep dive into the two types of Association relationships — Aggregation and Composition. What is an Association relationship? An association relationship between two classes is a “has a” relationship. For example:
A Car has an Engine and a Wheel A Person has a Leg and an Arm A Book has Pages
This usually represents when there are two classes, ClassA and ClassB, and either:
ClassA contains ClassB as an attribute, or Instances of ClassB are constructed inside ClassA
What’s the difference between Aggregation and Composition? There are two sub-types of Association relationships — Aggregation and Composition. What’s the difference between these two? Composition Composition implies that the contained class cannot exist independently of the container. If the container is destroyed, the child is also destroyed. Take for example a Page and a Book. The Page cannot exist without the Book, because the book is composed of Pages. If the Book is destroyed, the Page is also destroyed. Aggregation With an aggregation, the child can exist independently of the parent. So thinking of a Car and an Engine, the Engine doesn’t need to be destroyed when the Car is destroyed.
How are these represented a UML diagram? In a UML diagram, both Aggregation and Composition are represented with a diamond arrow between the classes. The diamond end goes on the side of the container.
Aggregation uses an open diamond Composition uses a closed diamond
Figure 1
Figure 2...