Is it possible to differentiate between composition and aggregation relationship by reading the source code?
I tried to find some patterns and I have listed them below.
I am taking the example from this site just to explain what I assume to be a pattern
COMPOSITION
public class Engine
{
. . .
}
public class Car
{
Engine e = new Engine();
.......
}
AGGREGATION
public class Address
{
. . .
}
public class Person
{
private Address address;
public Person(Address address)
{
this.address = address;
}
. . .
}
I find these patterns to differentiate
COMPOSITION (is a part of )
Defined as a field of a class.
Instantiated and assigned within the class.
Aggregation (has a)
Defined as a field of a class
Instatiated out side the class
Assigned in the constructor by sending the instance as a argument to the constructor.
CAN I CONSIDER THESE TO DIFFERENTIATE AGGREGATION AND COMPOSITION RELATION?
ARE THERE MORE CONSTRAINTS THAT ARE USED TO DIFFERENTIATE?
Not really, because there's not a unique way to implement each kind of association (in fact, the problem is that we have three kinds of associations, "normal associations", "aggregations" and "compositions").
If the language has pointers then you could try to guess that if Engine is defined in Car as a pointer then the programmer that wrote that piece of code is suggesting a softer relationship (aggregation or association) between Cars and Engines since removing the Car does not imply losing the Engine object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With