Jon Frank wrote:Hello,
I'd love to search for existing solutions to my problem, but I really can't come up with the terminology for a search.
I think I know the answer, but I'm second-guessing myself.
In my mind, I usually create only abstract base classes to tie groups of subclasses together, never using the base class itself.
My concern is when the instantiation of heavyweight subclasses is considered prohibitively expensive,
such as a large amount of member data queried from database.
When that happens the database sends extraneous fields which are simply dropped when subclass is cast to the base class.
If I want to display a list of many items of varying subtypes of same base class, can I instantiate the base class itself?
Are there drawbacks to removing the abstract modifier from a base class?
Jon Frank wrote:
Would I, in that situation, allow Animal to be instantiated by removing its abstract definition, and populate it based on a slimmer ResultSet?
First of all, if all your subclasses do is add additional member variables and you're half treating them as Animal and half as the subclass, then you're not really using inheritance correctly.
Jon Frank wrote:
First of all, if all your subclasses do is add additional member variables and you're half treating them as Animal and half as the subclass, then you're not really using inheritance correctly.
I agree with you there, based on what the example I'm giving you. Mammal is an obvious superclass to human, perhaps weight would have been a better criterion for my example.
My mindset in writing so far has been:
1)Cheaply get a potentially massive list of lightweight summary objects to work with,
2)display to user,
3)based on user's input, get specific objects to do the real work.
A big question from your example:
What is the return type of getSomeAnimalsFromDb(); ?
If it is an array of animals, how am I instantiating the individual abstract Animal elements?
Do i have to determine which animal type from mixed data, then instantiate accordingly?
Something like (with arraylist):
Yes, it would be something like that. There are more sophisticated approaches you could take, using reflection and perhaps a factory class, and again, an ORM tool like Hibernate can do a lot of that automatically for you, but the basic idea is, yes, that you instantiate the appropriate concrete class, populate it from the RS, and add it to the list of animals.
Jon Frank wrote:
Yes, it would be something like that. There are more sophisticated approaches you could take, using reflection and perhaps a factory class, and again, an ORM tool like Hibernate can do a lot of that automatically for you, but the basic idea is, yes, that you instantiate the appropriate concrete class, populate it from the RS, and add it to the list of animals.
Thank you very much, Jeff.
I believe the model I have in place was working, but it was my aim for efficiency that subverted my idea of how a hierarchy should work. I'll have to read more about Hibernate, as I was thinking it was just a quick way to map objects from tables. apparently it is much more.
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|