I have the following circular dependency that I'd like to eliminate. I think I must have a flaw in my design. And I'd much appreciate any feedback on how to fix this.
My circular dependency comes from 3 important classes. Worker
, WorkingContext
, and TaskManager
.
First, I have 2 Worker
classes (Worker
, and WorkerGroup
). Each Worker
is constructed with a WorkingContext
object that stores state info.
class Worker { Worker(WorkingContext * context); }; class WorkerGroup { };
Next, I have WorkingContext
.
class WorkingContext { // state info and various getters ... TaskManager * getTaskManager(); };
As shown, one of the things the WorkingContext
stores is a TaskManager
, which has a number of useful functions that depend on Worker
. Here is a snippet
class TaskManager { void assignCredit(Worker * worker, WorkerGroup * group, int credit); void revokeCredit(Worker * worker, int credit); };
The circular dependency is that Worker.h
needs to #include "WorkingContext.h"
, which needs to #include "TaskManager.h"
, which in turns needs to #include "Worker.h"
.
Right now it works since I'm using forward reference, but that really is a last resort.