java - Is using a Observable Singleton Class to handle network calls bad? -


i have develop client/server game uses lot of network request complete task.

both client , server receive multiple command through socket. handle these commands created networkhandler class listens new input on separate thread , allow me send new commands.

these "commands" heterogeneous , and used different classes. (for example "client ready" command used main server class, while "client wants card" used game class).

so created commanddispatcher class listens networkhandler (observable pattern) , dispatch different commands right receivers. (all through interfaces )

the problem every class wants register "command receiver" need call commanddispatcher set listener.

because there multiple class needs reference i'm thinking transform commanddispatcher in singleton class accessed everywhere.

i know singleton , global class bad , i'm hiding dependency , difficult test, alternative see passing commanddispatcher main class other classes.

can me find better solution? thank you.

edit: want clarify app turn based game, don't have large number of parallel request.

this common pattern has been addressed many times in many environments.

the primary issue need address speed of despatch of commands arrive. must ensure no command can clog system otherwise unpredictable response times. achieve must little possible manage it's arrival.

the classic solution network listener minimum amount of work on command , hand off queue. should merely grab arriving message, place in queue , go listening. don't deserialise it, throw @ queue.

at other end of queue there can 1 or more processes pulling commands out, constructing appropriate objects , performing functionality want on them. listeners should listen. happen deserialised object despatched appropriate queue handling may find more appropriate point listen @ other end of queues.

both network listener , command processor can implemented thread pools.

is using observable singleton class handle network calls bad?

bad? no - not stand high throughput. better disassociate network listener command dispatcher.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -