在将 SwingWorker 与需要将更新发送回 Controller 的长时间运行的进程一起使用时,如何实现 View 与模型的分离?
我可以使用
SwingWorkers
doInBackground()
通过调用例如model.doLongProcess()
来保持 EDT 响应太棒了!我遇到的问题是尝试在流程完成之前取回数据,以根据进度更新 View 。
我知道我可以通过使用
SwingWorkers
publish()
方法取回数据,但我认为这迫使我为doInBackground()
中的doLongProcess()
方法。
为了引用 MVC 实现,我看起来有点像这样:
http://www.leepoint.net/notes-java/GUI/structure/40mvc.html
/ structure/calc-mvc/CalcMVC.java -- Calculator in MVC pattern.
// Fred Swartz -- December 2004
import javax.swing.*;
public class CalcMVC {
//... Create model, view, and controller. They are
// created once here and passed to the parts that
// need them so there is only one copy of each.
public static void main(String[] args) {
CalcModel model = new CalcModel();
CalcView view = new CalcView(model);
CalcController controller = new CalcController(model, view);
view.setVisible(true);
}
}
我有一个模型类,它将许多其他类包装在一起,形成一个简单的 Controller 接口(interface)。
我真的不想将这些类中的所有/部分/任何代码移动到 Controller 中 - 它不属于那里。
更新:
这是我正在采用的方法 - 它不是最干净的解决方案,它可能被视为对 PropertyChangeSupport
.. 在语义层面上的滥用。
基本上所有具有长时间运行方法的低级类都会有一个propertyChangeSupport
字段。长时间运行的方法会定期调用 firePropertyChange()
来更新方法的状态,而不必报告属性的更改 - 这就是我所说的语义滥用!
然后包装低级类的模型类捕获这些事件并发出它自己的高级 firePropertyChange
.. controller
可以监听...
编辑:
澄清一下,当我调用 firePropertyChange(propertyName, oldValue, newValue);
- propertyName ---> 我滥用 propertyName 来表示主题名
- 旧值=null
- newValue = 我要广播的消息
然后模型中的 PropertyChangeListener 或任何地方都可以根据主题名称识别消息。
所以 Iv 基本上让系统像发布-订阅一样使用它......
我想我可以在更新的低级类中添加一个进度字段来代替上述方法,然后基于该字段进行 firePropertyChange.. 这将符合其应有的使用方式。
最佳答案
我将发布/处理对视为将数据从 SwingWorker 推送到 GUI。另一种传递信息的方法是让 GUI 或控件使用 PropertyChangeSupport 和 PropertyChangeListeners 从 SwingWorker 中拉出信息。考虑
- 为您的模型提供一个 PropertyChangeSupport 字段,
- 为其添加和删除 PropertyChangeListener 方法
- 让它通知支持对象状态的变化。
- 让 SwingWorker 向模型添加 PropertyChangeListener。
- 然后让 SwingWorker 通知控件或 View 模型状态的变化。
- SwingWorker 甚至可以对来自模型的更改信息使用发布/处理。
编辑
关于您的更新:
Basically all the low-level classes that have long running methods will have a propertyChangeSupport field. The long running methods call the firePropertyChange() periodically to update on the status of the method and not necessarily to report the change of a property - that is what I mean by semantic abuse!.
我不建议您这样做。请理解,如果被监听的绑定(bind)属性没有更改,则即使调用 firePC() 也不会通知任何 PropertyChangeListeners (PCL)。如果您需要轮询属性,那么我不会使用 PCL 来执行此操作。我会简单地对其进行投票,可能是从被投票的类(class)之外。
关于java - MVC - SwingWorker 具有一个应该更新 View 的长时间运行的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12759619/