模式匹配框架?

发布于 2024-08-16 10:34:13 字数 566 浏览 4 评论 0原文

我很快就会处理一个 Java (GWT) 项目(相关问题)。也许我想在这里扩展一些东西,但我想知道是否有任何用Java编写的“模式匹配框架”(真的不知道是否有一个术语)? (也许是我对 Erlang 的长期接触一直扭曲着我对模式的思考 :-)

我将使用“消息传递”架构在我的 Java 组件之间进行通信,并且我希望有效地将消息“匹配”到操作。

也许我应该坚持使用本地化状态机,或者还有其他什么吗?

更新:“消息”将是携带“仅数据”的实例对象。我目前不打算使用继承来向所述消息传递语义,而是使用简单的属性。

更新2:在汲取SO的集体智慧后(参见此处),看来 Scala 也超出了范围。

(注:这里是Java新手...请温柔点)

I will be tackling a Java (GWT) project soon (related question). Maybe I am trying to stretch things here but I was wondering if there is any "pattern matching framework" (don't really know if there is a term for this) written in Java? (maybe it is my prolonged exposure to Erlang that twists my thoughts around patterns all the time :-)

I will be using a "message passing" architecture to communicate between my Java components and I'd like to efficiently "match" messages to actions.

Maybe I should just stick with localized state-machines or is there anything else?

Updated: a "message" will be an instance-object carrying "data only". I am not currently planning on using inheritance for conveying semantics to the said messages but rather simple properties.

Update2: after taping the collective wisdom of SO (see here), it seems that Scala is out-of-scope also.

(NOTE: Java novice here... please be gentle)

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

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

发布评论

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

评论(2

ぺ禁宫浮华殁 2024-08-23 10:34:13

您可能正在寻找的是 Javaspaces (元组的 Java 实现)空间)并根据对象的属性(在 Javaspace 世界中称为“条目”)匹配对象。

空间存储具有特定属性或条目的对象(例如关联的货币、城市、用户等)。然后,您可以通过指定 0 个或多个此类条目从空间中选择对象,从而取回 0 个或多个对象。因此,它对于消息传递和生产者/消费者场景尤其有用。

因此,您可以存储具有特定属性(例如消息类型、消费者类型等)的对象(消息),并且您的消费者将根据一组 0 个或多个属性选择这些对象。请注意,这不需要需要修改您正在存储的底层对象。您可以进程内运行一个空间(在一个 JVM 中)——这不仅仅是一种网络存储模式。

What you may be looking for are Javaspaces (a Java implementation of tuple-spaces) and matching objects based on their attributes (called 'entries' in the Javaspace world).

Spaces store objects with particular attributes or entries (e.g. an associated currency, city, user, whatever). You can then select objects from the space by specifying 0 or more such entries, and thus get back 0 or more objects. As such, it's a useful pattern for messaging and producer/consumer scenarios in particular.

So you can store your objects (messages) with particular attributes (e.g. message type, consumer type etc.) and your consumers will select these objects based on a set of 0 or more attributes. Note that this doesn't require modification of the underlying object that you're storing. You can run a space in-process (in one JVM) - it's not just a networked storage pattern.

半衾梦 2024-08-23 10:34:13

我认为你想要的模式匹配是 正则表达式,根据您的描述,您将拥有一个事件驱动程序 其中消息是事件。

不要那样做。相反,使用实际对象作为消息,这样您就不必解析任何内容(这很慢),而只需匹配类型并检查相关指令(这很快)以获取实际操作。利用所有这些制作一个状态机也不是一个糟糕的选择。

What I think you want for pattern matching is regular expressions and by your description you're going to have an event-driven program where the messages are the events.

Don't do that. Instead use actual object as messages, this way you don't have to parse anything (that's slow) but just match types and check related directives (that's fast) for actual actions. Making a state machine out of all this isn't a bad choice either.

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