Lets say I am trying to design a district. I have

         1) house
         2) community (groups of 2 or more then 2 house)
         3) city (group of more then two community)
         4) district (groups of 2 or more then 2 city) 

What I have so far :

class house{
    house(std::string name, std::string id);
    std::string get_name();
    std::string get_id();
   std::string name;
   std::string id;


class community{
      // get house names
     std::vector<std::string> get_all_house_in_community();
     std::string name;
     std::vector<house> p_houses; // vector of house 

class city{
      // get house names by collecting from communities
      std::vector<std::string> get_all_house_in_city();
     std::string name;
     std::vector<community> p_communities; // vector of community

class district{

     // get house names from city
     std::vector<std::string> get_all_house_in_district();
     std::string name;
      std::vector<city> p_cities; 


When the get_all_house_in_district() function is called it has to call all the get_all_housexxxx of the classes. Is it a good idea to distribute data among classes this way or should the district hold all the houses(If I let district hold all the houses then the district class gets really complicated really quickly)?

side question : should I use inheritance here, I don't see any reason but was just curious.

With these classes I wanted to create a simple data base system.

Is there any design pattern to represent this scenario?


1 Answer 1


In general, there are two ways to look at data: logical and physical. In a database, all your houses would be in one table with foreign keys to figure out what the house belonged to. However, that's more of a "physical" layout of how data is stored. It doesn't represent the domain very well.

In Object Oriented Programming, we typically keep the classes behaving as if it were written using the logical model. Internally, they can use whatever optimizations are deemed appropriate. It is best to start with everything modeled logically. You can always change that later.

What you are describing is a compositional hierarchy of data. It makes sense then to use composition to represent the different types of objects. If it helps, you can think of the relationships of inheritance and composition like this:

  • Inheritance: is a relationship (SingleFamilyHouse is a House)
  • Composition: has a relationship, or contains relationship (Community has Houses)
  • Interface (pure virtual class in C++): acts as relationship (Joe acts as Police)

To be honest, composition provides the most reusable code, but there is a place for each approach.

In this approach you'll have to write the get_all_houses_in_district method to call the appropriate methods on it's direct children, and they do the same for their children.

  • what do u mean when u say "appropriate methods on it's direct children" is that similar to what I did when I do get_all_house_in_community(); ..
    – pokche
    Jul 21, 2017 at 3:18
  • Right. You just have to walk down the hierarchy. For each city you call get_all_houses_in_community which in turn will give you the houses to add to your master std::vector<house> etc. Although, you may want to name your methods better. Either change your get_all_houses_*** methods to return a vector of houses or change the name to get_all_house_names_***. It's probably safest to work strictly with the house obejcts. Jul 21, 2017 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.