Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Tolls

Thread: Best Object Oriented approach for building model objects?

  1. #1
    mnvsriram is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Best Object Oriented approach for building model objects?

    Hi,
    I have a query regarding how the value objects( model objects) should be designed?

    Let us say I have Country, State, District, City and I need to build the model objects for these.

    My country Model object (CountryVO.java)will have
    Java Code:
    private int countryId;
    private String CountryName;
    //other details
    My State Model object (StateVO.java)will be having
    Case 1:
    Java Code:
    private int stateId;
    private String stateName
    private int countryId;
    private String countryName;
    case 2:
    Java Code:
    private int stateId;
    private String stateName
    private CountryVo countryVO;
    Problems with Case 1:
    Disadvantage:
    So, for the City object (Country->State->District->City)there will be lot of variables in the model object

    Java Code:
    private int cityId;
    private int cityName;
    private int disctrictId;
    private int districtName;
    private int stateId;
    private String stateName
    private int countryId;
    private int countryName;
    also, if we want more details in the (like one variable for number of people in all these) we need to add one for each(like populationInCity,populationInDistrict,populationIn State,populationInCountry) in the city model object.

    Advanteges: looks good while retrieving the details

    Java Code:
    cityVo.getVCountryId();


    Case 2:
    Advantage:
    The cityVo will have district reference, district will have state reference, state will have country reference.

    so the city Model object will look like

    Java Code:
    private int cityId;
    private String cityName;
    private DistrictVO districtVO;
    I feel this is neat when compared to the case 1.

    Disadvantage:

    While retriecing the details, we need to do multiple level of delegation

    to get the countryName from the cityModelObject

    Java Code:
    cityVo.getDistrictVO().getStateVO().getCountryVO().getCountryName();

    Can anyone please help me in finding the best OO way(either case 1 or case 2) to build these kind of model objects?
    Last edited by mnvsriram; 05-09-2012 at 09:19 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Best Object Oriented approach for building model objects?

    2.
    No two ways about it.

    With 1 you'd have to maintain the data across dozens and dozens of objects.
    In addition, think of the functionality held in Country. Would you duplicate that across State, District and City?
    Maintenance nightmare.

    A chunk of OO is having Objects control their state and functionality.
    Option 2 would have every object control the state of the Country object.
    Look up encapsulation.
    Sierra likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    mnvsriram is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: Best Object Oriented approach for building model objects?

    Quote Originally Posted by Tolls View Post
    2.
    No two ways about it.

    With 1 you'd have to maintain the data across dozens and dozens of objects.
    In addition, think of the functionality held in Country. Would you duplicate that across State, District and City?
    Maintenance nightmare.

    A chunk of OO is having Objects control their state and functionality.
    Option 2 would have every object control the state of the Country object.
    Look up encapsulation.
    Thanks for your reply.

    2 looks a better option even to me as it is clean and follows OO principle and also re-usability.But I get the below 2 thoughts before implementing that way.

    a) Memory - Let us say each of Country,State,District have 20 variables(int or String).Once if you create the CityObject, the memory allocated for that city Object will be equal to the memory of 80 variables.
    b) Is the Delegation that I explained in my previous post perfectly fine? Is it good to do some thign like cityVo.getDistrictVO().getStateVO().getCountryVO() .getCountryName() to get the country name?


    Even thought we have both the above problems, you feel 2 is better approach than 1?

    Can you please share your thoughts on these points?

  4. #4
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: Best Object Oriented approach for building model objects?


  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Best Object Oriented approach for building model objects?

    a) is not true. In the example you give in State, in option (1) you'd be using 4 bytes for the int and 4 bytes (for 32 bit machines) for the String reference for each class that you wanted to duplicate the values for...and that's for only 2 variables. Whereas the reference to Country will occupy 4 bytes, no matter how much data there was in it.
    b) yes. It's used all the time.

    (1) is an appalling concept. Ask yourself how you would maintain the data between the objects. Ask yourself how you would maintain, say, Country-specific logic in the non-Country classes.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Best Object Oriented approach for building model objects?

    As a different approach, would it not make more sense to make the references go both ways? ie a country is a group of states, which are groups of districts, etc. States have to know their country and their districts. Unfortunately this does have something of the data maintenance issue that Tolls mentioned above.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Best Object Oriented approach for building model objects?

    See the cross post at OTN.
    That suggestion was pointed out there.

    For something like that (and it's quite common as well), you need to decide who has ownership, so either Country or State would control which Country a State belonged to.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Building website according to the MVC model
    By jprog in forum Advanced Java
    Replies: 6
    Last Post: 05-14-2011, 08:26 AM
  2. New to object-oriented
    By fluteattack in forum New To Java
    Replies: 6
    Last Post: 03-28-2011, 01:43 PM
  3. ArrayList with Objects the right approach?
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 09:26 AM
  4. Object Oriented
    By GraemeH in forum New To Java
    Replies: 3
    Last Post: 03-29-2009, 07:50 PM
  5. Opinions on best approach for objects
    By spikey in forum Advanced Java
    Replies: 1
    Last Post: 04-11-2008, 05:20 PM

Posting Permissions

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