CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2011
    Posts
    1

    To use Classes that extend a base class or not

    I am wondering what is the best practise for doing the following;

    I am going to query an api, and it will return xml similiar to this.

    <EventList>
    <Event Type=A>
    <id>1</id>
    <A_Specific>
    </A_Specific>
    </Event>

    <Event Type=B>
    <id>2</id>
    <B_Specific>
    </B_Specific>
    </Event>

    <Event Type=C>
    <id>3</id>
    <C_Specific>
    </C_Specific>

    </Event>

    </EventList>

    I'll then use this to build an arraylist of objects. Should I;

    1) Create a class with all the above fields in the 1 class of type Event, or
    2) Create an Event base class with id as it's only field then 3 more classes like 'EventTypeC' that extends Event and then add the C_Specific value in this constructor?

    If it's number 2, what would be the best way of taking this information then building an ArrayList<With_Multiple_Object_Types>?

    Thanks in advance for the feedback. I'm assuming people will say it's a personal choice but I guess I'm looking for what is the best approach. I'm learning so would like to learn the right way, but i'm weighing up the extra effort of deciding what object i'm dealing with etc when i decide to scan the arraylist and deal with each event independantly and perform actions based on the event type.

    Thanks again

    Chris

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

    Re: To use Classes that extend a base class or not

    If you have values that are specific to one type and are meaningless to another type then go with approach 2. Or if the behaviour of the types is different so for example you will need different method implementations for each type then again use approach 2.
    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
  •  





Click Here to Expand Forum to Full Width

Featured