-
May 9th, 2012, 02:26 AM
#1
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
Code:
private int countryId;
private String CountryName;
//other details
My State Model object (StateVO.java)will be having
Case 1:
Code:
private int stateId;
private String stateName
private int countryId;
private String countryName;
case 2:
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
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,populationInState,populationInCountry) in the city model object.
Advanteges: looks good while retrieving the details
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
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
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?
-
May 12th, 2012, 05:20 AM
#2
Re: Best Object Oriented approach for building model objects?
There is also a third OO way, ie by creating a class hierarchy whereby City extends District, which extends State, which extends Country.
There are other potential advantages to using case 2 over case 1 such as if you make the objects immutable you only need to create one object for each discreet entity and that object can be shared with each higher level instance. For example if you had to create objects for 5 cities in 3 different districts in 2 different states of the same country, you could do it with 1 country object, 2 state objects, 3 district objects and 5 city objects.
Then, if the relationship of parent to child is linked in both directions and you are modeling every city in a country you can do things like ask a district/state/country how many cities in contains, or a country how many states/districts/cities it has etc.
While retriecing the details, we need to do multiple level of delegation
Not necessarily, you can always add shortcut getXXX methods in the higher level class that just asks the lower level (parent) class for the info.
Can anyone please help me in finding the best OO way
That really depends on what you are planning on using it for but in this sort of scenario I generally tend to favour composition (case 2) over inheritance (case 3) and would definitely avoid duplication (case 1).
-
May 12th, 2012, 06:24 AM
#3
Re: Best Object Oriented approach for building model objects?
Originally Posted by keang
There is also a third OO way, ie by creating a class hierarchy whereby City extends District, which extends State, which extends Country.
I wouldn't recommend inheritance be used like that because these are natural part-whole composition relationships. For example a State is not a Country - a State is part of a Country.
One OO design pattern in special targets such relationships and that's Composite.
Last edited by nuzzle; May 12th, 2012 at 06:55 AM.
-
May 12th, 2012, 07:06 AM
#4
Re: Best Object Oriented approach for building model objects?
I wouldn't recommend inheritance be used like that because these are natural part-whole composition relationships. For example a State is not a Country - a State is part of a Country.
I agree, that's why I went on to say I would favour composition and why I also pointed out other advantages of using composition. I was just offering it up as another case to look at.
-
May 12th, 2012, 10:27 AM
#5
Re: Best Object Oriented approach for building model objects?
Originally Posted by keang
I agree, that's why I went on to say I would favour composition and why I also pointed out other advantages of using composition. I was just offering it up as another case to look at.
Sure, as long as it's clear that you don't avoid case 3 because you favour composition but because inheritance models the wrong relationship. To model that a City is-a Country is bad in itself and unrelated to whether you favour composition as a language feature or not.
It's important to distinguish between the two different usages of the inheritance-composition pair. It's used both to denote two language features as well as two modelling relationships. They're often confused.
Last edited by nuzzle; May 12th, 2012 at 10:47 AM.
-
May 12th, 2012, 12:23 PM
#6
Re: Best Object Oriented approach for building model objects?
To model that a City is-a Country is bad in itself
Agreed, it's is not a sound relationship to model. It clearly fails the is-a-kind-of test and should not be modeled using inheritance.
The OP was looking at possible designs and weighing up the pro and cons and so I mentioned inheritance as something that should also be considered. The fact that it should also be discarded is irrelevant at this stage, the OP is just looking at the possible options.
and unrelated to whether you favour composition as a language feature or not.
Where did I ever say that? Admittedly what I said wasn't as clear as it might have been but in no way does it imply that.
-
May 13th, 2012, 01:03 AM
#7
Re: Best Object Oriented approach for building model objects?
Originally Posted by keang
Where did I ever say that? Admittedly what I said wasn't as clear as it might have been but in no way does it imply that.
You said this,
"I generally tend to favour composition (case 2) over inheritance (case 3) "
You introduced case 3 as a possible alternative for the OP to use. I'm pointing out that case 3 more than anything is an example of bad OO.
And that's why it shouldn't be used. Not because one prefers composition over inheritance but because inheritance as used in case 3 doesn't model the intended relationship here.
I leave it at that now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|