0

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)

1
  • 1
    Does make sense an empty location? (No troll, no cave). If Location can exist without them, then seems to me It's aggregation. Otherwise is composition.
    – Laiv
    Oct 18, 2016 at 6:31

1 Answer 1

4

The problem in your example is the class itself. It implies that each location has a troll and a cave, which is surely incorrect. As a result, you (and anyone else) would struggle to determine the relationship, since the starting point itself is misleading. Coming back to a more formal description, whether we have a full Has-A ownership (composition) or Uses-A`Belongs-to` association (aggregation), neither work here.

A better design would have cave inherit from location (Is-A), and troll have a reference to a location (Has-A) - which is aggregation.

4
  • Only at location[x,y]. Numerous locations on the 2D array will be left empty therefore. Oct 18, 2016 at 0:26
  • Sorry, din't quite get your comment. If you mean that not all locations have caves or trolls, and stand empty, then it's exactly what I explained in my answer.
    – RomanK
    Oct 18, 2016 at 1:35
  • The program I have is over 800 lines, and so I think I would need to provide you with more than the snippet given in the question. I tried to present minimal code, but realise this isn't enough. I don't think I can upload 800 lines of code...can I? Oct 18, 2016 at 7:47
  • Even if you can, I probably won't analyze 800 lines of code (maybe someone else would). However, even with your updated text my answer stands correct: if not every location has a cave and a troll, then the class design seems wrong.
    – RomanK
    Oct 18, 2016 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.