Home > Net >  Trying to program to abstractions in C# but neither interfaces nor classes really work
Trying to program to abstractions in C# but neither interfaces nor classes really work

Time:06-06

I've been trying to apply SOLID principles more consciously on my current project. Using interfaces to create the abstraction and allowing classes that are handling the dependency injection to provide the concretions has really helped with decoupling some of the code and (hopefully!) making it more maintainable in the long run.

However, here and there I'm hitting a bit of a wall where it seems neither interfaces nor abstract classes work for the reason that there are functions for which I want an implementation defined.

This means:

  • Interfaces will not work since I can't define an implementation and obviously don't want to repeat the code in all implementing classes
  • Abstract classes will not work because I cannot derive from multiple classes

Some super simple code to illustrate the problem:

    public abstract class Vehicle
{
    public void MoveForward()
    {
        // Some code here
        // This implementation is always the same
    }

    public abstract void PerformUniqueAbility(); // This is for the derived class to implement
}

public abstract class RadioSignalBroadcaster
{
    public void StartBroadcast()
    {
        // Some code here
        // This implementation is always the same
    }

    public abstract void PerformUniqueBroadcastingAbility(); // This is for the derived class to implement
}

Now of course what I'd like to do is this:

    public class MyNewClass: Vehicle, RadioSignalBroadcaster
{
    // Class that contains the implementations for both MoveForward() AND StartBroadcast() but also allows me to define
    // bodys for the abstract methods

    public override void PerformUniqueAbility()
    {
        // class specific code here
    }

    public override void PerformUniqueBroadcastingAbility()
    {
        // class specific code here
    }
}

Of course I cannot do this because of the error:

Error CS1721 Class 'MyNewClass' cannot have multiple base classes: 'Vehicle' and 'RadioSignalBroadcaster'

What's the best way to approach these scenarios?

CodePudding user response:

You could use interfaces with default implementations which were introduced in C# 8. Then you could derive from these interfaces.

Here's an example of how you could you provide default implementations for the MoveForward() and StartBroadcast() methods:

public interface IVehicle
{
    void MoveForward()
    {
        // your code
    }
    void PerformUniqueAbility();
}
public interface IRadioSignalBroadcaster
{
    void StartBroadcast()
    {
        // your code
    }
    void PerformUniqueBroadcastingAbility();
}

CodePudding user response:

You can't inherit more than 1 class but you can inherit more than one interface. Is this what you are looking for?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            IVehicle vehicle = new Vehicle();
            IRadioBroadcaster broadcaster = new RadioBroadcaster();

            vehicle.MoveForward();
            vehicle.PerformUniqueAbility();
            broadcaster.StartBroadcast();
            broadcaster.PerformUniqueAbility();
        }
    }

    public interface IUniqueAbillity
    {
        void PerformUniqueAbility();
    }

    public interface IVehicle: IUniqueAbillity
    {
        void MoveForward();
    }

    public interface IRadioBroadcaster : IUniqueAbillity
    {
        void StartBroadcast();
    }

    public abstract class RealWorldObject : IVehicle, IRadioBroadcaster
    {
        public void MoveForward()
        {
            // Move forward
        }

        public abstract void PerformUniqueAbility();

        public void StartBroadcast()
        {
            // Start broadcast
        }
    }

    public class Vehicle : RealWorldObject, IVehicle
    {
        public override void PerformUniqueAbility()
        {
            // Do something
        }
    }

    public class RadioBroadcaster : RealWorldObject, IRadioBroadcaster
    {
        public override void PerformUniqueAbility()
        {
            // Do something
        }
    }
}

CodePudding user response:

C# classes can only inherit from one base class, but can inherit from any number of interfaces.

If your goal is to have multiple base classes being inherited to MyNewClass, you could change one of your abstract classes to inherit from the other, for example:

public abstract class RadioSignalBroadcast : Vehicle
{
    // Implementation goes here
}

public class MyNewClass : RadioSignalBroacast
{
    // Implementation goes here
}

However, as you can see from this approach, it violates Single Responsibility Principle as now RadioSignalBroadcast (and now MyNewClass) has more than one reason to change (if there's a change to Vehicle or RadioSignalBroadcast logic). Any change that happens to any of the base classes will propagate to all other classes which inherit from those base classes, which may or may not be what you're after.

What's the best way to approach these scenarios?

That entirely depends on the design of your application. Questions to ask yourself:

  • Do you require Vehicle and RadioSignalBroadcast to be abstract classes, or can it easily be an interface? By the looks of your implementation, you have a couple of methods which you want to share to your derived classes so I understand you wanting to keep them as base classes, but it's something to keep in mind. Also check out if the implementation of MoveForward and StartBroadcast can have a default interface implementation.
  • Does MyNewClass need to implement both base classes/interfaces? Couldn't two separate classes work out better? Separating out classes like this helps to focus each of the classes to have one single responsibility.
  • If MyNewClass is not truly a Vehicle or a RadioSignalBroadcast (as per the previous point), can this object be composed by a combination of either of the two, for example:
public class MyNewClass : Vehicle
{
    private readonly RadioSignalBroadcast radio;

    public MyNewClass(RadioSignalBroadcast radio)
    {
        this.radio = radio;
    }

    public void DoStuff()
    {
        // Do Stuff
        this.radio.PerformUniqueBroadcastingAbility();
    }

    // Implementation goes here
}

Let me know if you want example or more points to point out.

CodePudding user response:

I think Jonas gave you the best answer that you can use default interface implementations. However I keep my post, because it gives information, how to achieve same effect, using technology without this language feature.

public abstract class Example : IExample 
{
    private readonly IVehicle vehicle;
    private readonly IRadioSignalBroadcaster;

    public Example(IVehicle vehicle, IRadioSignalBroadcaster radioSignalBroadcaster)
    {
        this.vehicle = vehicle;
        this.radioSignalBroadcaster = radioSignalBroadcaster;
    }

    public void MoveForward() => vehicle.MoveForward();
    
    public void StartBroadcast() => radioSignalBroadcaster.StartBroadcast();

    public void PerformUniqueAbility() => vehicle.PerformUniqueAbility();

    public void PerformUniqueBroadcastingAbility() => radioSignalBroadcaster.PerformUniqueBroadcastingAbility();
}
public interface IExample : IVehicle, IRadioSignalBroadcaster
{
}
public interface IVehicle 
{
    void MoveForward();
    void PerformUniqueAbility();
}
public interface IRadioSignalBroadcaster 
{
    void StartBroadcast();
    void PerformUniqueBroadcastingAbility();
}
public abstract class Vehicle : IVehicle
{
    public void MoveForward()
    {
        // ...
    }

    public abstract void PerformUniqueAbility();
}
public interface ICustomVehicle : IVehicle 
{
}
public class CustomVehicle : Vehicle, ICustomVehicle
{
    public void PerformUniqueAbility() 
    {
        // ...
    } 
}
public abstract class RadioSignalBroadcaster : IRadioSignalBroadcaster
{
    public void StartBroadcast()
    {
        // ...
    }
    public abstract void PerformUniqueBroadcastingAbility();
}
public interface ICustomRadioSignalBroadcaster : IRadioSignalBroadcaster
{   
}
public class CustomRadioSignalBroadcaster : RadioSignalBroadcaster, ICustomRadioSignalBroadcaster 
{
    public void PerformUniqueBroadcastingAbility()
    {
        // ...
    }
}

You will create another classes like that:

public class CustomExample : Example, ICustomExample
{
    public CustomExample(ICustomVehicle customVehicle, ICustomRadioSignalBroadcaster customRadioSignalBroadcaster) : base(customVehicle, customRadioSignalBroadcaster)
    {
    }
}
public interface ICustomExample : IExample
{
}
  • Related