vb.net类组织

标签 vb.net class

我一直在努力让自己的注意力集中在类里面。即使在阅读了几个网站和书籍之后,我仍然不觉得我真的“明白”了。

我正在处理的应用程序有一个 sub,它将通过 USB 到 GPIB 接口(interface)控制几个不同的测试设备。

所以,我有:

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

在我的潜艇中,我想打开 GPIB 设备,发送一些 SCPI 命令以打开电源,再发送一些命令到 HP34970 以切换继电器或测量电压。

看起来很简单,我可以很容易地让所有东西都在同一个模块中工作。然而,我认为为 GPIB 接口(interface)、电源和 HP34970 提供一个单独的类会好得多。如果是这样的话,我可以很容易地在其他项目中重用代码。

这是我无法获得心智模型的地方——如果我创建了 GPIB 类的实例并有一个方法来“打开”到 GPIB 总线的 channel ,我如何允许其他类中的方法(比如电源)使用在 GPIB 类中创建的开放 channel ?例如电源类中设置电压的方法。

如果有人可以花几分钟时间发布伪代码和一些解释来说明我可以/应该如何组织它,我将不胜感激!

谢谢

最佳答案

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

这可能是其中一种用法 - 用 C# 编写(抱歉,如果我误解了实际用法:) 这里我假设电源和 dataLogger 连接到接口(interface),接口(interface)连接到笔记本电脑)。

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...
   }
}

现在,由于接口(interface)知道它所连接的设备,它必须将它们包含在它的构造函数中(在面向对象的设计中,也称为依赖注入(inject),更具体地说,这里是构造函数注入(inject)):

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 的引用。但是,如果它连接到接口(interface),然后连接到记录器,电源必须只引用接口(interface)而不是记录器(因为它没有连接到它)。

我希望你明白这个想法——它并不是无缘无故地被称为“面向对象”——将类视为真实的对象——如果一个对象可以做 thing1,那么你应该在你的类中有方法“thing1()”;如果对象与 object441 有连接,那么该类应该有对该类的引用。只要遵循这个原则,看看它会把你带到哪里......

当你掌握它时,你的程序将真正受益于一些控制反转 (IoC) 模式 - 这样你只需在开始时定义什么是记录器,什么是电源,谁作为显示器执行,以及何时有人需要 IoC 容器提供的特定设备。这样,您就不必在每次有新设备出现时都重写代码 - 只需向 IoC 容器注册新类型,一切正常。

关于vb.net类组织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3613547/

相关文章:

vb.net - 如何在 vb.net 中对 DataTable 中的列进行重新排序?

vb.net - HRESULT:尝试使用NAudio库播放mp2编码的wav文件时,0xC00D5212

ios - 尝试将数据存储在类中,更改 View Controller 时返回 nil

c# - 如何捕获主线程可以监视的多线程应用程序中的事件?

c# - 动态加载用户控件的订阅事件

c++ - 使用 'pointer' 和结构调用 DLL (C++ -> VB.NET)

c++ - 如何在 C++ 中处理对象的混合集合

java - 如何从类文件中获取 java.net.URLConnection 或哈希表?

php - 对(非对象)中的非对象调用成员函数prepare()

java - 可以在构造函数方法参数中设置类变量吗?