如何在Java Swing中正确地在两个不同的MVC控制器之间进行通信? [英] How to correctly communicate between two different MVC Controllers in Java Swing?

查看:154
本文介绍了如何在Java Swing中正确地在两个不同的MVC控制器之间进行通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能解释一下Controller如何以简单但仍然正确的方式调用另一个Controller方法吗?

Can you explain me how a Controller can call another Controller method in a simple but still correct way?

请提供一些代码!

背景:
我有2个不同的Controllers-View-Model和2个库,他们之间需要相互沟通:

background: I Have 2 different Controllers-View-Model and 2 libraries and they need to communicate between each other:


  1. SettingsWindow / SettingsController / SettingsModel:负责应用程序设置。该模型是单例,因为我需要整个应用程序的设置数据;

  1. SettingsWindow/SettingsController/SettingsModel: Responsible for the app settings. The model is a singleton because I need the settings data all over the app;

用于监控目录的库,每次创建文件时都会创建一个事件在特定目录中。受监视的目录路径在SettingsModel中定义;我正在使用Java 7 WatchService API;

A Library for Monitoring a directory which creates an event every time a file is created in a specific directory. The monitored dir path is defined in the SettingsModel; I'm using the Java 7 WatchService API for that;

用于监控网络服务器和下载新文件的库。 Web服务器地址和保存目录都在SettingsModel中定义;我正在使用HttpsUrlConnection和一个计时器;

A Library for Monitoring a webserver and download new files. The webserver address and the save directory are both defined in the SettingsModel; I'm using HttpsUrlConnection and a timer for that;

MainWindow / MainController / MainModel:负责主应用程序窗口,它有一个必须更新的表每次在受监控目录中创建新文件时,以及每次上述库下载文件时。

MainWindow/MainController/MainModel: Responsible for the main app window, which has a table that must be updated every time a new file is created in the monitored directory, and also everytime a file is downloaded by the above libraries.

那么我如何正确地实例化和传达这4种不同的功能?

他们的控制器如何在它们之间进行通信,因为它们都是相关的?
我应该如何根据MVC组织这个项目?

So how do I correctly Instantiate and communicate those 4 different features?
How do their Controllers commnuicate between them, since they are all related? How should I organize this project in terms of MVC?

请提供基本的MVC脚手架(骨架)并显示我在哪里创建实例,在哪里以及如何我称彼此为方法。我是Java新手,特别是swing和mvc。我已经阅读了很多,但我遇到了这种情况。

PLEASE provide the basic MVC scaffold (skeleton) and show where I create the instances, where and how I call each others method. I'm new to Java, specially swing and mvc. I've read a lot, but I got stuck in this situation.

推荐答案

正如建议的那样模型 - 视图 - 控制器观察者等模式表示重复出现的设计元素。一个简单的大纲可以说明类的交互,但一般的解决方案超出了StackOverflow的范围。

As suggested here, patterns such as model-view-controller and observer represent recurring design elements. A simple outline may illustrate the interaction of classes, but a general solution is beyond the scope of StackOverflow.

但是,看看如何应用这些原则可能会有所帮助。假设您正在关注教程并已实施 WatchService SwingWorker 中,如图所示这里。现在,假设您希望主视图( JTable )更新自身以响应到达的 WatchEvent 。您将安排相应的 TableModel 将工作人员注册为 PropertyChangeListener ,显示为此处此处。当 PropertyChangeEvent 到达时,您更新 TableModel 并触发相应的事件以更新 JTable 。已知的 TableModel 实现已经包含了所需的方法。

Still, it may be useful to look at how the principles might be applied. Suppose you are following the tutorial and have implemented a WatchService in a SwingWorker, as shown here. Now, suppose you want the main view, a JTable, to update itself in response to an arriving WatchEvent. You would arrange for the corresponding TableModel to register itself with the worker as a PropertyChangeListener, shown here and here. When the PropertyChangeEvent arrives, you update the TableModel and fire the appropriate event to update the JTable. The known TableModel implementations already include the required methods.

类似的方法将用于更新响应表来自网络的数据。

A similar approach would be used to update the table in response to data arriving from the web.

对于设置,请考虑 java.util.Preferences ,提到 ,或 javax.jnlp.BasicService ,讨论过其他

For settings, consider java.util.Preferences, mentioned here, or javax.jnlp.BasicService, discussed here.

这篇关于如何在Java Swing中正确地在两个不同的MVC控制器之间进行通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆