What I need

I need a suitable and scalable solution to manage data flow between the classes in my C++ program.

Why I need it

Upon new feature requests, I'm continually adding new classes to the program. I initially registered callback functions to classes and injected a dependency to the class via an interface:

Callback Sequence

Now, as the program scales, this is becoming quite difficult to maintain and keep track of where and when my data is flowing between class instances.

My Proposed solution

I've now redesigned the system to use an Observer pattern. I have a central event hub (Observable) to which I can subscribe my various services (Observers) to:

UML Design

Note: I've also implemented this as an example on OneCompiler and the code is also available as a gist.

I can also pass the event hub to the classes if they will also need to push notifications.

The new data flow will now be:

New data flow Sequence

What I want to know

Before I embark on a refactoring mission I would like to know:

  • Is the observer pattern as I've implemented it in the example, a scalable solution to manage the data flow between my classes?
  • If not, what is a better alternative to ensure I can pass data between different sections of my program on an event-driven basis?

Thanks in advance for the help.

  • Seems almost like you're re-creating signals and slots. Apr 13, 2021 at 17:20
  • That kind of depends on the problem you are solving. For example if this was an iterative simulation, an ECS would be better. If this was a a data transformation line, i'd look at pipe and filter architecture.
    – Kain0_0
    Apr 14, 2021 at 4:03
  • Thanks for the inputs. I can address both comments simultaneously. This is for a simulation with a web user interface and a couple of socket connections with other systems. I'm trying to figure out how to share data and data changes between sub-levels of high-level class abstractions. Currently, I'm aiming for the signals and slots idea i.e. observer pattern and I pass interfaces down the hierarchy of both lanes for readback purposes. But, I'm going to have a look at both ECS and the Pipe and Filter thanks. Apr 14, 2021 at 7:28
  • The EventHub that you've made is often called Mediator. I am not sure why/how the new solution must be better. If the problem was the apparent lack of a hierarchy in the data flow, then you should probably focus on just that, by enforcing layers and their separation. Apr 14, 2021 at 12:19
  • @ShadowsInRain thanks for pointing out the Mediator. It looks like it will do the job and fit in better than the Observer for my use-case, thanks! I'm going to go with your advice for "enforcing layers and their separation" together with the Mediator design pattern to solve my problem. Apr 14, 2021 at 19:14


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.