I understand the concept behind Composition ('has a') where the contained class is destroyed upon termination of the container class. Likewise, I get Aggregation which is a 'looser' relationship between two classes, and termination of one object doesn't dispose of the other entirely.
The hard bit, for me, is to actually pick out these relationships in coded form.
Take for example:
class Location { public Troll myTroll; public Cave myCave; public Location() { myTroll = null; myCave = null; } }
Class Location is instantiated in another class called Simulation. It is used as a 2D array of type Location, into which i could have a troll or cave etc.
In the Simulation class, there are constructors for create new Cave and create new Troll. e.g.
private Location[,] Landscape; Landscape[x, y].Cave = new Cave(); Landscape[x, y].Troll = new Troll();
Can one define a relationship between Location and Troll and Cave from the snippet above? I consider that Simulation and Troll and Cave is an example of Composition, but it's Location that's giving me the problem.
My tendency, albeit very amateurish, is to generally look for the instantiation of the class, ie. the new() Keyword, and that would tell me if its Composition. (I know this is an incorrect approach)