I've been creating a Java Swing app which tracks the position of dummy cars and plots them on a map. I'm afraid that the app is getting bigger and I need a sort of split between UI and app logic.
Is it possible to implement an event/listener architecture with different types of events to update the UI when necessary? How?
At present, I manage to update car position in this way:
class car {
setPosition(int x, int y) {
_x = x; _y = y;
carImage.setPosition(x,y);
}
changeImage(enum typeOfImage) {
carImage.changeImage(typeOfImage);
}
}
As you can see I need a UI object inside my logic app, and this breaks the split between UI and logic. In addition, I would like to change the logo of the car whenever the car's state changes and hide or show the image at my convenience. So I think that an observer pattern should be suitable in this scenario.
Thank you
One way to add "events" to any code is to use a listener framework, and a clean one to use is the java.beans Property Change Listener framework. You would add to the model classes a support object and methods that allow addition or removal of property change listener objects. This will allow outside code to be notified of what changes occur and when. For example:
Assuming a simple enum
Best to have the listener return a single object, and so if changing position in one method, have a record to allow this to be returned
Then this can be used in the view as follows