February 22nd, 2011, 11:49 PM
Extend generic class along with imported data
I'm not exactly sure if the terminology I'm going to use is correct so bare with me.
I have a generic equation and I want to create two specified instances. However I have data that is loaded and I want to be able to access it from both specified instances. I don't want to make a copy of the data for every new instance of the equation, I want to inherit it . I was thinking of creating a generic class and extending it but that doesn't solve the issue of loading the data. Does anyone have suggestions on how to accomplish this?
I've attached a diagram of what I'm talking about
February 23rd, 2011, 06:40 AM
Re: Extend generic class along with imported data
You need to use inheritance for the equation classes and composition for the data class, as per your diagram. Where you have gone wrong is in thinking you want to inherit the data.
If the data is loaded into an object then you can pass that object to any instance of the equation class you instantiate and the data will be available to it. You can pass the data object in either via a constructor or a method.
Be careful though, if the equation objects modify the data in your data object then you can't use the same data object for multiple equation classes unless the idea is to perform a chained series of calculation where the output of one equation forms the input for the next equation. Often the best approach to take is to make the data class immutable and if any data needs to be changed the object that does the transform creates a new immutable data object containing the transformed data. A good example of this approach is the Java String class.
Click Here to Expand Forum to Full Width