Question

If I have a class called Location and a class called Maze which has a field which is an array of 'Location' objects as shown below then is this a composition relationship because 'Maze' typically has more than one 'Location' object?

enter image description here

Is this a correct relationship between the 2 classes? or is composition only if it has one object of another class?

Also say if Maze has a constructor which takes an parameter of type Space such as: public Maze(Space space){} then what would be the relationship between Maze and Space?

Was it helpful?

Solution

A good question to ask about composition is "If the parent is destroyed, is the child destroyed". In composition (as opposed to aggregation), the lifecycles are intertwined. In this case, if maze is destroyed, all the locations should be destroyed as well, making it clearly a composition relationship.

OTHER TIPS

This is both a composition and a many to one relationship. Maze has many Locations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top