in reply to Advice on OO Program structure & organization

Have a Class method (not a instance method) called (say) loadFrom(), that takes a filename, and returns an array of instances.

Have another Class method (say) findMatching(), that takes a reference to that array and a partially completed instance of that class, and returns one or more matching instances from the array.

use Car; my @cars = Car->loadFrom( 'cars.dat' ); my $partial = Car->new( horsepower => 200 ); my @matches = Car->findMatching( \@cars, $partial ); print $_->details for @matches;

If you move your data from a flatfile to a DB, you pass a DB handle (or credentials) to loadFrom() and everything else still works.

As you add attributes to your class, the search code continues to know how to compare your instances, and your instantiator knows how to construct (partial) instances. The internals can change as needed, with the external inteface remaining the same, except where you need deal with new attributes.


Examine what is said, not who speaks.        The end of an era!
"But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
"Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: Advice on OO Program structure & organization
by exussum0 (Vicar) on Dec 11, 2004 at 13:42 UTC
    I'd do one better than that. Pass it a file handle (glob) instead of the file name. That way, you can have it read from aynthing that's filehandle driven, like a tcp/ip socket. If it's done right, that the class only reads the minimum amount of data FOR a car, you can put multiple cars in one file and initialize multipl objects like that. Or if it makes you happy, mix-and match data in your data stream so that you can read a car, then a person, then a drink, then a cow.. you get my drift.

    ----
    Give me strength for today.. I will not talk it away..
    Just for a moment.. It will burn through the clouds.. and shine down on me.