Home > Software design >  The best practice in using the base interface
The best practice in using the base interface

Time:02-08

Our purpose of using Interface was to have the same method for each class, we realized that the method we previously determined was not sufficient for the task of each class.

I decided to solve this problem by creating an overload method

Is the usage in the example below a correct method? What do you recommend

Base Interface

public interface IBankMapper<T1,T2,T3>
    {
        public  T1 Test(T2 t2,T3 t3);
    
    }

Interface that inherits from the base interface for X bank

 public interface IXBank : IBankMapper<string, string, string>
    {
        public int Test(string t2, string t3, string t4,int t5);
    }

use of the interface

   public class XBank : IXBank
    {
        public string Test(string t2, string t3)
        {
            throw new NotImplementedException();
        }


        public int Test(string t2, string t3, string t4, int t5)
        {

            return t2.Length   t3.Length   t4.Length   t5;
        }

    }

we used it like in this example before we needed new parameters.(Y Bank)

  public interface IYBank : IBankMapper<string, string, string>
    {
     
    }




   public class YBank : IYBank
    {
        public string Test(string t2, string t3)
        {
            return (t2.Length   t3.Length).ToString();
        }
    }

As seen here, the method named Test from the base interface throw new NotImplementedException(); to be left with the body is this wrong? What's the solution?

FLOW enter image description here

CodePudding user response:

is this wrong?

This would be a rather clear violation of liskovs substitution principle, i.e. all objects implementing a interface should interchangeable. Implementing an interface is a promise of providing some functionality, throwing NotImplementedException violates that promise.

What's the solution?

This depend on the specific situation.

In some cases it is possible to provide a default value for added parameters. If so, you can make the parameter optional, make the old method a default interface method or an extension method to avoid changing all the call sites.

It might even be possible to remove the old method completely, and change all the callers, but this assumes the callers can provide meaning full values for all parameters.

In other cases it might be a sign that the interfaces should be separate, so that each can be used where appropriate. c# allow multiple interface implementations, so a class can still implement both interfaces.

It is difficult to provide concrete recommendations since I assume the example is fictional.

CodePudding user response:

Tested this, runs with no problem, however as pointed by Jonas, it's not the best solution:

using System;

namespace ConsoleApp2
{
class Program
{
    static void Main(string[] args)
    {
        var xB = new XBank();
        var yB = new YBank();

        Console.WriteLine(xB.Test("a", "b"));
        Console.WriteLine(yB.Test("a", "b"));
    }
}

public interface IBankMapper<T1, T2, T3>
{
    public T1 Test(T2 t2, T3 t3);

}

public interface IXBank : IBankMapper<string, string, string>
{
    public int Test(string t2, string t3, string t4, int t5);
}

public class XBank : IXBank
{
    public string Test(string t2, string t3)
    {
        return (t2.Length   t3.Length).ToString();
    }


    public int Test(string t2, string t3, string t4, int t5)
    {

        return t2.Length   t3.Length   t4.Length   t5;
    }

}

public interface IYBank : IBankMapper<string, string, string>
{

}


public class YBank : IYBank
{
    public string Test(string t2, string t3)
    {
        return (t2.Length   t3.Length).ToString();
        }
    }
}
  •  Tags:  
  • Related