Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
  }
}

1 Answers

That looks absolutely fine. Perfect example of why you'd use an abstract class over an interface. It's a bit like a strategy pattern and I have used this fairly regularly and successfully.

Make sure that what the class doing is still dealing with one 'concern' though, only doing one task. If your base class does repository access but the objects are representing documents, don't put the functionality in the base class, use a separate repository pattern/object.

like image 149
Kieren Johnstone Avatar answered Aug 17 '26 07:08

Kieren Johnstone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!