I've run into the following situation multiple times, and in every case have struggled with the naming.
I want a class to force its children to implement a method, but I want the parent class to be able to override this behaviour if needed. For example:
public abstract class ParentClass {
public bool debugOverride = false;
public float GetFloatValue() {
if (debugOverride) return DebugFloatValue();
return GetFloatValueFromChild();
}
float DebugFloatValue() {
float someDebugValuesForTesting = 6; // this would be more in reality
return someDebugValuesForTesting;
}
// How do I name this method?
protected abstract float GetFloatValueFromChild();
}
This way, consuming classes just call GetFloatValue()
. And this class can overwrite its children's behaviour by setting debugOverride
to true
My issue, is that the children classes then have this awfully named method in them that is not super clear, they shouldn't care so much that they are children, they should just worry about figuring out the float value.
Perhaps there's a better pattern here that I'm missing?