4 Replies Latest reply on May 10, 2012 10:05 PM by jschellSomeoneStoleMyAlias

    Best Object Oriented approach for building model objects?

    936025
      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
      private int countryId;
      private String CountryName;
      //other details

      My State Model object (StateVO.java)will be having
      Case 1:
      private int stateId;
      private String stateName
      private int countryId;
      private String countryName;


      case 2:
      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

      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

      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

      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

      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?
        • 1. Re: Best Object Oriented approach for building model objects?
          TPD-Opitz
          Welcome to the forum.

          I'd prefer approach 2. This is because is also provides possibility for (let's say) a country to know all its states:
          class Country {
            private final Set<State> states = new HashSet<>();
            void addState(State newState) { states.add(newState);}
          }
          
          class State {
             private final Country iAmIn;
             State(Country county){
                iAmIn= county;
                iAmIn.addState(this);
            }
          }
          user10850960 wrote:While retriecing the details, we need to do multiple level of delegation
          to get the countryName from the cityModelObject

          <tt>cityVo.getDistrictVO().getStateVO().getCountryVO().getCountryName(); </tt>
          This violates a design rule (don't talk to strangers).
          If you need to get the country name from a city you should have a method <tt>getCountryName()</tt> in your City class which delegates it to a <tt>getCountryName()</tt> method of the State object it holds which in turn delegates to its countyVO...
          This way you could swich both approaches behind the scenes without changing the code using your cityVo for getting the country name.

          bye
          TPD

          Edited by: T.PD on 09.05.2012 11:31
          • 2. Re: Best Object Oriented approach for building model objects?
            876530
            Cross-posted: http://www.java-forums.org/new-java/59475-best-object-oriented-approach-building-model-objects.html
            • 3. Re: Best Object Oriented approach for building model objects?
              dadams07
              Whichever way you go, I would strongly recommend against using primitive data types for internal variables.
              • 4. Re: Best Object Oriented approach for building model objects?
                jschellSomeoneStoleMyAlias
                user10850960 wrote:
                Hi,
                I have a query regarding how the value objects( model objects) should be designed?
                ...
                Problems with Case 1:
                ...
                So, for the City object (Country->State->District->City)there will be lot of variables in the model object
                That isn't the problem.

                The problem is that you are creating a data model based on code rather than creating it based on business needs and design needs.

                Consider this as one possible business problem - what is the name of Taiwan and is it a country? If you think the answer is easy then you need to do more research on the subject.

                As for a design problem, say you want to display a list of countries in a GUI, which a user picks and then you must pick a list of "states" associated with that country - why would that data object need the name of the country?

                Other than that I would guess that neither of your objects is likely to fit any business/design needs. The following however MIGHT fit the need of some application
                 .
                      Country
                         int Id
                         String Name;
                
                       ProvinceState
                          int CountryId
                          int Id
                          int Name
                
                       City
                          int CountryId
                          int ProvinceStateId;   // This can be 'undefined'
                          int Id
                          int Name