-
January 19th, 2015, 11:28 AM
#1
Use same interface for different classes
Hi,
- I have a situation where the a company that delivered a motor to us has changed the dll+Motor which I was using to control the motor. The old motor runs with the old dll and new motor runs with the new one.
- My aim is that the user need not require to know which motor is connected to the system. I have written a simplified code by using interface, though I am sure that it is not the best way to do this.
- There's a function in each dll which recognizes the version of motor but I can't use it effectively as I have to know in advance in order to cast the object to respective motor.
- I would like to know if I can use interface in the following situation or if there's a better solution to it? In my solution I'll always have to cast the object to the respective version. Is there any way to avoid it?
Code:
public interface IMotor
{
double GetSpeed();
string GetVersion();
}
public class Motor2_0 : IMotor
{
[DllImport("Motor2_0.dll")]
static extern double GetSpeedOfMotor();
[DllImport("Motor2_0.dll")]
static extern string GetVersionOfMotor();
public string GetVersion()
{
return GetVersionOfMotor();
}
public double GetSpeed()
{
return GetSpeedOfMotor();
}
}
public class Motor1_0 : IMotor
{
[DllImport("Motor1_0.dll")]
static extern double GetSpeedOfMotor();
[DllImport("Motor1_0.dll")]
static extern string GetVersionOfMotor();
public string GetVersion()
{
return GetVersionOfMotor();
}
public double GetSpeed()
{
return GetSpeedOfMotor();
}
}
public class Motor
{
Object _motorObject;
string _version;
void InitMotor(string aVersion)
{
_version = aVersion;
if (_version == "1.0")
_motorObject = new Motor1_0();
if (_version == "2.0")
_motorObject = new Motor2_0();
}
double GetSpeed()
{
if (_version == "1.0")
return ((Motor1_0)_motorObject).GetSpeed();
if(_version == "2.0")
return ((Motor2_0)_motorObject).GetSpeed();
return -1; //false version
}
}
Thaks
Ricky
Last edited by ricky_cpp; January 19th, 2015 at 11:47 AM.
-
January 19th, 2015, 06:57 PM
#2
Re: Use same interface for different classes
Instead of declaring the motor as an object, declare it as an interface.
Code:
public class Motor
{
private IMotor_motor;
private string _version;
public void InitMotor(string version)
{
_version = version;
switch(_version)
{
case "1.0":
_motor = new Motor1_0();
break;
case "2.0":
_motor = new Motor2_0();
break;
default:
throw new Exception("Invalid motor version");
}
}
public double GetSpeed()
{
return _motor.GetSpeed();
}
}
Furthermore, if you don't want user intervention, just read the version of the dll (assuming that your vendor properly versions the dll and that only one is installed on any given system) and then load up the correct IMotor implementation.
-
January 21st, 2015, 10:00 AM
#3
Re: Use same interface for different classes
Seems to be a good idea.
Thanks!!
-
January 30th, 2015, 10:08 PM
#4
Re: Use same interface for different classes
It is basic to answer one question. "Do you need different classes doing different things?"
If you have several classes in your plan which are different enough in behavior, the best way is to use an interface for all. But if you can compact the functionality in one single class you have to do it. Don't use interfaces when it is not needed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|