lundi 11 novembre 2019

How should I handle transfering data between two unrelated objects without using global variables?

I have been designing a drag and drop system for components in Java and I ran into a design problem that I am unable to solve without using global variables in some form. The only global variable I find acceptable in this case is keeping track of the mouse position.

Whenever I click on a component, I create an object which generates data based on the implementation and "exports" them into the global mouse variable all the while setting the state to "Dragging". On mouse release I find which component the mouse is above and call the DataHandler for which I pass the data for processing.

The issue is that I would want to avoid using "Global" mouse all together since it is against the OOD principles. I took some inspiration from the Java swing's framework TransferHandler. I am not sure how exactly it was handled in the swing framework, I have not managed to find any traces of any global variables in the documentation nor any information about the background functionality on forums I have been browsing or any other stack overflow answers.

That is why I would like to ask whether there is a proprietary method to handle this without globals, or ideally whether someone knows how Swing framework handles transferring in the background.

Aucun commentaire:

Enregistrer un commentaire