Multiple Inheritance and C #

Let's say I have two basic abstract classes with completely different functionality: a laptop and a smartphone. (Assume the functionality is completely different). And in my current project, I already had many implementations of laptops and smartphones, and they were always completely different.

But unexpectedly, I received a request to add a class, which is an implementation of a PC tablet, which actually has the functions of both a smartphone and a laptop. It’s too late to change the base classes, and in fact I’m very sure that this PC tablet will appear only once.

The problem is that I should be able to keep my PC tablet in a container for smartphones, but it should also be a laptop because of the inherited functionality (in fact, next to this, in some part of the project, the PC tablet only uses like a laptop, and it doesn’t need the functionality of a smartphone, and it's bad to look at a PC tablet as a smartphone for this particular part of the project). So, I have a PcTabletAsLaptop: a laptop class, this is actually a laptop, not a smartphone.

My solution is to add a wrapper:

class PcTablet : SmartPhone { private PcTabletAsLaptop _pcTablet; // Here goes all the methods of PcTabletAsLaptop as proxies: public void Call(int number) { _pcTablet.Call(number); } // ..... } 

There are 200+ methods, and I want them to be automatically generated from PcTabletAsLaptop.

This solution looks rather complicated. My question is good, or maybe there are simpler ways to do this?

+4
source share
3 answers

You can extract the interface of both SmartPhone and Laptop, and then create a third PcTablet interface that inherits from the first two. Thus, you can use PcTablet as a smartphone or laptop.

Edit:

To be able to reuse the logic inside each smartphone and laptop, you can use the adapter template, so PcTablet should look something like this:

 public class PcTablet :ISmartPhone, ILaptop { private SmartPhone _smartphone; private Laptop _laptop; public void ISmartPhone.Call() { _smartPhone.Call(); // OR IMPLEMENT THE RIGHT BEHAVIOR //INSTEAD OF CALLING _smartPhone.Call() } } 

Of course, you will need to create a smartphone and laptop in the constructor, but it should do the trick! Thus, you can reuse the code in a laptop and smartphone, but also override their behavior in case they do not provide the correct one.

+3
source

If you need several inheritances - in most cases you are doing something wrong or trying to solve the problem incorrectly. How about a hierarchy as shown below?

 internal class PcTabletAslaptop : LaptopBase { // here is you can expose / override laptop specific stuff } internal class PcTabletAsSmartphone : SmartphoneBase { // here is you can expose / override smartphone specific stuff } public interface IPcTablet { // just expose PcTablet specific API } public sealed class PcTablet : IPcTablet { private PcTabletAsSmartphone asSmartphone; private PcTabletAsLaptop asLaptop; } 
+1
source

I think that you can think not with aggregation, but with composition.

aggregation-vs-composition

How about a SmartPhone contains a Tablet .

0
source

All Articles