OOP design question again

  • Thread starter Thread starter Wing Siu
  • Start date Start date
W

Wing Siu

I have a class named Brand
Under the Brand, it may contains several exhibition, and the relationship of
Brand:Exhibition is 1:M

Then I am using this design
class Brand {
ArrayList Exhibitions;
}

But in Exhibition, do I need indicate it's Brand?
For example:
class Exhibition {
Brand brand;
}

Thanks
 
Wing Siu said:
I have a class named Brand
Under the Brand, it may contains several exhibition, and the relationship of
Brand:Exhibition is 1:M

Then I am using this design
class Brand {
ArrayList Exhibitions;
}

But in Exhibition, do I need indicate it's Brand?
For example:
class Exhibition {
Brand brand;
}

That depends - do you need to be able to navigate from a Brand to an
Exhibition in your code? If you don't, it would be gneerally nicer not
to have the bidirectional link - but if it's something you need, you
can certainly do it with no problems.
 
Jon Skeet [C# MVP] a écrit :
That depends - do you need to be able to navigate from a Brand to an
Exhibition in your code? If you don't, it would be gneerally nicer not
to have the bidirectional link - but if it's something you need, you
can certainly do it with no problems.

I have a question about this : is the Garbage Collector able to detect
(and handle) circular references ? If not (which I assume), the code
above will lead to memory "leaks".
If you create and reference a Brand in your application. Assume that
this brand has 1 Exhibition : the GC sees 1 reference to your Exhibition
(from Brand), and 2 references to your Brand (1 from the application, 1
from the Exhibition). If you free your reference to Brand in the
application, you will expect the GC to elect Brand and Exhibition, but
unfortunately there still exists one reference for each, so that they
will not be collected until the end of your application.
The solution is to use a WeakReference (see MSDN) in your Exhibition to
reference the Brand.

Mathieu
 
Mathieu

The GC IS able to detect cirular references and when the application
reference is released, will mark both of the objects for collection. It
starts by assuming that EVERYTHING is garbage and when he can't get a root
reference (since it's been released), will mark the objects for collection
since they are unreachable.

Still not a good idea to code it that way, but the GC WILL handle it.

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT


Mathieu Cartoixa said:
Jon Skeet [C# MVP] a écrit :
That depends - do you need to be able to navigate from a Brand to an
Exhibition in your code? If you don't, it would be gneerally nicer not
to have the bidirectional link - but if it's something you need, you
can certainly do it with no problems.

I have a question about this : is the Garbage Collector able to detect
(and handle) circular references ? If not (which I assume), the code
above will lead to memory "leaks".
If you create and reference a Brand in your application. Assume that
this brand has 1 Exhibition : the GC sees 1 reference to your Exhibition
(from Brand), and 2 references to your Brand (1 from the application, 1
from the Exhibition). If you free your reference to Brand in the
application, you will expect the GC to elect Brand and Exhibition, but
unfortunately there still exists one reference for each, so that they
will not be collected until the end of your application.
The solution is to use a WeakReference (see MSDN) in your Exhibition to
reference the Brand.

Mathieu
 
WhiteWizard said:
Mathieu

The GC IS able to detect cirular references and when the application
reference is released, will mark both of the objects for collection. It
starts by assuming that EVERYTHING is garbage and when he can't get a root
reference (since it's been released), will mark the objects for collection
since they are unreachable.

Still not a good idea to code it that way, but the GC WILL handle it.

Why do you say it's not a good idea to code it that way? If you potentially
need to navigate from an Exhibition to a Brand and vice versa?

Kind Regards,
Allan Ebdrup
 
Wing said:
I have a class named Brand
Under the Brand, it may contains several exhibition, and the relationship of
Brand:Exhibition is 1:M

Then I am using this design
class Brand {
ArrayList Exhibitions;
}

But in Exhibition, do I need indicate it's Brand?
For example:
class Exhibition {
Brand brand;
}

You look in your UML class diagram and see if there are arrows
only one way or both ways.

:-)

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top