C++: Dependency injection, circular dependency and callbacks

Posted by Jonathan on Stack Overflow See other posts from Stack Overflow or by Jonathan
Published on 2011-01-14T07:32:11Z Indexed on 2011/01/14 7:53 UTC
Read the original article Hit count: 455

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).

© Stack Overflow or respective owner

Related posts about c++

Related posts about dependency-injection