Program - User Interface

How is this supposted to be connected with good programming practise on a class-diagram level?

Say I have a board game, say tic tac toe. All my logic are in the classes Board, Player and Move. I also need a GameController class that controls the game. Now I want to have an UI for this game. Say a Text User Interface is enough. I want to be able to enter commands and view the game.

The design should be pretty generic and say that in the future I would like to write a GUI instead of the TUI. This should also handle commands and show the game.

How would I design this? Should I make an Interface: UserInterface that all other interfaces must implement? And what classes should know others? Somebody told me that the GUI should never know (have a reference to) the logic classes... or was it the other way around? .. for some reason.

Very grateful for help!

[890 byte] By [_Yonder_a] at [2007-10-2 10:16:38]
# 1

You're on the right track: for your logic code to be independent from the GUI, use interfaces. You logic code would then just fire general events to the (GUI-)Listeners, e.g. "moved player1 from A to B". Your TUI would simple create such a text output and your GUI would do some fancy 2D/3D graphics. On the other side, your TUI/GUI needs to know the game logic because the user is issuing the commands. As there can only be 1 logic, it's a good idea that all of your listeners (TUI/GUI/FUI, ... ;-) know the central controller class (that itself knows the logic classes). But your game logic shoudl not know about the listeners.

Hint: use WeakReferences to store your Listeners to avoid MemoryLeaks, because listeners could basically be any clients that could also be running independ from the logic VM and can crash and therefore can't unregister (= exit). E.g. if you would like to have mutliple clients that can just register to the core logic.

MartinHilperta at 2007-7-13 1:42:11 > top of Java-index,Other Topics,Patterns & OO Design...