避免 Swing GUI 中的循环引用
也许在这种情况下不值得担心,但假设您有两个类,一个包含所有组件的 JFrame 和一个处理来自远程客户端的请求的类似服务器的类。用户能够通过 GUI 启动和停止服务器对象,并显示每个服务器对象发生的各种事件。无论我是否使用显式模式(如 MVC),JFrame 似乎都需要对服务器类的引用(以调用启动和停止),并且服务器需要对 JFrame 的引用(以通知它某些事件) 。
这是一个问题,还是我以错误的方式看待这种情况?
Maybe it's not worth worrying about in this scenario, but lets say you have two classes, a JFrame with all its components, and a server-like class that handles requests from remote clients. The user is able to start and stop server objects through the GUI, and is shown various events that happen to each server object. Whether or not I use an explicit pattern (like MVC), it seems like the JFrame needs a reference to the server class (to call start and stop) and the server needs a reference to the JFrame (to notify of it of certain events).
Is this a problem, or am I looking at this situation in the wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这听起来像是应用监听器模式的地方。您的服务器可能有一个名为 addSomethingListener(SomethingListenerlistener) 的方法,JFrame 通过 SomethingListener 的实现来调用该方法。然后,只要发生适当的事件,您的服务器就会调用侦听器的方法。
This sounds like a place to apply the Listener pattern. Your server could have a method called addSomethingListener(SomethingListener listener), which the JFrame calls with an implementation of SomethingListener. Your server then would call the listener's methods whenever appropriate events happen.