1.I would like to know if the following structure is incorrect, what is the reason, and what is the solution for that:
Assume I have implemented a client for net game
The client has 2 main packages:
A.GUI - hold all the swing Jpanels etc.
B.LogicEngine
In Logic engine I have a class called clientThread which main goal is to communicate with the server to get commands to execute on the Gui Panel and also to send information back as a result of the user choices on the Gui Panels..
2.In order to do so I tend to hold reference of my main Gui panel in clientThread and vise versa, is it so wrong to do cyclic reference between two classes of different projects?
3.Is it wrong in matter of object oriented programming to execute things to be shown on the Gui from within class like client thread which is responsible in some way to manage the flow of the game although it is on logical engine package?
4.Also if the Gui part know and uses the logical part is it a problem?
Would like to hear some advices
Thank you very much
Obviously the GUI should depend on the engine, not the other way around (and, god forbid, they should not depend on each other).
Your problem is actually pretty common and simple to solve. The engine thread should allow client code to install a listener that will be notified every time something happens. Than GUI implements that listener and installs it. Note that the game logic engine is only aware of the listener interface, not the particular implementation that lies in your GUUI package.
This is an implementation of the Observer pattern and it has several advantages:
Finally there is nothing wrong with manipulating your GUI from logic thread, hoever you must be aware of event dispatching thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With