实现java对象之间通信的正确方法是什么?

发布于 2024-08-25 17:53:33 字数 331 浏览 1 评论 0原文

我正在开展一个学术项目,该项目模拟了java中相当大的排队过程。模拟器的核心位于一个包中,其中存在 8 个类,每个类都实现一个概念。项目中的每个类都遵循 SRP。这些类封装了模拟器的行为,并互连项目中的所有其他类。

出现的问题是,正如我认为逻辑上的那样,这 8 个类中的大多数都是紧密耦合的,并且每个类都必须具有此包中每个其他类的工作知识,以便能够在需要时从中调用方法。应用程序只需要每个类的一个实例,因此最好为新类中的每个类创建静态字段并使用它来进行调用,而不是在每个类中为包中的每个其他类保留引用(我我确信这是不正确的)-,但这被认为是正确的设计解决方案吗?或者是否有更适合我的需求的设计模式?

I'm working on an academic project which simulates a rather large queuing procedure in java. The core of the simulator rests within one package where there exist 8 classes, each one implementing a single concept. Every class in the project follows SRP. These classes encapsulate the behavior of the simulator and inter-connect every other class in the project.

The problem that has arisen is that most of these 8 classes are, as is logical i think, tightly coupled and each one has to have working knowledge of every other class in this package in order to be able to call methods from it when needed. The application needs only one instance of each class so it might be better to create static fields for each class in a new class and use that to make calls -instead of preserving a reference in each class for every other class in the package (which I'm certain that is incorrect)-, but is this considered a correct design solution? or is there a design pattern maybe that better suits my needs?

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

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

发布评论

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

评论(3

动听の歌 2024-09-01 17:53:33

听起来你有一种复杂的状态机。您可以将对象之间的方法调用抽象为异步事件。每个对象可以将通用事件发送到“路由器”对象,而不是直接调用其他对象上的方法。路由器对象会将事件转发给在路由器上注册侦听器的任意数量的对象。您可以在侦听器或路由器算法中实施过滤器来限制谁接收事件。状态变化也将作为事件发布。
如果您使用 JMS 服务器作为“路由器”,您甚至可以将对象分布在多个主机上。
这种方法以公共事件模式/接口的形式在对象之间提供了一个简单的、可重用的接口。

It sounds you have a kind of a complex state machine. You could abstract the method calls between the objects as asynchronous events. Instead of directly calling a method on the other objects, each object could send generic events to a 'router' object. The router object would forward the event to any number of objects who registered their listeners at the router. You can implement filters in the listeners or in the router algorithm to restrict who receives the events. State changes would be published as events too.
If you use a JMS server as the 'router', you could even distribute your objects on multiple hosts.
This approach provides a simple, reusable interface between your objects in the form of a common event schema/interface.

风渺 2024-09-01 17:53:33

您是否考虑过使用接口来解耦类?您有八个类,但您可能只需要几个接口即可实现它们之间的通信/互操作性并获得很大的灵活性。

Have you thought about using interfaces to decouple the classes? You have eight classes but it's possible that you will only need a few interfaces to enable communication/interoperability between and gain a good deal of flexibility.

墨小沫ゞ 2024-09-01 17:53:33

我遇到的问题是,这 8 个类中的大多数(我认为这是逻辑上的)是紧密耦合的,并且每个类都必须具备此包中其他每个类的工作知识,以便能够从需要时使用。

没关系。 Java 组件的最小实现单元(除了各个类之外)是包。包内的类可以紧密耦合。只需确保耦合不会泄漏到外部(例如使用包保护的类)。

应用程序只需要每个类的一个实例,因此最好为新类中的每个类创建静态字段并使用它来进行调用 - 而不是在每个类中为包中的每个其他类保留引用(我确信这是不正确的)

如果这八个类中的每一个都需要拥有所有其他七个类的实例,那么跨类划分功能的方式可能并不好。如果每个类都只有一个职责范围,为什么会出现这种严重的交叉依赖呢?

这些类有状态吗?或者只是静态方法的集合?

但同样,只要这个包的公共接口看起来没问题,我就不会太担心。客户端代码是否也需要了解这八个类?

The problem that I has arisen is that most of these 8 classes are, as is logical i think, tightly coupled and each one has to have working knowledge of every other class in this package in order to be able to call methods from it when needed.

That is okay. The smallest unit of implementation (outside of individual classes) for Java components is the package. Classes within a package can be tightly coupled. Just make sure that the coupling does not leak outside (use package-protected classes for example).

The application needs only one instance of each class so it might be better to create static fields for each class in a new class and use that to make calls -instead of preserving a reference in each class for every other class in the package (which I'm certain that is incorrect)

If every of these eight classes needs to have an instance of all other seven classes, maybe the way you split functionality across classes is not good. If every class has only a single area of responsiblity, why does this heavy cross-dependency arise?

Are these classes with state? Or just a collection of methods that might as well be static?

But again, as long as the public interface of this package looks okay, I would not worry too much. Does client code also need to know about the eight classes?

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