C++: Dependency injection, circular dependency and callbacks
- by Jonathan
Consider the (highly simplified) following case:
class Dispatcher {
public:
receive() {/*implementation*/}; // callback
}
class CommInterface {
public:
send() = 0; // call
}
class CommA : public CommInterface {
public:
send() {/*implementation*/};
}
Various classes in the system send messages via the dispatcher. The dispatcher uses a comm to send. Once an answer is returned, the comm relays it back to the dispatcher which dispatches it back to the appropriate original sender. Comm is polymorphic and which implementation to choose can be read from a settings file.
Dispatcher has a dependency on the comm in order to send. Comm has a dependency on dispatcher in order to callback. Therefor there's a circular dependency here and I can't seem to implement the dependency injection principle (even after encountering this nice blog post).