Search code examples
c#oopinheritancepolymorphism

OOD, inheritance, and Layer Supertype


I have a question concerning holding common code in a base class and having the derived class call it, even though the derived class's trigger method has been dispatched from the base. So, base->derived->base type call stack.

Is the following look OK, or does it smell? I have numbered the flow steps...

public abstract class LayerSuperType
{
  public void DoSomething()  // 1) Initial call from client
  {
    ImplementThis(); // 2) Polymorphic dispatch
  }

  protected abstract void ImplementThis();

  protected void SomeCommonMethodToSaveOnDuplication(string key)  // 4)
  {
    Configuration config = GetConfiguration(key);
  }
}

public class DerivedOne : LayerSuperType
{
  protected virtual void ImplementThis() // 2)
  {
    SomeCommonMethodToSaveOnDuplication("whatever");  // 3) Call method in base
  }
}

public class DerivedTwo : LayerSuperType
{
  protected virtual void ImplementThis() // 2)
  {
    SomeCommonMethodToSaveOnDuplication("something else"); // 3) Call method in base
  }
}

Solution

  • Looks like a very simplified Template Method Pattern where your sub-classes do some specific kinds of things at the right points in the implementation of your algorithm, but the overall flow is directed by a method on the base class. You've also provided some services to your sub-classes in the form of base class methods; that's ok too as long as you're good as far as SOLID goes.