Coming from a C++ background, Im used to multiple inheritance. I like the feeling of a shotgun squarely aimed at my foot. Nowadays, I work more in C# and Java, where you can only inherit one baseclass but implement any number of interfaces (did I get the terminology right?).
For example, lets consider two classes that implement a common interface but different (yet required) baseclasses:
public class TypeA : CustomButtonUserControl, IMagician
{
public void DoMagic()
{
// ...
}
}
public class TypeB : CustomTextUserControl, IMagician
{
public void DoMagic()
{
// ...
}
}
Both classes are UserControl
s so I cant substitute the base class. Both needs to implement the DoMagic
function. My problem now is that both implementations of the function are identical. And I hate copy-and-paste code.
The (possible) solutions:
TypeA
and TypeB
to share a common baseclass, where I can write that identical function definition just once. However, due to having the limit of just one baseclass, I cant find a place along the hierarchy where it fits.DoMagic
function in a separate helper class, but the function here needs (and modifies) quite a lot of internal variables/fields. Sending them all as (reference) parameters would just look bad.I tagged this with language-agnostic since it applies to all languages that use this one-baseclass-many-interfaces approach.
Also, please point out if I seem to have misunderstood any of the patterns I named.
In C++ I would just make a class with the private fields, that function implementation and put it in the inheritance list. Whats the proper approach in C#/Java and the like?
You could use pattern strategy to define different behaviors and make the subclasses implement them.
The only way to implement multiple inheritance is to implement multiple interfaces in a class. In java, one class can implements two or more interfaces. This also does not cause any ambiguity because all methods declared in interfaces are implemented in class.
Interfaces provide an alternative to multiple inheritance. Java programming language does not support multiple inheritance. But interfaces provide a good solution. Any class can implement a particular interface and importantly the interfaces are not a part of class hierarchy.
Which design patterns benefit from the multiple inheritances? Explanation: Adapter and observer patterns benefit from the multiple inheritances.
You can use the strategy pattern or something like it to use has a (composition) instead of is a (inheritance):
public class TypeA : CustomButtonUserControl, IMagician { IMagician magicObj = new Magical(); public void DoMagic() { magicObj.DoMagic(); } } public class TypeB : CustomButtonUserControl, IMagician { IMagician magicObj = new Magical(); public void DoMagic() { magicObj.DoMagic(); } } public class Magical : IMagician { public void DoMagic() { // shared magic } }
There are other ways to instantiate your private IMagician members (such as passing them as a param via constructor) but the above should get you started.
DoMagic
works with a lot of Private members. Can you package these private variables internal
possibly? This way the extension method could access them.If nothing seems particularly good, pick what feel best, use it a couple times, and rearrange tomorrow. :)
Replace inheritance with composition.
Move your 'common' function to separate class, create an instance of that class, and insert it to TypeA object and to TypeB object.
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