如何组织库类结构

发布于 2024-12-06 01:31:24 字数 965 浏览 1 评论 0原文

我有一个可以通过串行端口进行通信的控制器电路,我想为其编写一个类库。我认为调用方法比重复硬编码冗长的字符串要容易得多(并且更具可读性)。不管怎样,控制器预编程了约 100 个获取/设置功能,分为三类:传感器设置、输出设置和环境设置。这些函数用于获取或设置控制器的设置。我想知道“最好的”或“公认的”班级组织是什么?

所有功能都属于同一个控制器并使用相同的串行端口,因此我认为 Controller 将是顶级类。在此类中,我设置了一个 SerialPort 实例,并使用该实例创建了一个简单的发送/接收方法:string SendReceive(string commandString)。

因为我真的不想在单个类中拥有约 100 个函数和属性,所以我尝试创建一些嵌套类(SensorSettingsOutputSettingsEnvironmentSettings< /code>) 并将相应的函数放置在其中。然而,当我尝试构建时,我收到一个编译错误,指出我试图从嵌套类之一访问更高级别的非静态方法 (SendReceive(string commandString))。

每种方法都有一个唯一且可变的发送/接收命令,因此我将在方法中创建命令字符串,调用 SendReceive 并处理返回的命令。有什么办法可以做到这一点吗?

我想使用属性来获取/设置各种设置。例如...

controllerInstance.SensorSettingsProperty.Units; // 使用中的单位。

或...

controllerInstance.OutputSettingsProperty.GetOutput(sensor1); // 获取传感器 1 输出。

...但是所有这些都需要使用相同的串行端口和 SendRecieve

I have a controller circuit that I can communicate with via serial port and I would like to write a class library for it. I figured it is far easier calling a method (and far more readable) than repeatedly hard-coding lengthy character strings. Anyway, the controller comes pre-programmed with ~100 get/set functions separated into three categories: Sensor Settings, Output Settings, and Environment Settings. These functions are used to get or set the controller's settings. I was wondering what the "best" or "accepted" class organization would be?

All the functions belong to the same controller and use the same serial port so I figured that Controller would be the top level class. Within this class, I setup a SerialPort instance and created a simple send/receive method using the instance: string SendReceive(string commandString).

Because I really don't want to have ~100 functions and properties in the single class, I tried creating some nested classes (SensorSettings, OutputSettings, and EnvironmentSettings) and placing the respective functions within them. When I tried to build, however, I received a compile error stating that I attempted to access a higher level, non-static method (SendReceive(string commandString)) from within one of the nested classes.

Each of the various methods has a unique and variable send/receive command so I would create the command string within the method, call SendReceive, and process the returning command. Is there any way to do this?

I would like to use properties to work my way down to get/set the various settings. For example...

controllerInstance.SensorSettingsProperty.Units; // Units in use.

or...

controllerInstance.OutputSettingsProperty.GetOutput(sensor1); // Get sensor 1 output.

...but all of these require the use of the same serial port and SendRecieve.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱ヾ落雨殇 2024-12-13 01:31:24

听起来您需要为每个命令创建对象,然后使用控制器发送命令。

示例:

 public class GetSensorReadingCommand
 {
      public GetSensorReadingCommand(Sensor sensor, SerialController controller)
      {
          // Set up command
      }

      public int Execute()
      {
          // Call controller.SendReceive with whatever, get or send stuff
      }
 }

那么每次您想要执行某个功能时,您只需创建新对象即可。

 SerialController controller = new SerialController(somePortNumber);
 GetSensorReadingCommand command = new GetSensorReadingCommand(Sensor.Sensor10, controller);
 int reading = command.Execute();

对于您可以发送的每个命令,您都将遵循类似的模式。如果它不是已知的编译类型传感器编号,请提供一个整数,而不是使用枚举。

您最终会得到很多小代码文件,而不是一堆大代码文件。此外,您可以使用特定于其功能的逻辑来定制每个命令。

It sounds like you need to make objects for each command, and then use your controller to send commands.

Example:

 public class GetSensorReadingCommand
 {
      public GetSensorReadingCommand(Sensor sensor, SerialController controller)
      {
          // Set up command
      }

      public int Execute()
      {
          // Call controller.SendReceive with whatever, get or send stuff
      }
 }

Then you would simply make new objects every time you want to do a function.

 SerialController controller = new SerialController(somePortNumber);
 GetSensorReadingCommand command = new GetSensorReadingCommand(Sensor.Sensor10, controller);
 int reading = command.Execute();

You would follow a pattern like that for each command you can send. If it's not a known at compile type sensor number, instead of using an enum, provide an integer.

You'll end up with a lot of small code files instead of a bunch of large ones. In addition, you can tailor each command with logic specific to it's functioning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文