Context:
I'm working on an embedded system with many small functional modules. The business logic within each module is made as simple as possible, but those modules generally need to gather bits of information from other places within the system to do their work.
So far, to support unit testing and generally break dependencies among the modules, I've been passing in functions that promise to deliver some value, e.g.:
typedef time_t (*system_time_fn_t)(void);
static time_t s_system_time_fn;
void my_module_init(system_time_fn_t fn) {
s_system_time_fn = fn; // save pointer to system time function
}
int my_module_operation(void) {
time_t now = s_system_time_fn(); // call function to get system time.
... do some stuff ...
return result;
}
The problem
This form of dependency injection works fine for modules that only need a few bits of information from other modules, but sometimes a module really needs to gather many bits of info from other places in the system.
In these cases, passing in lots of functions becomes unwieldy.
Proposed solution
I'm considering a "bulletin board" model, where a module can read inputs from slots in the bulletin board and write its outputs to other slots, perhaps like this:
void my_module_operation(void) {
time_t now = bboard_read(BB_SYSTEM_TIME);
... do some stuff ...
bboard_write(BB_MY_MODULE_RESULT, result);
}
I realize a bulletin board model has the potential for devolving into something as ugly as a zillion global variables, but I promise to be disciplined about its use. And besides, it makes it really clear how to write unit tests for each module.
Big note:
This system is written in pure C: so no virtual functions, no OOP, no thunks, no late binding.
The questions:
Is this a viable approach? What has been your experience with it? Pitfalls? Advantages?