C # interface - implementation with a different signature

I believe this is also a design issue. Is it possible to override a method from the interface when the redefinition signature has a different type of signature?

For example, let's say that I need two different classes, which should have the following:

 interface IProtocolClient
 {
    void connect(Type1 t1, Type2 t2, Type3 t3);
 }

Is it possible to implement an interface, but you have a different set of parameters?

 class A : IProtocolClient {
   public void connect( Type1 t1, Type2 t2, Type3 t3 ) {}
 }

 class B : IProtocolClient {
   public void connect( Type1 t1, Type2 t2, Type3 t3, Type4 t4 ) {}
 }

Or should I approach this by creating a base class instead, and then create a wrapper method in class B, for example:

 class B : IProtocolClient {
   public void connect( Type1 t1, Type2 t2, Type3 t3, Type4 t4)
   {
      // do what is needed with t4 to customize and then ...
      connect(t1,t2,t3);
   }

   public void connect( Type1 t1, Type2 t2, Type3 t3) {}
 }
+3
source share
9 answers

- "", "", , . , , , , , , , , , .

+3

- , ( , ).

, , , , connect(Type1 t1, Type2 t2, Type3 t3), , ( ).

+3

, , . , , , .

+3

, , .. , : . . , , , .

+1

, 2, , .

+1

, , .

, , .

public interface ITestClass
{
    void Test(Type a, Type b);
}

public class TestClass : ITestClass
{

    //implement the interface here
    public void Test(Type a, Type b)
    {
        Test(a, b);
    }

    //you actual implementation here
    public void Test(Type a, Type b, Type c = null)
    {
        //implementation
    }
}

, :

    public void Test(params Type[] parameters)
    {
        //sample usage
        Type one, two, three, four;
        Test(one, two);
        Test(one, two, three, four);
    }
0

, , IProtocolClient, , -

public interface IProtocolClient
 {
    void connect(Type1 t1, Type2 t2, Type3 t3);
    void connect( Type1 t1, Type2 t2, Type3 t3, Type4 t4)
 }

,

    class A : IProtocolClient {
       public void connect( Type1 t1, Type2 t2, Type3 t3 ) {}
       void  IProtocolClient.connect(Type1 t1, Type2 t2, Type3 t3, Type4 t4){
        throw new NotImplementedException();
       }     
 }

, , classB , . classB IProtocolInterface .

0

If you pass all the parameters as an object, you can get a class of parameters, and then apply it in the implemented method.

    interface IProtocolClient
    {
        void connect(ParamType p);
    }

    class ParamType
    {
        public Type1 t1 { get; set; }
        public Type2 t2 { get; set; }
        public Type3 t3 { get; set; }
    }

=>

    class A : IProtocolClient
    {
        public void connect(ParamType p)
        {
            //do something with p.t1, p.t2, p.t3
        }
    }

    class B : IProtocolClient
    {
        public void connect(ParamType p)
        {
            var p2 = p as DerivedParamType;
            if (p2 == null)
                throw new ApplicationException("p must be of type DerivedParamType");
            //do something with p2.t1, p2.t2, p2.t3, p2.t4
        }
    }

    class DerivedParamType : ParamType
    {
        public Type4 t4 { get; set; }
    }

Hth

0
source

All Articles