wood burning stoves 2.0*
The moose likes Java in General and the fly likes Best Object Oriented approach for building model objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Best Object Oriented approach for building model objects?" Watch "Best Object Oriented approach for building model objects?" New topic
Author

Best Object Oriented approach for building model objects?

Mandy Ram
Greenhorn

Joined: May 09, 2012
Posts: 16
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

My State Model object (StateVO.java)will be having
Case 1:


case 2:

Problems with Case 1:
Disadvantage:
So, for the City object (Country->State->District->City)there will be lot of variables in the model object

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,populationInState,populationInCountry) in the city model object.

Advanteges: looks good while retrieving the details




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

I feel this is neat when compared to the case 1.

Disadvantage:

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

to get the countryName from the cityModelObject



Can anyone please help me in finding the best OO way(either case 1 or case 2) to build these kind of model objects?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Definitely not your first approach. In that case you've got information that's specifically about one class in another class, which is breaking the principles of high cohesion and low coupling.

The one exception I'd make to this comment is if you decided your requirements were simple enough that you didn't need all the extra model objects, and you were happy with a simple City (or even Address) class containing the city name, state name, country name etc (in that case I don't think you'd need IDs for all these). But if you want to be able to treat them as separate entities then the second approach is the way to go. And personally I wouldn't find the method chaining too much of a problem because I think it's still pretty readable as long as the methods are well named. I'd probably strip it down to:
For example, I think the "Country" in "getCountryName" is redundant because you're calling it on a CountryVO object.

If you found that you were writing that a lot, I suppose you could still add getState() and getCountry() methods to CityVO which delegate behind the scenes, so you'd just need cityVo.getCountry().getName(). But I doubt I'd bother.
Mandy Ram
Greenhorn

Joined: May 09, 2012
Posts: 16
Matthew Brown wrote:Definitely not your first approach. In that case you've got information that's specifically about one class in another class, which is breaking the principles of high cohesion and low coupling.

The one exception I'd make to this comment is if you decided your requirements were simple enough that you didn't need all the extra model objects, and you were happy with a simple City (or even Address) class containing the city name, state name, country name etc (in that case I don't think you'd need IDs for all these). But if you want to be able to treat them as separate entities then the second approach is the way to go. And personally I wouldn't find the method chaining too much of a problem because I think it's still pretty readable as long as the methods are well named. I'd probably strip it down to:
For example, I think the "Country" in "getCountryName" is redundant because you're calling it on a CountryVO object.

If you found that you were writing that a lot, I suppose you could still add getState() and getCountry() methods to CityVO which delegate behind the scenes, so you'd just need cityVo.getCountry().getName(). But I doubt I'd bother.


Thanks Mathew for your reply.

I would like to stress on the exceptional case that you mentioned.

Your explanation helped me to understand when the country object have just 2 variables. But,in case if country contains 15 variables along with countryId and countryName and if I want to use only the id and name in the state object, then is it better to go with the first approach?

If we go with the 1st approach in this scenario, arent we breaking the OO principle?

If we go with the 2nd approach, when the state object is created, memory for 30 variables will be allocated( 15 variables for country + 15 vairables for State) and it gets worse when creating the City object (memory for about 60 variables will be allocated)

So, what do you think is the best approach in this scenario?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Adding more variables to state or country would make me even more likely to take the second approach, because it's making it clearer that "country" is an important concept in its own right.

I wouldn't even begin to worry about the amount of memory. If you need the variable you need them anyway regardless of which class they're in. So it's still only an extra three references. If you're that worried about memory you probably shouldn't be using Java in the first place .
Mandy Ram
Greenhorn

Joined: May 09, 2012
Posts: 16
Matthew Brown wrote:Adding more variables to state or country would make me even more likely to take the second approach, because it's making it clearer that "country" is an important concept in its own right.

I wouldn't even begin to worry about the amount of memory. If you need the variable you need them anywaym regardless of which class they're in. So it's still only an extra three references. If you're that worried about memory you probably shouldn't be using Java in the first place .


I am clear.Thank you.
 
Consider Paul's rocket mass heater.
 
subject: Best Object Oriented approach for building model objects?
 
Similar Threads
Not able to insert data
How to display a list box with values from an "Array List" in "Struts2"
Selected value getting lost on form Submit
Ajax and alert problem
Rich Faces 4.0 Autocomplete