A GUI design question

Hi, I'm relatively new in OO programming so I hope this isn't a stupid question.

Suppose we have the following interfaces,

publicinterface Driveable{

publicvoid startDriving();

publicvoid stopDriving();

publicvoid turnLeft();

publicvoid turnRight();

}

publicinterface Flyable{

publicvoid takeOff();

publicvoid land();

publicvoid flightLeft();

publicvoid flightRight();

}

and also the following classes

publicclass Carimplements Driveable{

// interface implementation

}

publicclass Planeimplements Flyable{

// interface implementation

}

publicclass FlyingCarimplements Driveable, Flyable{

// interface implementation

}

The last class could implement Harry Potter's flying car :)

(stolen by the way, http://www.guardian.co.uk/uk_news/story/0,3604,1604151,00.html )

Anyway, each of these classes should have a UI with menus like "Start the car" for the Car class and "Take off the plane" for the Plane class. FlyingCar's UI should have the menus of both Car and Plane UIs and what I ask is, can I take advantage of that fact?

I was thinking of having a common UI class for all three cases and pass the object I want to draw as a parameter. But in this case that class would be filled up with "if" or "switch" statements checking the kind of the object that needs to be drawn, which seems a little ugly to me. Is there any other more convinient way so I don't have to write three times the same code? Or if you think the design could be better, please say so.

Thanks in advance.

[2987 byte] By [bill180282a] at [2007-10-2 3:51:36]
# 1
I'd have a JPanel for each interface. Just like the FlyingCar implements both interfaces, have the JPanel for its UI accept the JPanels for each interface as well.%
duffymoa at 2007-7-15 23:07:51 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

This is a good idea but I don't know if it can fully fit in my case.

What if I want to have a JMenuBar that contains all the possible actions. It cannot be added in a JPanel.

Also, the UI would be split into two pieces (in the case of FlyingCar), which is something I'd like to avoid.

bill180282a at 2007-7-15 23:07:51 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

It is a bit the question, what you want your classes to take care of.

Your classes are defining actions till now. They still do not model fuel, altitude, velocity.

But let's say we are talking about the entire real-time object.

Then you would need abstract base classes, that can give java.swing.Action[]. The same would be done for view components.

And in the flying car, you could elect to have a single steering wheel,

by overriding everything appropiately.

Mind you, this is all theoretical.

joop_eggena at 2007-7-15 23:07:51 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

> They still do not model fuel, altitude, velocity.

Yes, I'd like to model those too. Didn't include them for simplicity.

The solution with the abstract base classes seems to be very flexible, but also seems to need a lot of classes.

On the other hand, the "a JPanel for each interface" solution needs only two classes but is much less flexible (if not at all).

I'll think of both solutions and see which one best fits with my problem.

And something else, I think the JMenuBar problem I mentioned above can be solved by having a separate JMenu for each interface.

bill180282a at 2007-7-15 23:07:51 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
Have you menu use the 'instance of' check to see whether an instance of Car or Plane (I forget the actual terms you defined). Enable that menu link based on the type of instance that is encountered. For FlyingCar, this will mean all menu items are enabled.- Saish
Saisha at 2007-7-15 23:07:51 > top of Java-index,Other Topics,Patterns & OO Design...
# 6
> Have you menu use the 'instance of' check to see> whether an instance of Car or PlaneThanks for your reply but this check is what I'd like to avoid.
bill180282a at 2007-7-15 23:07:51 > top of Java-index,Other Topics,Patterns & OO Design...