I have the class hierarchy:
class AbstractProcess
{
virtual void Do() = 0;
};
class BuildProcess : public AbstractProcess
{
virtual void Do();
};
class UpdateProcess : public AbstractProcess
{
virtual void Do();
};
However, I now want to introduce 2 (4?) new subclasses as a process can also be manual or automated. The following looks to be too long winded:
class ManualBuildProcess : public BuildProcess
{
virtual void Do();
};
class ManualUpdateProcess : public UpdateProcess
{
virtual void Do();
};
class AutomatedBuildProcess : public BuildProcess
{
virtual void Do();
};
class AutomatedUpdateProcess : public UpdateProcess
{
virtual void Do();
};
This will only get worse if I want to introduce additional subclasses of either Build/Update or Manual/Automated processes.
Is there a better design?
I can suggest two approaches.
One is to make your
Manual...Process
templated:Secondly, have two hierarchies and prefer composition over inheritance, something like
or vice-versa (
Operator
holding a pointer to aProcess
), or even in a symmetrical way withOperator
s andProcess
es not knowing about each other, but with a third class holding a pointer toOperator
and a pointer toProcess
.Of course, this all applies only if there is some code common for all
Manual
cases and some code common for allBuild
cases, etc. If each of these version have its own code, then you obviously have no choice.