代码之家  ›  专栏  ›  技术社区  ›  Dee

vb.net版班级组织

  •  3
  • Dee  · 技术社区  · 14 年前

    我正在开发的应用程序有一个sub,它可以通过USB-to-GPIB接口控制一些不同的测试设备。

    所以,我有:

    Laptop
       USB - GPIB Interface
            Power Supply (Device Address 5)
            HP34970 Datalogger (Device Address 6)
    

    在我的sub中,我想打开GPIB设备,发送几个SCPI命令来打开电源,再发送几个到HP34970来切换继电器或测量电压。

    这里是我无法获得心智模型的地方-如果我创建了一个GPIB类的实例,并且有一个方法“打开”到GPIB总线的通道,那么我如何允许其他类(如powersupply)中的方法使用在GPIB类中创建的开放通道?例如,power supply类中用于设置电压的方法。

    如果有人能花几分钟来发布伪代码和一些解释来说明我可以/应该如何组织这个,我将非常感谢您的帮助!

    3 回复  |  直到 14 年前
        1
  •  1
  •   veljkoz Danko Valkov    14 年前

    把类本身看作设备。这里的主要好处是能够重用一次编写的代码—例如,您的所有设备都有地址,它们可以连接、测试连接、断开连接—为什么不在一个抽象的“设备”类中使用这些代码,并使所有设备从中继承?

    public class DeviceTesting
    {
       private string powerSupplyAddress = "DA5";
       private string dataLoggerAddress = "DA6";
    
       public static void main()
       {
    
          //bring interface into life
          DeviceInterface interface = GPIBInterface(); //example of inheritance - GPIB could be just one of the many device interfaces
    
          //the constructor in PowerSupply takes on the responsibility of initializing itself
          PowerSupply ps = new PowerSupply(powerSupplyAddress, interface); //plugged in with interface - has a reference to it
          //you could have multiple types of data loggers - all could inherit from DataLogger, and here so does HP34970
          //This means you can reuse the common code for data loggers, and write only the specifics for each specific device of that kind
          DataLogger myLogger = new HP34970(dataLoggerAddress, interface);
    
    
          //now, do whatever you do with the real devices
          ps.SetVoltage(220); //send voltage command directly
          interface.SendLogMessage("Interface is operational");
          interface.ExecuteTest("test1"); //send voltage command through interface
          //etc... you get the idea...
       }
    }
    

    现在,由于接口拥有与之接口的设备的知识,它必须在其构造函数中包含这些设备(在面向对象设计中,也称为依赖项注入,更具体地说,这里是构造函数注入):

    public GPIBInterface : DeviceInterface //here we are reusing code that's same for all interfaces
    {
        //whoever attaches to this interface is responsible for registering itself with it
        public PowerSupply Power{get;set;}
        public DataLogger Logger{get;set;}
        //notice here that it can work with any class that inherits from DataLogger
        public GPIBInterface()
        {
    
        }
    
        private override void TestInitialization() //as an example, we write our own testing by overriding some method in base class
        {
            base.TestInitialization(); //make sure that the base class checks it's ok - e.g. if it's own display is working or similar...
    
            if (Power.TestConnection() == false)
                throw new DeviceNotWorkingException(ps);
            if (Logger.TestConnection() == false)
                throw new DeviceNotWorkingException(_logger);
        }
    
        public void SendLogMessage(string message)
        {
            Logger.SendMessage(message);
        }
    
        public void ExecuteTest(string testName)
        {
            switch(testName)
            {
                case "test1":
                    Power.SetVoltage(280);
                    Logger.SendMessage("Voltage increased to 280V");
                    break;
            }
        }
    }
    

    基本上,如果你的设备在“现实生活”中相互作用,那就意味着它们应该有一个相互连接的设备的引用。例如,如果您将PowerSupply直接插入到logger,那么PowerSupply类应该有对logger的引用。但是,如果它连接到接口,然后再连接到记录器,那么电源必须只引用接口而不是记录器(因为它没有连接到它)。

    当你掌握了这一点,你的程序将真正受益于某种控制反转(IoC)模式——这样你就可以在一开始就定义什么是记录器,什么是电源,谁是显示器,什么时候有人需要某个特定的设备,IoC容器就会提供它。这样你就不必每次城里有新设备的时候都重写代码——只要用IoC容器注册新类型,一切就都正常了。

        2
  •  1
  •   supercat    14 年前

        3
  •  1
  •   Robert Harvey    14 年前

    我如何允许其他方法 类(如电源)来使用 上课?

    一种方法是创建一个复合类,其中包含powersupply类和GPIB类的实例(对于C#很抱歉):

    public class PowerSupplyGPIBController
    {
        // Members
        GPIB gpib;
        PowerSupply powerSupply;
    
        // Constructor
        public PowerSupplyGPIBController(GPIB myGPIB, PowerSupply myPowerSupply)
        {
            gpib = myGPIB;
            powerSupply = myPowerSupply;
        }
    
        // Method
        public void SendPowerSupplyVoltage()
        {
            GPIB.Send(myPowerSupply.Voltage);
        }
    }