For my assignment, I have made a class Robot which has a pointer to a class State. This State class is an interface. The actual implementation of the State child classes needs to be done in a separate DLL.
I've been able to successfully make these classes in the separate DLL, so that the DLL with State's child classes (StateDLL
) only references the DLL that contains the State interface class.
The problem starts with the constructor of the Robot class. I have to let Robot's private variable State*
know what his first childclass will be, for example (where S is of type State*
)
Robot::Robot() {
S = StateDLL::StateChild1::get();
}
Does this mean that Robot needs to know about the StateDLL content? Is there a way to tell him about the childclass in any other way, or does it not matter if I let the RobotDLL reference the StateDLL (with the idea StateDLL can change a lot while the RobotDLL should be more static)?
Your Robot class should not know about the StateDLL at all, preferably it should get an instance of a State*
passed in through the constructor. That will keep the dependencies at a minimum, and allowing for exchangeable implementations, and easy testing.
To prevent the RobotDLL from getting any dependencies from the StateDLL, an interface consisting of a factory function that simply supplies a State*
would be enough to allow the StateDLL to be interchangeable with another DLL that provide the same factory function, or if you later on decides to merge the two dlls the same function can be defined within the RobotDLL.