BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

newby OO design question

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
David Novo
Guest





PostPosted: Thu Jul 24, 2003 9:28 pm    Post subject: newby OO design question Reply with quote



Hello,

I am trying to convert my application to use more OO oriented approach.
Also, to take advantage of the automatic garbage collection provided by
interfaces. However, in my case, I am having some trouble figuring out how
to best use it.

I have a data analysis application. The datasets can be plotted on
different plots. Different plots can plot multiple datasets. So there is a
one to many relationship between both plots and datasets.

In order to save memory, I only want to load the dataset in memory once.
Thus, if a plot wants to display a dataset, it passes in a filename to the
data library. If the file is loaded, the dataLibrary passes out the
existing internalID for the dataset. If the library is not yet loaded, it
loads it and does the same. The Library is resposible for freeing the
datasets when no more plots are using it.

The way that I implement this is that when a plot requests a dataset, it
recieves and internalID. When it is done (i.e. the user closes the plot, or
changes the file), it returns the InternalId to the library. Whenever it
needs to, it can ask the library to translate the InternalId into a
tDataObject. The annoying thing is I have to be very careful about checking
ID's in and out of the library. Also, InternalID's can exist in undo Items.
So a dataObject is freed if there no more outstanding ID's. The last tricky
thing is that if a plot is the LAST plot holding on to a particular ID,
before it closes it has to check if the dataset has changed, and ask the
user if they want to save.

I would like to have the library give out interfaces (IDataSet) that are
automatically refcounted. This way I can pass them around the program, and
when no one is using them anymore, they are automatically freed. The
problem is, how do I store them in the library to pass the references out.

i.e.
plot passes in a new file name and requests an IDataSet
Library loads datafile into a TDataSet and returns IDataSet
another plot requests the same datafile, it will return another IDataSet
representing the same TDataSet
if both plots close, the IDataSets will decrement to zero and the TDataSet
will automatically be freed

but how will I know that it has been freed to remove it from the list of
available dataSets. Should I create a TInterfacedObject descendant that
exposes an onDestroy event and plug the list into the onDestroy event. Am I
even approaching this the correct way?


Back to top
David Novo
Guest





PostPosted: Fri Jul 25, 2003 2:43 pm    Post subject: Re: newby OO design question Reply with quote



Hi Joanna,

Thanks for the help, however, as I undertand your example, the list (and
any contained datasets) will be freed only at the end of the application.
This is not possible, because uses will cram through hundreds of dataSets
at perhaps 100 Mb each. I need to free the dataset as its used. Also, with
your suggestion, here is how I understand you would add a new dataset:

class function TDatasetFactory.GetDataset(const Filename: string): IDataset;
var
i,j: Integer;
begin
Result := nil;
Assert(_Datasets <> nil, 'Dataset List not available');
for i := 0 to _Datasets.Count - 1 do
if _Datasets[i].Filename = Filename then
begin
result := _Datasets[i]
Break;
end
else begin
j:= _DataSets.add(TDataSet.create(fileName)); // assume the
constructor can take a filename to load a file during creation
result:=_dataSets[j];
end
Assert(Result <> nil, 'Dataset not found');
end;

however, now the newly created dataSet has 2 references. One from the plot
that requested it, and one place in the list. If the plot is freed in the
course of the program, my dataset will not automatically be freed,
defeating the whole advantage of automatic garbage collection.
Back to top
Joanna Carter
Guest





PostPosted: Fri Jul 25, 2003 8:32 pm    Post subject: Re: newby OO design question Reply with quote



David Novo wrote:

Quote:
however, now the newly created dataSet has 2 references. One from the plot
that requested it, and one place in the list. If the plot is freed in the
course of the program, my dataset will not automatically be freed,
defeating the whole advantage of automatic garbage collection.

If you want the dataset only to live for as long as it is in use, then use
the factory method to create an instance into an interface reference without
adding it to any list; when the requestor has finished with it it will be
destroyed automatically.

Joanna

--
Joanna Carter
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.