petes1234
March 27th, 2008, 09:48 PM
Hey there, I have seen you posting a lot around the forums and was wandering if you could offer me a little advice.
I have a simple problem and no problem coding it, just trying to work out how best to design the class structure.
Basic system :
There is a building, which has 1 or more floors.
A floor has a name and one or more rooms.
A room has a name and 1 - 5 guards.
A guard has a name and may move between rooms and floors but can only bei n 1 room at a time.
I am trying to come up with a class diagram for this and am having issues with the idea of interfaces/abstract classes.
From that description I get the following information about the classes :
Guard
Variables : name, location
Methods : GetName, SetName, CreateNew, GetLocation, SetLocation, Move
Room
Variables : name, location, contents
Methods :GetName, SetName, CreateNew, GetContents, GetLocation, SetLocation, UpdateContents
Floor
Variables : name, contents
Methods :GetName, SetName, CreateNew, GetContents, GetLocation, SetLocation, UpdateContents
Building
Variables : contents
Methods :GetContents, UpdateContents
From this I now have an issue with the interface/abstract class issue.
I can envision an Abstract Class with data (name) and methods (GetName, SetName, CreateNew) which has subclasses Floor/Room/Guard
I can then see 2 interfaces :
hasLocation : GetLocation, SetLocation
Implemented by Guard/Room
Container : GetContents, UpdateContents
Implemented by Room/Floor/Building
This is all good and fine but im worried about the shared variables (loc, contents) which are not represented (would have to be defined in each relevant class) and surely with the use of interfaces there will be a LOT of repeat identical code between classes (which I always thought was a bad idea)
I would really appreciate hearing your insight on the problem, this problem is going to give me much better understanding of interfaces/abstract classes.
Thankyou in advance.
To radnire: Many consider it inappropriate to ask questions such as these as a private message rather than in the forum, and I am among them. The reasons are several-fold and include:
The problem and solution aren't shared by all which is the main reason for existance of these forums (in my mind)
There's the chance (likely I think in this case) that you have asked the same question to multiple people causing many of us to spin our wheels by re-answer questions that have already been answered. We have lives to live too you know.
There's a chance (quite likely in this case) that this is a homework problem, and that you don't want the teacher to know that you've been asking for solutions to this problem.
So for these and many more reasons, please ask all questions in the forum, not in private.
I have a simple problem and no problem coding it, just trying to work out how best to design the class structure.
Basic system :
There is a building, which has 1 or more floors.
A floor has a name and one or more rooms.
A room has a name and 1 - 5 guards.
A guard has a name and may move between rooms and floors but can only bei n 1 room at a time.
I am trying to come up with a class diagram for this and am having issues with the idea of interfaces/abstract classes.
From that description I get the following information about the classes :
Guard
Variables : name, location
Methods : GetName, SetName, CreateNew, GetLocation, SetLocation, Move
Room
Variables : name, location, contents
Methods :GetName, SetName, CreateNew, GetContents, GetLocation, SetLocation, UpdateContents
Floor
Variables : name, contents
Methods :GetName, SetName, CreateNew, GetContents, GetLocation, SetLocation, UpdateContents
Building
Variables : contents
Methods :GetContents, UpdateContents
From this I now have an issue with the interface/abstract class issue.
I can envision an Abstract Class with data (name) and methods (GetName, SetName, CreateNew) which has subclasses Floor/Room/Guard
I can then see 2 interfaces :
hasLocation : GetLocation, SetLocation
Implemented by Guard/Room
Container : GetContents, UpdateContents
Implemented by Room/Floor/Building
This is all good and fine but im worried about the shared variables (loc, contents) which are not represented (would have to be defined in each relevant class) and surely with the use of interfaces there will be a LOT of repeat identical code between classes (which I always thought was a bad idea)
I would really appreciate hearing your insight on the problem, this problem is going to give me much better understanding of interfaces/abstract classes.
Thankyou in advance.
To radnire: Many consider it inappropriate to ask questions such as these as a private message rather than in the forum, and I am among them. The reasons are several-fold and include:
The problem and solution aren't shared by all which is the main reason for existance of these forums (in my mind)
There's the chance (likely I think in this case) that you have asked the same question to multiple people causing many of us to spin our wheels by re-answer questions that have already been answered. We have lives to live too you know.
There's a chance (quite likely in this case) that this is a homework problem, and that you don't want the teacher to know that you've been asking for solutions to this problem.
So for these and many more reasons, please ask all questions in the forum, not in private.