Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define an interface method that takes different parameters

Tags:

c#

.net

oop

My application uses measurement instruments that are connected to the PC. I want to make it possible to use similar instruments from different vendors.

So I defined an interface:

interface IMeasurementInterface
    {
        void Initialize();
        void Close();
    }

So far so good. Before a measurement I need to setup the instrument and this means for different instruments very different parameters. So I want to define a method that takes parameters that can have different structures:

interface IMeasurementInterface
{
    void Initialize();
    void Close();
    void Setup(object Parameters);
}

I will then cast the object to whatever I need. Is this the way to go?

like image 252
Enrico Avatar asked Oct 26 '08 16:10

Enrico


2 Answers

You might be better off coming up with an abstract "Parameters" class that is extended by each different instruments parameters... e.g. and then using Generics to ensure that the correct parameters are passed to the correct classes...

public interface IMeasurement<PARAMTYPE> where PARAMTYPE : Parameters
{
    void Init();
    void Close();
    void Setup(PARAMTYPE p);
}

public abstract class Parameters
{

}

And then for each specific Device,

public class DeviceOne : IMeasurement<ParametersForDeviceOne>
{
    public void Init() { }
    public void Close() { }
    public void Setup(ParametersForDeviceOne p) { }
}

public class ParametersForDeviceOne : Parameters
{

}
like image 118
Eoin Campbell Avatar answered Nov 15 '22 20:11

Eoin Campbell


To me it sound like the Factory pattern might be usefull, especially if your are going to unit test your app.

like image 22
Kasper Avatar answered Nov 15 '22 21:11

Kasper