Aggregates & associations

Bruce Adams tortoise_74 at yeah.who.co.uk
Tue Dec 18 16:15:25 PST 2007


Hi,
    A lot of the recent debate surrounding garbage collection and to a  
lesser extent const has got me wondering about
aggregation, association and attribution in (garbage collected) languages  
like D and Java.
In object oriented modelling there is a clear distinction between  
association and aggregation. An aggregate is
part of the object. If the object is destroyed it should be destroyed as  
well. An association is just a reference to
another object. How are these two concepts distinguished in D or Java?
Even in C++ you can't express them directly. I find myself labelling  
pointers as "owned" or "not-owned".

In java:

class Foo;

class Bar
{
    Foo aggregate;
    Foo association;
};

In C++:

class Foo;

class Bar
{
   Foo aggregate;
   Foo* association
};

or

class Bar2
{
   ~bar2()
   {
     delete aggregate;
   }

   // owned
   Foo* aggregate;

   // not owned
   Foo* association
};

It seems to me that in D the correct idiom is:

class Bar
{
    Foo aggregate;
    Foo* association;
}

and specifically that:

class Bar
{
    Foo association;
}

should be considered an error.
How do you label associations and aggregations? Is there any modern  
language that lets you represent these distinct concepts directly?

Also I have just realised I have a fundermental gap in my understanding of  
very basic D.
When I declare:

class Foo
{
    int x;
    int y;
}

class Bar
{
    Foo aggregate;
}

What is the memory layout of the object I am actually declaring?
Is it the C++ equivalent of:

class Bar
{
    Foo aggregate;    // i.e. { int x, int y }
};

or

class Bar
{
    Foo* aggregate;
};

If I was being more low-level than I need to be most of time these days  
the memory layout matters. How do you choose
which kind of aggregation (has-a versus part-of) is implemented?
I'm amazed I never noticed this before but semantically its not a problem  
because an aggregate is an aggregate.
Could someone please enlighten me - my brain isn't screwed in today?




More information about the Digitalmars-d mailing list