开发者

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 开发者_JS百科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,


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.


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.


If the parameters can be different, then it isn't really a common interface. Put it this way: does the caller need to know the implementation class? If so, you've lost the loose coupling benefits of interfaces.

One option is to encapsulate the parameters into another type, and make the class generic on that type. For example:

public interface IViewModel<T>
{
    void ResetReferences(T data);
}

Then you'd encapsulate the List<Color> colors, List<Size> sizes into one type, and possibly put List<StateProvinces> stateProvinces in another.

It's somewhat awkward though...


You will need to implement the interface method, but you can still do what you want

public class LocationViewModel : IViewModel
{
    public void ResetReferences(List<StateProvinces> stateProvinces) // ...

    void IViewModel.ResetReferences() // ...
}


You would have to have both methods in the interface (and have the one not correct for an instance throw a non-supported exception), or have the interface inherit from two other interfaces to the same effect.

An interface definition is the entire signature.

It may also be possible to pass an object as a parameter (perhaps derived from a ParameterProvider base class) so that the object encapsulates the dynamic nature and still allows the interface to be static. But that that point you're basically working around the type system anyway.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜