类驱动程序实现 [英] Class driver implementation

查看:81
本文介绍了类驱动程序实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个班级司机来控制一些环境室,并提出一个相当基本的问题: -



我到目前为止只有一个司机工作它服务于两个腔室(它们都使用相同的通信协议)。我已经在类级别声明了变量,并在我知道我连接到哪个腔室时稍后将其设置为instatiate。现在我开始编写我的下一个驱动程序,当我将变量解析为driver_1类型时,我很明显我无法实例化driver_2类型。结果是我必须以不同的方式命名我的所有驱动程序实例,并使用if语句来调用它们。我希望这是有道理的。我的问题是: - 你最好怎么做?这几乎就像我需要继承,但事实上驱动程序之间的整个消息传递和功能是不同的;唯一的共性是界面。这是C#/ Java中接口的目的吗?



好​​的,希望这会让它更清晰: -



I am writing a class driver to control some environmental chambers and have a fairly fundamental question to pose:-

I have so far been working on just one driver that serves two chambers (they both use the same communication protocol). I have declared the variable at class level and instatiate it later on when I know what kind of chamber I am connecting to. Now that I am starting to code my next driver, It''s imediately obvious that I cannot instatiate a driver_2 type when I have decaled the variable as a driver_1 type. The result is that I have had to name all of my instances of drivers differently and use if statements to call them. I hope this makes sense. My question is:- How best would you do it ? It''s almost as if I need to inherit but for the fact that the whole messaging and functionality is different between the drivers; the only commonality is the interface. Is this a the purpose of interfaces in C# / Java ?

Ok, hopefully this will make it clearer:-

public class classDriver
{//My declarations
drivertype1 driver;
//drivertype2 driver; Ignore this for now..
//So within this method, I would like to select which driver to use.
public void method1(chamberType)
{
switch chamberType

case type1:
driver = new drivertype1();

break;

case type2:
driver = new drivertype2(); //but of course I cant make an instance because this is declared drivertype1.

break;
}

public void method2()
{
driver.dosomethingorother();// I want to be able to call my generic functions elsewhere.
}

}





所以,我有四个司机,都是相同的方法和属性,但希望能够选择在运行时使用哪一个。



So, I have four drivers, all with the same methods and properties but want to be able to select which one to use at runtime.

推荐答案

确定 - 给出新的信息



您必须确保drivertype1和drivertype2派生自相同的接口,抽象或基类。然后,将驱动程序声明为basetype(或var),然后实例化特定的drivertype:



OK - with the new info given

You just have to ensure that drivertype1 and drivertype2 derive from the same interface, abstract or base class. You then declare your driver as the basetype (or var) and then instantiate the specific drivertype:

public abstract class drivertypeGeneric {
  public virtual void dosomethingorother(){
    //Virtual allows you to override the method while still making it available to the subclass by way of "base.dosomethingorother()"
  }
}

public class classDriver
{
//declare driver as the generic type
drivertypeGeneric driver;

public void method1(chamberType)
{
switch chamberType
 
case type1:
//instantiate driver as any one of the derived types
driver = new drivertype1();
 
break;
 
case type2:
driver = new drivertype2(); //you can downcast with out any cast modifyers

break;
}
 
public void method2()
{
driver.dosomethingorother();// You can call any method that exists in the generic type or even a derived type if you upcast.
((DriverDerived)driver).dosomethingInDerivedNotInGeneric
}
 
}


我不认为我会在这里找到一个接口,更可能是两个类派生的抽象基类。这样,任何常见的低级代码都可以在基类中,更高级别的东西由实现的类处理。你说他们都使用相同的协议,所以我会尝试在基类中实现它,理解所包含的数据由派生类处理 - 你不能用接口来做,因为它只是合同 - 它根本不包含任何代码。
I don''t think I would go for an interface here, more likely an abstract base class from which both classes derive. That way any common low-level code can be in the base class with the higher level stuff being handled by the implemented class. You say they both use the same protocol, so I would try to implement that in the base class with the understanding of the contained data being handled by the derived classes - you can''t do that with an Interface because it is "just the contract" - it contains no code at all.


是这是接口的目的。



你需要确保两个驱动程序都具有相同的暴露签名。这意味着使用可用于loader类的相同接口。接口可能只有一个或两个方法签名,如Init()和Run()。你的两个驱动程序都应该实现这些方法。加载类时,您可以检查接口以确保可以将其转换为接口类型,然后您就可以调用Init或Run。运行的实际代码可能会大不相同,但签名才是最重要的。



所以你有EG:



Yes this is the purpose of interfaces.

You will need to ensure that both of your drivers have the same exposed signature. This means using the same Interface that is available to the loader class. The interface might have only one or two method signatures such as Init() and Run(). Both of your drivers should implement these methods. When you load the class you can check the interface to ensure that you can cast it as that interface type and you will then be able to call Init or Run. The actual code that gets run might be wildly different but the signature is all that matters.

so you would have for EG:

public interface IDriver
{
    void Init();
    void Run();
}
public class Driver1:IDriver
{
    #region Implementation of IDriver

    public void Init()
    {
        throw new NotImplementedException();
    }

    public void Run()
    {
        throw new NotImplementedException();
    }

    #endregion
}
public class Loader
{
    public void InitAndRun(string assemblyPath,string className )
    {
        Assembly a = Assembly.LoadFile(assemblyPath);
        if(a.GetType(className).GetInterfaces().Contains(typeof(IDriver)))
        {
            IDriver myDriver = (IDriver)a.CreateInstance(className);
            myDriver.Init();
            myDriver.Run();
        }
        else
        {
            //You can be more specific with exception cases than this
            throw new Exception(
                string.Format("The class {0} in assembly {1} does not exists or does not implement IDriver"));
        }
    }
}





您可以改为使用抽象类将允许您包含一些可能重载的共享字段或方法。上面的代码是相同的,但不是a.GetType(className).GetInterfaces()而是你必须查看a.GetType(className).IsSubclassOf(typeof(DriverAbstract))



You could instead use an abstract class which will allow you to include some shared Fields or Methods that you could overload. The code above would be the same but instead of "a.GetType(className).GetInterfaces()" you would have to look at "a.GetType(className).IsSubclassOf(typeof(DriverAbstract))"


这篇关于类驱动程序实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆