3

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

Please provide some code!

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

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

  2. 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;

  3. 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;

  4. 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.

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?

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.

4

1 回答 1

4

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

不过,看看如何应用这些原则可能会很有用。假设您正在遵循本教程并已在 a 中实现了WatchServicea ,如此SwingWorker所示。现在,假设您希望主视图 a更新自身以响应到达的。您将安排相应的将自己注册为工人,如此此处所示。当到达时,您更新并触发适当的事件来更新. 已知的实现已经包括所需的方法。JTableWatchEventTableModelPropertyChangeListenerPropertyChangeEventTableModelJTableTableModel

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

对于设置,请考虑此处java.util.Preferences提到的,或此处讨论的。javax.jnlp.BasicService

于 2012-06-09T00:57:44.303 回答