Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create method interface with variable parameters / different method signatures?

I'm trying to create an interface to a common class, but the implementation classes can have different parameters.

e.g.

public interface IViewModel
{
    //...
    void ResetReferences(); 
}

// and then, in my class implementations, something like this:
public class LocationViewModel : IViewModel
{
    public void ResetReferences(List<StateProvinces> stateProvinces) //...
}

public class ProductViewModel : IViewModel
{
    public void ResetReferences(List<Color> colors, List<Size> sizes) //...
}

So notice that I want to standardize on the ResetReferences naming convention. I'm pretty sure I can't do this, but is there a design pattern that could work? e.g. in my interface, something like below?

// variable parameters
void ResetReferences(params object[] list); 

But then how do I make I do type checking or having it call the actual method signature that I want, etc?

Maybe an interface is the wrong thing to use? Maybe just a base class and some coding conventions?

Thanks,

like image 243
Raymond Avatar asked May 24 '11 17:05

Raymond


People also ask

Can an interface method have parameters?

A Java interface is a bit like a Java class, except a Java interface can only contain method signatures and fields. A Java interface is not intended to contain implementations of the methods, only the signature (name, parameters and exceptions) of the method.

Can we pass interface as a parameter to a method in Java?

Yes, you can pass Interface as a parameter in the function.

CAN interfaces declare method signatures?

Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).

Can interface methods have parameters C#?

You could use an interface method with a variable number of arguments using the params keyword. But you then need to cast each argument to the appropriate type, which is a bit error prone. Show activity on this post. Methods with different parameters cannot both implement the same interface method declaration.


2 Answers

Replace your args lists with objects that implement a related interface:

public interface IViewModel
{
    //...
    void ResetReferences(IResetValues vals); 
}

I should add that, IMO, ResetReferences() should not take an argument... it should reset to some default value that would be specific to the individual type(s) that implement your interface..."Reset" being the word that means, to me, "restore to initial state"...adding args implies that you can control that.

like image 153
JeffSahol Avatar answered Sep 24 '22 17:09

JeffSahol


The purpose of an interface is to have client code know about the interface and be oblivious of the implementation. If your implementations require special treatment when called, the client code need to know what implementation it is calling and then the whole purpose of the interface is lost.

Unless I misunderstand totally what you're trying to accomplish, you're down the wrong road.

like image 23
Anders Abel Avatar answered Sep 25 '22 17:09

Anders Abel