Extend generic class along with imported data
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Extend generic class along with imported data

  1. #1
    Join Date
    Feb 2011
    Posts
    1

    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
    Attached Images Attached Images  

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center