I'm developing a program which does communication to different types of devices (with respective protocols). It should concurrently acquire messages from devices and write them to a file with specific format. Timing is very strict, therefore it's decided to be implemented as 2 threads - one getting messages from device and the second - writing them. The common object for both threads is a message queue.
There is a thread
DeviceHandler, which starts acquisition thread
DeviceDriver. In my opinion,
messageQueue object should belong to
DeviceHandler and is redundant to be at
DeviceDriver and increases coupling. I decided to pass it to
acquire method which should start this thread, but I don't know how to combine these both without making
messageQueue an instance variable.
Moreover, since getting messages from devices is a big task, implementors of
DeviceDriver use their own message requestors, which in turn should somehow get queue and actually fill it by messages. The same problem here. Also, due to implementation details,
DeviceBDriverRequestor might be run as a thread.
So I have questions:
- Am I right avoiding making
messageQueuean instance variable of DeviceDriver? Or is it acceptable to have it everywhere?
- What would be a good way to implement
DeviceDriverclass (combination of
acquire(Queue messageQueue)methods) in a way I proposed?
- Are there maybe some good practices in dealing such situations?
Thank you in advance!