I have to develop a client/server game that uses a lot of network request to complete its task.
Both the client and the server receive multiple Command through socket. To handle these commands I created a NetworkHandler class that listens for new Input on a separate thread and that allow me to send new commands.
These "commands" are really heterogeneous and and are used by different classes. (For example the "client ready" command is used by the Main server class, while the "client wants a card" is used by the Game class).
So I created a CommandDispatcher class that listens to the NetworkHandler (Observable pattern) and that dispatch the different commands to the right receivers. (All through interfaces )
The problem is that every class that wants to register as a "command receiver" need to call the CommandDispatcher to set itself as a listener.
Because there are multiple class that needs this reference I'm thinking to transform the CommandDispatcher in a singleton class that could be accessed everywhere.
I know that Singleton and global class are bad and that I'm hiding a dependency and it will be difficult to test, but the only alternative I see is passing the CommandDispatcher from the Main Class to all the other classes.
Can you help me find a better solution? Thank you.
Aucun commentaire:
Enregistrer un commentaire