1

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.

asked Mar 6, 2012 at 19:29
2
  • 1
    Not 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. Commented 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 sense Commented Sep 1, 2012 at 7:29

1 Answer 1

2

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.

answered Mar 6, 2012 at 19:38
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah hmmm and what about say for the wall class when I instantiate say 4 walls for one room in the wall array list. Each wall might be a different size?. Can you have factory methods for those? like createroomType1 walls and then inside of that method instantiate the four walls that the room has?
Whatever is common to all rooms, such as Wall instantiation, put into the Room constructor.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.