Created
March 15, 2023 01:03
-
-
Save basus/16496a699b78b511a412ec0032d6d3d7 to your computer and use it in GitHub Desktop.
Avoiding adding a default method to the subclass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SuperClass { | |
void defaultDummyMethod() { | |
// This is the no-op dummy method that will be overriden in some child class | |
doNothing(); | |
} | |
void actualMethod() { | |
// This has lots of gnarly code, plus a call to the dummy method, | |
// even though this class doesn't need to call the dummy method. | |
// Lots of gnarly code | |
defaultDummyMethod(); | |
} | |
} | |
class SubClass : public superClass { | |
void defaultDummyMethod() { | |
// This overrides the dummy method | |
doTheExtraThing(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SuperClass { | |
public void actualMethod() { | |
// This is the publicly exposed method | |
// A client would call this method, that just delegates to another method | |
gnarlyCode(); | |
} | |
protected void gnarlyCode() { | |
// This is only visible to this class and its children | |
// Lots of gnarly code | |
} | |
} | |
class SubClass : public superClass { | |
public void actualMethod() { | |
// This overrides the parent method, also delegates functionality | |
// to the inherited gnarlyCode() and adds a call to the stuff only | |
// this subclass needs to do | |
gnarlyCode(); | |
doTheExtraThing(); | |
} | |
private void doTheExtraThing() { | |
// The stuff that only this subclass needs to do. | |
doStuff(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment