Alan Colburn wrote:
> Well, a day later I realized that I can address the issues I'm talking
> about below by simply having one class for the collection (TPeople), and
> have classes with SaveToFile and LoadFromFile methods that derive from
> TPeople. Each derived class could use different ways to persist data ...
Notice how a lot of classes in the VCL have two kinds of saving methods.
One is called SaveToFile, and the other is SaveToStream. The latter is
much more flexible, and once you have it, the former is trivial to write
and doesn't need to be duplicated in any of the descendant classes.
SaveToStream receives a TStream reference as a parameter. The receiver
then calls methods on the stream to save itself there.
SaveToFile is now just a special application of SaveToStream:
procedure TPeople.SaveToFile(const Name: TFileName);
var
fs: TFileStream;
begin
fs := TFileStream.Create(Name, ofCreate);
try
SaveToStream(fs);
finally
fs.Free;
end;
end;
SaveToStream can be virtual so descendants can override it and provide
specialized streaming ability. SaveToFile does not need to be virtual
because the implementation above should be sufficient for all descendants.
SaveToStream is great because right away, your class knows how to save
itself not only to files, but also to memory buffers, compression
algorithms, network connections, registry keys, and anything a TStream
descendant is available for.
LoadFromFile and LoadFromStream work the same way.
--
Rob
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi