Question

What would be an elegant OOP design for these requirements?

"Design class structure for a building, floors and space. The space can be an apartment, a store or an office. Include any properties, fields and methods you think would be interesting to have."

I've tried this but i think it's not a good solution:

space {
    floor {
    }
    building {
        floor{
        }   
    }
}
Was it helpful?

Solution

Here's some pseduo C++ to get you started. I didn't provide any public members - just structures with relationships to get you started.

As for the issue of "a space can belong in more than one building". That's an exercise, I'll leave up to you.

class Building;
class Space;
class Floor;

class Building
{
   int number_of_floors;
   vector<Floor*> _floors;  // list of floors in the building

   // building specific data
   string name;
   string address;
};

class Floor
{
   Building* pBuilding; // building for which this floor belongs
   int floor_number;    // the floor number (e.g 1-99)
   vector<Space*>; // list of spaces on this floor
}; 


class Space
{
public:
    enum SpaceType
    {
        Apartment,
        Store,
        Office
    };

private:
    SpaceType _spacetype;
    Floor* pFloor;   // floor in which this space exists on
};

OTHER TIPS

Let's analyze the requirements.

You have 3 objects; building, floor, and space.

A building can have one or more floors.

A floor can have one or more spaces.

You should be able to develop objects and their relationships from this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top