Software Design and Analysis: Assignment 1 Question 5
Software Design and Analysis: Assignment 1 Question 5
Assignment 1 Question 5
Composition:
Composition is a ‘part-of’ relationship, i.e. the part contained cannot exist without its container.
Considering the same example as above and an additional class for Wards, we can safely assume
that an object of Wards Class cannot exist on its own unless it is contained in a Hospital Class.
Also, the Hospital cannot exist if it does not have any Wards. Therefore, this relationship is
composition.
Implementation:
We make the Wards Objects inside the Hospital Constructor since Hospital owns Wards.
Destruction of Hospital would automatically destroy all the Wards in it too.
Java Code Differences:
In Java, the code for Association in nothing very specific. It is simply using two different objects
(usually in the Main class) and establishing a relationship between them. Aggregation and
composition codes do differ in Java. For example, in Aggregation, we instantiate the two kinds
of objects separately, e.g. Doctors List would be created separately and then this list would be
passed onto the Hospital’s constructor as an argument. Even if the Hospital that has this Doctors
List, is destroyed, the Doctors would still exist separately. On the other hand, in composition, the
Wards objects will be created inside the Hospital Class, i.e. Wards would be a part of the
Hospital Class. Destroying a single Ward would not destroy the Hospital, although destroying
the Hospital would destroy all the Wards as they were instantiated inside Hospital.