I need advice on class design
-
I'm at the level in my programming experience where I understand algorithms and functions well, but I'm having a hard time conceptualizing classes and how they interact. Here's my problem: I've got three classes: System, Room, and User. I need to dynamically instantiate multiple Rooms and multiple Users from the System class. Of course, accessing a particular Room or User from the System is easy enough, if I know the name, but I also need to access members of the System class from my Room classes, as well as members of the User classes from my Room classes. So, my dilemma is twofold. First, how do I instantiate Room or Player classes dynamically; that is, how can I dynamically define the names of those classes as they are instantiated? Second, since a class can only access the public methods and properties of classes instantiated within it, how in the world do I handle the communication between my classes, both queries from Room to System and queries from Room to User? Or am I trying to bend unbreakable rules and need to go back to the drawing board? I appreciate any insights.
-
I'm at the level in my programming experience where I understand algorithms and functions well, but I'm having a hard time conceptualizing classes and how they interact. Here's my problem: I've got three classes: System, Room, and User. I need to dynamically instantiate multiple Rooms and multiple Users from the System class. Of course, accessing a particular Room or User from the System is easy enough, if I know the name, but I also need to access members of the System class from my Room classes, as well as members of the User classes from my Room classes. So, my dilemma is twofold. First, how do I instantiate Room or Player classes dynamically; that is, how can I dynamically define the names of those classes as they are instantiated? Second, since a class can only access the public methods and properties of classes instantiated within it, how in the world do I handle the communication between my classes, both queries from Room to System and queries from Room to User? Or am I trying to bend unbreakable rules and need to go back to the drawing board? I appreciate any insights.
tantiboh wrote: First, how do I instantiate Room or Player classes dynamically; that is, how can I dynamically define the names of those classes as they are instantiated? You can't define the name of the variable that way, but why would you ? Store a name in the class instance, take it in the constructor. tantiboh wrote: Second, since a class can only access the public methods and properties of classes instantiated within it, how in the world do I handle the communication between my classes, both queries from Room to System and queries from Room to User? In the absence of multiple inheritance, you can use interfaces here. Define an interface that describes the information you need to get in common from all three class types ( such as name ), and one for any stuff you need from each class seperately. You may want to also write Fascade classes, for example a RoomFascade class would be used to provide methods that help you work with a Room, but would bloat the Room class if they were all put in there. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
-
tantiboh wrote: First, how do I instantiate Room or Player classes dynamically; that is, how can I dynamically define the names of those classes as they are instantiated? You can't define the name of the variable that way, but why would you ? Store a name in the class instance, take it in the constructor. tantiboh wrote: Second, since a class can only access the public methods and properties of classes instantiated within it, how in the world do I handle the communication between my classes, both queries from Room to System and queries from Room to User? In the absence of multiple inheritance, you can use interfaces here. Define an interface that describes the information you need to get in common from all three class types ( such as name ), and one for any stuff you need from each class seperately. You may want to also write Fascade classes, for example a RoomFascade class would be used to provide methods that help you work with a Room, but would bloat the Room class if they were all put in there. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer
Christian Graus wrote: You can't define the name of the variable that way, but why would you ? Store a name in the class instance, take it in the constructor. Interesting. Tell me if I'm off base here: let's say I instantiate Player and give its Name property as "xyz". I can make that Player part of a hashtable off its Name property, and anytime I need to get to it, I can get the hash for its Name property and I've got it. So, if that's all well and good, then I've got a dozen players, all neatly placed into a hashtable that is part of the System class. That still leaves the problem of how to refer to, say, Player.Name(xyz) from the Room class, as that player was instantiated under System. Christian Graus wrote: In the absence of multiple inheritance, you can use interfaces here. I'll research interfaces more, but the little I've absorbed so far tells me that it should allow me to access my System class from any Room class. If that's the case, then I should be able to access the Player hashtable in the System class from the Room class, giving me the Player class' "name." However, at that point, I'm stuck. How do I actually use that name to talk to that Player class from my Room class, since it wasn't instantiated there? Thanks for your help.
-
Christian Graus wrote: You can't define the name of the variable that way, but why would you ? Store a name in the class instance, take it in the constructor. Interesting. Tell me if I'm off base here: let's say I instantiate Player and give its Name property as "xyz". I can make that Player part of a hashtable off its Name property, and anytime I need to get to it, I can get the hash for its Name property and I've got it. So, if that's all well and good, then I've got a dozen players, all neatly placed into a hashtable that is part of the System class. That still leaves the problem of how to refer to, say, Player.Name(xyz) from the Room class, as that player was instantiated under System. Christian Graus wrote: In the absence of multiple inheritance, you can use interfaces here. I'll research interfaces more, but the little I've absorbed so far tells me that it should allow me to access my System class from any Room class. If that's the case, then I should be able to access the Player hashtable in the System class from the Room class, giving me the Player class' "name." However, at that point, I'm stuck. How do I actually use that name to talk to that Player class from my Room class, since it wasn't instantiated there? Thanks for your help.
That still leaves the problem of how to refer to, say, Player.Name(xyz) from the Room class, as that player was instantiated under System. Make the room class keep a reference to the system object that created it, or just keep a reference to ur player hashtable. U can pass this reference through the room's constructor.
public Room(System owner){...}
Everytime u need to get some player from a room object u can get it through your System object that "owns" the room and hass all the necessary information. -
That still leaves the problem of how to refer to, say, Player.Name(xyz) from the Room class, as that player was instantiated under System. Make the room class keep a reference to the system object that created it, or just keep a reference to ur player hashtable. U can pass this reference through the room's constructor.
public Room(System owner){...}
Everytime u need to get some player from a room object u can get it through your System object that "owns" the room and hass all the necessary information.