views:

159

answers:

4

Hi all. I'm working on a small java game which is a sort of clone of the 1995 game Theme Hospital. I've recently been working on the GUI and the Level structure now I have the path finding and grid working. My current way of "building stuff" consists of one room and one reception desk, and it works ok so far, but I need to implement a way to add different types of rooms and items. Here is my current code on the button press for the creation of the object (http://snipt.org/lUm).

I'm sure there must be a better way to do this! My implementation is poor, I know, but I have only recently began to understand how to use abstract classes and interfaces.

Actually setting the properties of the room or item, I currently have no issue with. I am thinking ahead, but I love how I get different opinions on the best way to do things here on Stack. At ideas on how such a thing should be achieved? Any examples of this sort of thing? I would search it, but I'm not really sure what to search for, as I don't if what I'm doing has an exact name.

I'm sorry if I haven't been too clear exactly what I'm talking about. I will gladly answer any further questions on this, and or upload more sections of code as needed or requested.

Thanks in advance for your time and thoughts!

+3  A: 

Check out the factory pattern. The factory is a means of creating objects by providing parameters, and getting objects back that adhere to a common interface. The implementation, however, is determined by the factory. The calling code does not need to know this.

By encapsulating the object construction in another object (the factory), the mechanics of selecting the particular object to build is abstracted away from the calling code. You can easily provide further subclasses at a later date by only modifying the factory itself.

So in your example code, the buildMe() method is a form of factory. It takes some parameter specifying what's required to be built - a Room or a ReceptionDesk. Both these would implement the same interface (a HospitalComponent?) and the calling code would then place that component within the hospital. You can add further subclasses of HospitalComponent to the factory (an OperatingTheatre?) and the calling code doesn't have to change.

It's worth investigating design patterns (in this scenario, check out this list of creational patterns) to understand different ways of using objects to solve common problems, and for how to communicate your solutions with other people. The Gang-of-Four book is the bible for this subject.

Brian Agnew
this looks interesting. I shall have to read it when i can keep two eyes fully open! :)
Relequestual
Hum. Looking at the java example on the first link to the factory patten, this looks exactly like what I need to be doing.Going to keep the question open for a few more days to see if anyone has anything else to say. This does seem like the answer though.
Relequestual
Sudden though when looking at the example, doing it that way, rather than a check price class, for the room, one of the methods would be canEnter, to check if a person can or cannot enter :) woop!
Relequestual
Yes - you should rarely have to explicitly check class types if your class abstraction and polymorphism is correct.
Brian Agnew
I will need to do checking when a person has to go to a type of room, as all the rooms are stored in one array for easy processing. I believe I just need to check to see if it an instance of a class. Do say if I'm wrong on this.
Relequestual
I think you're better off having methods such as isPersonAcceptable(Person p) on the base class and override as required for different subclasses. You shouldn't really need to have type-checking.
Brian Agnew
I see your point, but I still do need type checking, as all the rooms are stored in one array, and then each room is checked to see if it is the room the patient is looking for before they go to it. with regards the person being acceptable, I was thinking in terms of only doctors in some rooms and only nurses in some rooms ect.
Relequestual
+1  A: 

Polymorphism might be overkill for what you are doing.

Personally, I would just have a Room class, with a table of the values for each building (max size, cost per tile etc.). Then, when you build a new room, get the matching table entry, and create your Room object with the details from the table.

This may not be the best practice, and it probably goes against Java conventions (I came to Java from dynamic languages) but in terms of lines of code that need changing to make a new room, it's the lowest I've found.

Macha
I considered this. One thing I struggled with thinking about is do I need a class for every item and or room, and I figured out yes, because some have methods that others don't, although I'm still not sure.What do you mean by LOC? I'm still rather new to programming :)
Relequestual
LOC = lines of code
Jake
ah ok, thanks.Further to this, I know I will need a set of items that can be built per room, which can be done with an array of numbers I think.Any examples of where using a single "room" like class with a table of values for each type? cheers
Relequestual
A: 

What you have now is one class with a bunch of static functions. It isn't very OOP--the class is just a way to group all the functions in one file.

If you go the OOP route, you'd want a Room base class, then OperatingRoom, ReceptionistDesk, Bathroom, Office (for Staff), MRIRoom, WaitingRoom, and maybe even Hall would be child classes of Room.

public class Room {
    protected int width, length;  //how many squares wide/long the room is

    protected int x, y;  //where it is on the gride

    protected float buildingProgress;  //how far construction has come

    protected bool isReady;  //is the building ready for use?

    protected Person occupants[];  //some list/array of people currently in the room

    protected Person resident;  //the person 'in charge' in the room--the receptionist, manager, MRI technician, etc etc.
}
Jake
Yeh, that was what I was going for. I have done so for Items and the reception desk. GridObject->Item->UsableItem->ReceptionDesk. The first 3 being abstract classes now.I decided that a reception desk would be classed as a usable item, so it has a point of usage. I haven't started to expand the Room class yet, as the "room" has not really be made different from any other room. I believe I am able to expand with new classes as I need to. My question was mainly to do with how I should construct the rooms in the first place, as I'm new to the concept of abstract classes.
Relequestual
Although what you say is right, I have already got a Room class. I misunderstood what you were saying. I did not paste the Room class, as my question was focused on how to build different rooms and or items with regards to polymorphism, which suggests I already have a class structure sorted.
Relequestual
A: 

I'd think carefully before modeling this problem with polymorphism.

The question is: what fundamentally different behavior will each type of Room exhibit? What's common between all of them?

Polymorphism isn't always the answer. Composition can sometimes keep things more flexible. A data driven solution, as recommended earlier, might be best of all.

duffymo
That is indeed the question. Realistically, I'm not exactly sure yet, however, I'd like to leave as much room to expand for different functions in different rooms as possible.The reason I chose polymorphism is because of my previous question (http://stackoverflow.com/questions/736081/would-this-be-the-correct-place-to-use-the-java-keyword-interface), however it may be the case this is needed for items and not rooms.What I can think of now, is rooms will have different staff allowed in them, different items that are built in them, and some rooms will be allowed lots of people, like toilets
Relequestual
Polymorphism and composition are orthogonal, aren't they ? Composition is related to how you construct objects, whereas polymorphism relates to overriding behaviours. I'm not sure I understand your last paragraph.
Brian Agnew
You are correct. I have only just learnt how to use polymorphism, so I still know very little. Oh sorry, my last paragraph, I was just thinking about the things that would be different between different rooms, which followed on from the answer's question. I think I have assigned the common properties and methods in the Room class
Relequestual
@Relequestal - I was addressing duffymo and "Polymorphism isn't always the answer. Composition can sometimes keep things more flexible"...!
Brian Agnew
ah ok. my bad. Yeh I just assumed that something was said that went over my head, as this is the first time I've used polymorphism, only learnt what it meant a few days ago! lol
Relequestual