I tried searching within questions dedicated to design patterns/data exchange/classes design, to no avail. I am specifically programming in c++, but being this mainly a design problem, I think is quite a general one.
What I am trying to do is designing the data exchange between at least two classes, could be more, as follows:
- One class reads images from disk and shares them
- Arbitrary number of classes (0+) read and process these images independently
The sharing class should not be constrained to the presence of consumer classes. Not being an expert, only options I could think of are either publish-subscribe machinery or using a shared memory.
What are the possible solutions for such a problem, and their pros and cons?
Thank you in advance
You could implement it as a classic producer-consumer pattern. You did not mention whether producers can work from different threads, but I will assume multi-threading capability to make this solution more flexible.
This a very, very beta-version concept, but it should give you an overview. Some notes:
ImageProducerclass which could provide a public accessor to obtain the pointer/reference to it - the choice depends on particular needs.boolflag (e.g.m_processingActive, possibly wrapped instd::atomic<>). This flag would be initialized to true during construction and, after last image is produced, changed to false by the producer. Consumers would end waiting for images when queue becomes inactive.There are probably some additional improvements, some things may be done differently and, possibly, better. But this basic concept is a quite good starting point (I hope).
Of course, you are not limited to a single
ImageConsumerclass. Actual processing function (processImagein my code) could be a virtual function, which is implemented in specialized classes.