有效实施现有协议

发布于 2024-10-07 15:20:08 字数 237 浏览 8 评论 0原文

我有一个现有的协议,我想为其编写一个java客户端。该协议由消息组成,消息头包含消息类型和消息长度,然后是宣布的有效负载字节数。 我在建模时遇到了一些麻烦,因为为每种消息类型创建一个类对我来说似乎有点过多(结果是 20 多个类只是为了表示通过网络传输的消息)我正在考虑替代模型。但我想不出一种有效的方法。

除了在消息传入时通过发布订阅进行通知以及在某些情况下回复之外,我不希望对消息进行任何花哨的操作。

有关于去哪里看的指示吗?

I have an existing Protocol I'd like to write a java Client for. The Protocol consists of messages that have a header containing message type and message length, and then the announced number of bytes which is the payload.
I'm having some trouble modeling it, since creating a Class for each message type seems a bit excessive to me (that would turn out to be 20+ classes just to represent the messages that go over the wire) I was thinking about alternative models. But I can't come up with one that works.

I don't want anything fancy to work on the messages aside from notifying via publish subscribe when a message comes in and in some instances reply back.

Any pointers as to where to look?

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

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

发布评论

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

评论(1

メ斷腸人バ 2024-10-14 15:20:08

每种消息类型的类是对此进行建模的自然 OO 方式。事实上有 20 个课程不应该让您望而却步。 (根据消息之间的关系,您可能可以在超类中实现常见功能。)

我的建议是一开始就不要太担心效率。只需专注于获取提供所需功能的干净 API。一旦一切正常,请分析代码并查看协议类是否是一个重要的瓶颈。如果它们是......那么考虑如何使代码更高效。

A class for each message type is the natural OO way to model this. The fact that there are 20 classes should not put you off. (Depending on the relationship between the messages, you can probably implement common featues in superclasses.)

My advice is to not worry too much about efficiency to start with. Just focus on getting clean APIs that provide the required functionality. Once you've got things working, profile the code and see if the protocol classes are a significant bottleneck. If they are ... then think about how to make the code more efficient.

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