Reputation: 455
I have implemented a Async TCP Server using TcpListener class. (Async means, it is non-blocking). It accepts multiple client connections , read data as it comes and provides callback. The messages that are received by server are essentially commands that need to be parsed and action needs to be taken on the data storage structures. Lets call this server as,
Class MyTcpServer (with ReadCallback)
and the storage structures are packaged as
Class MyDatabase
( with Add, Delete, Search methods).
This is a multi-threaded application and this database will eventually be exposed to rest of the system as well.
My question is what design approach should I take to interface MyTcpServer and MyDatabase?
Should I simply create a singlton called Class MsgProcessor
. This class will have methods to process the incoming messages and manipulate data into MyDatabase
. These methods can then be invoked on any ReadCallback()
on MyTcpServer
Or is there a better design pattern?
Upvotes: 1
Views: 275
Reputation: 4524
I think you'd be interested in using a Command Pattern in a situation like this.
Disclaimer: This is what came off the top of my head after reading your problem statement and doing a very brief wiki search to make sure I wasn't completely off my rocker.
Upvotes: 0
Reputation: 6516
You should have a MessageHandler class that holds a MyDatabase and a MyTcpServer instance and is subscribed to its read callback.
In addition, the MessageHandler should implement some sort of queue or pool mechanism for invoking the actions in a managed manner.
Then, each time a read callback is invoked, the MessageHandler will first parse and store it in the queue and later processed.
No need for a singleton here.
Upvotes: 1