I have a problem with using the right inheritance-chain without loosing good parts of the DRY-principle.
Given the following structure:
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?
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.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With