I am having a bit of trouble with the design aspect of my JAVA program, there a couple of ways in which I have thought of doing, but I dont know which way is best, or if there is a better way to do it?. Below is an example of one way
<< ABSTRACT >>
Rooms class
extends extends extends
Room TYPE U Connector X Connector U
AGGREGATE walls - into each room type
The reason why I am getting a bit confused is that the 3 different types of rooms that I am using only differ in there property values(height,width etc..), but all have the same properties. Does this warrant creating a new class for EACH room type?.
Or should I do it the other way which is having the one rooms class and instantiating it three times for each room type and just changing its properties via setters and getters?.
because I would have to set each rooms properties and its aggregated wall properties which could get quite long!.
Any help is greatly appreciated.
-
1Not sure if it's possible to answer in a meaningful way with the info given. If they're only different in size, it totally depends on how you want to identify/use them. It also sounds like you should have a ctor that takes dimensions rather than instantiating-then-setting.Dave Newton– Dave Newton2012年03月06日 19:32:14 +00:00Commented Mar 6, 2012 at 19:32
-
You really need to explain your problem with more details if you want a good answer. Your question, as it stands, is almost impossible to understand. You should start by defining the problem : "Consider a house that has many rooms. Each room has different dimensions and different number of walls... I have come up with the following design and..". I hope this makes senseChetan Kinger– Chetan Kinger2012年09月01日 07:29:38 +00:00Commented Sep 1, 2012 at 7:29
1 Answer 1
If the rooms only differ by their property values, consider using a factory-style pattern. Here's a simple example:
class Room {
// room properties
int x, y, z;
// constructor taking properties
public Room (int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
// factory methods
public static Room createRoomType1() {
return new Room(100, 200, 300);
}
public static Room createRoomType2() {
return new Room(400, 500, 600);
}
}
The factory methods are like "preset" constructors for different types of rooms. This strikes a balance between repetition/property setting, and having too many classes.
2 Comments
Wall instantiation, put into the Room constructor.