Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different Inheritance-chains without breaking the DRY-principle

I have a problem with using the right inheritance-chain without loosing good parts of the DRY-principle.

Given the following structure:

  • abstract class A is my base for all classes
  • abstract class B : A is my base for all classes that can have special features (made available through B)

Now, I need to have two new classes which share the same features. But one is of type A whilst the other one is of type B. (This can not be changed!) Both classes should have a method SetSize().

So at the end there would be class C:A and D:B, both to have the same SetSize method.

Question: How would I create a base-class, intermediate layer to have the SetSize()-method only declared/implemented once (DRY)? I guess something about using interfaces or some static helper-classes to implement the logic of SetSize()?

Are there any patterns or best-practices to achieve this behavior?

like image 868
KingKerosin Avatar asked Jul 10 '16 09:07

KingKerosin


1 Answers

You can't do this via inheritance in C# because it doesn't support multiple inheritance; the tool the language gives you for this scenario is interfaces:

public interface  
{     
    void SetSize(Size size);
}

public SizableA: A, ISizable { ... }
public SizableB: B, ISizable { ... }

Also bear in mind that inheritance should be used when the types in the inheritance chain have a is a relationship (a Cat is an Animal, a B is an A, etc.) while interfaces are preferred when completely unrelated classes have a very specific common behavior; IEquatable, IComparable, IDisposable, etc. Your SetSize behavior seems to fit better in this last category as it doesn't seem to be a functionality specific to A or B.

Now, if both SizableA and SizableB should share the same implementation of SetSize then the best solution is to use composition or simply delegate functionality:

public interface ISizable
{
     void SetSize(Size size, ISetSizeProvider provider); //Alternatively inject provider in SizableA and SizableB's constructor to get composition.
}
like image 92
InBetween Avatar answered Nov 09 '22 08:11

InBetween