sorry if the title is a bit messy but I wasn't sure how to form my question. So I have some classes that I've used for a project that I'm looking to making more general so that I can reuse them for other projects. The idea was to move all code that is unique to the current project to a subclass of the original class. This has worked well for most parts since the most common way a function was made was that the general behaviour was first in the function and the program specific behaviour last. So in my new solution I just call the base class function from the subclass and then do the program specific code or the other way around. For example this:
void ParentClass::doStuff()
{
doGeneralStuff();
doprogramSpecificStuff();
}
Would become this:
void ParentClass::doStuff()
{
doGeneralStuff();
}
void SubClass::doStuff()
{
ParentClass::doStuff();
doProgramSpecificStuff();
}
The problem arises when I have a function that looks like this:
void ParentClass::doOtherStuff()
{
doOtherGeneralStuff();
if (condition) {
doEvenMoreGeneralStuff();
doMoreProgramSpecificStuff();
}
doTheLastGeneralStuff();
}
Now the program specific code is in the middle of the function. One way to solve it would be to just copy the entire function to the subclass and remove the program specific line form the parent. But then I would end up with dublicated code and if I were to change the behaviour of my program down the line that could become a problem. I guess another way to solve it would be to call a function from the parent class that is empty but can be overwritten in a subclass if it wishes to do something at that point of the program. But that also seem kind of bad incase I end up with a lot of sub classes that all needs to do things like that. Then I would end up with a lot of just empty function calls.
Any advice how to solve this?