应用程序架构:在java中保持串行连接打开

时间:2021-08-09 21:45:15

I'm working on a project in Java, which is communicating with two serial port devices using Suns's serial port library. I must somehow keep connections to devices open all the time and handle events of them (on data revieved...). The application is interacting with user (via SOAP) and is a console application (later it might be moved to an application server, but ignore that for now).

我正在开发一个Java项目,它使用Suns的串口库与两个串口设备进行通信。我必须以某种方式保持与设备的连接始终打开并处理它们的事件(在数据恢复...)。应用程序正在与用户交互(通过SOAP)并且是一个控制台应用程序(稍后它可能会被移动到应用程序服务器,但暂时忽略它)。

The question is how to handle devices. For now I have a static class with two static variables that returns objects that handle the device. In that class's static constructor I open connections and set initial parameters etc. Then whenever I need a device in application, I get it by something like: Device.MyDevice, Device. MyDevice2 etc... Is there any better way of doing this or is this one ok?

问题是如何处理设备。现在我有一个带有两个静态变量的静态类,它返回处理设备的对象。在该类的静态构造函数中,我打开连接并设置初始参数等。然后,无论何时我需要应用程序中的设备,我都可以通过以下方式获取它:Device.MyDevice,Device。 MyDevice2等...有没有更好的方法来做这个还是这个好吗?

Remember, I don't have problems with connecting to devices, I need architectural advice.

请记住,我没有连接到设备的问题,我需要建筑建议。

Thanks

1 个解决方案

#1


3  

My experience has been that static constructors can be messy. Also, if you wanted to test the logic by writing unit tests to mock the serial communication, this architecture would make it hard.

我的经验是静态构造函数可能很混乱。此外,如果您想通过编写单元测试来模拟串行通信来测试逻辑,那么这种架构会让它变得困难。

An alternative would be to have a Device class with a constructor that takes some configuration arguments (which serial port to use, for example), and have the two actual devices you would connect with as public static final fields of the class. Something like this:

另一种方法是使Device类具有构造函数,该构造函数接受一些配置参数(例如,使用哪个串行端口),并将要连接的两个实际设备作为类的公共静态最终字段。像这样的东西:

public class Device {
    public static final Device Device1 = new Device(...);
    public static final Device Device2 = new Device(...);

    public Device( ... ) {

    }
}

To make tests even easier, you could have a Device interface that this device class implements - it helps make the development loop tighter if you can test the logic for interacting with devices without dealing with the device itself.

为了使测试更容易,您可以拥有此设备类实现的Device接口 - 如果您可以测试与设备交互的逻辑而不处理设备本身,则有助于使开发循环更紧密。

#1


3  

My experience has been that static constructors can be messy. Also, if you wanted to test the logic by writing unit tests to mock the serial communication, this architecture would make it hard.

我的经验是静态构造函数可能很混乱。此外,如果您想通过编写单元测试来模拟串行通信来测试逻辑,那么这种架构会让它变得困难。

An alternative would be to have a Device class with a constructor that takes some configuration arguments (which serial port to use, for example), and have the two actual devices you would connect with as public static final fields of the class. Something like this:

另一种方法是使Device类具有构造函数,该构造函数接受一些配置参数(例如,使用哪个串行端口),并将要连接的两个实际设备作为类的公共静态最终字段。像这样的东西:

public class Device {
    public static final Device Device1 = new Device(...);
    public static final Device Device2 = new Device(...);

    public Device( ... ) {

    }
}

To make tests even easier, you could have a Device interface that this device class implements - it helps make the development loop tighter if you can test the logic for interacting with devices without dealing with the device itself.

为了使测试更容易,您可以拥有此设备类实现的Device接口 - 如果您可以测试与设备交互的逻辑而不处理设备本身,则有助于使开发循环更紧密。