Skip to content

Instantly share code, notes, and snippets.

@basus
Created March 15, 2023 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basus/16496a699b78b511a412ec0032d6d3d7 to your computer and use it in GitHub Desktop.
Save basus/16496a699b78b511a412ec0032d6d3d7 to your computer and use it in GitHub Desktop.
Avoiding adding a default method to the subclass
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();
}
}
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