在 JavaFX 中使用 ObservableList 是否违背了模型-视图-控制器分离? [英] Does the use of ObservableList in JavaFX go against Model-View-Controller separation?

查看:46
本文介绍了在 JavaFX 中使用 ObservableList 是否违背了模型-视图-控制器分离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试研究 JavaFX,因为我想将它用作我的程序的 GUI.我的问题本质上是一个概念性的问题:

I am attempting a study of JavaFX because I want to use it as the GUI of my program. My question is essentially a conceptual one:

到目前为止,我的程序主要是 MVC 模式的模型"部分;也就是说,几乎我所有的代码都是类意义上的抽象的 OO 表示,并且所有这些代码都是逻辑代码.

To date my program is mostly the "Model" part of the MVC pattern; that is, almost all of my code is the OO-representation of abstractions in the sense of classes, and all of that code is logical code.

由于我不想成为我程序的唯一用户,所以我想添加 MVC 的视图"部分,以便人们可以轻松使用和操作我程序的模型"部分.为此,我想使用 JavaFX.

Since I do not want to be the only user of my program, I want to add the "View" part of MVC so that people can easily use and manipulate the "Model" part of my program. For this, I want to use JavaFX.

在我的模型"类中,我显然使用了来自 Java 集合 API 的各种列表、地图和其他类.为了让我的程序的用户操作这些底层列表和映射,我想使用 JavaFX 中的 Observable(List/Map) 接口.

In my "Model" classes I obviously use various Lists, Maps, and other classes from the Java Collections API. In order to let the users of my program manipulate these underlying Lists and Maps I want to use the Observable(List/Map) interfaces in JavaFX.

一个让情况更清晰的具体例子:

假设我有一个 MachineMonitor 类,它每 3 分钟检查一次机器的某些属性,例如连接是否仍然良好,齿轮转动的速度等.如果确定满足不等式(假设齿轮速度下降到 1 转/秒的速度)MachineMonitor 触发 RestartMachineEvent.

Let's say that I have a MachineMonitor class that every 3 minutes checks certain properties of a Machine, such as if the connection is still good, the speed that the gears are turning, etc. If certain inequalities are met (say that the speed of the gears has fallen to a rate of 1 turn/sec) the MachineMonitor fires a RestartMachineEvent.

目前我使用一个 ArrayList<MachineMonitor> 来跟踪所有单独的 MachineMonitor.现在扩展到 MVC 的视图"部分,我希望用户能够操作显示 MachineMonitor 列表的 TableView,以便他们可以,例如,创建和删除新的 MachineMonitor 用于监控各种机器.

Currently I use an ArrayList<MachineMonitor> to keep track of all of the individual MachineMonitor's. Now extending to the "View" part of MVC, I want the User to be able to manipulate a TableView that displays the list of MachineMonitors so that they can, for instance, create and remove new MachineMonitor's to monitor various Machines.

这样我就可以跟踪我的程序的用户想要做什么(例如,为 Machine #5 创建一个 MachineMonitor 以检查齿轮的转数/秒是否低于0.5) 我使用 ObservableList<MachineMonitor> 作为 TableView 的基础列表.

So that I can keep track of what the user of my program wants to do (say, create a MachineMonitor for Machine #5 that checks to see if the turn/sec of the gears falls below 0.5) I use an ObservableList<MachineMonitor> as the underlying List for the TableView.

链接我的程序的模型"和视图"的最简单方法就是将模型"类更改为具有 ObservableList<MachineMonitor> 而不是 ArrayList<MachineMonitor> 但是(进入问题的主题)我觉得这很混乱,因为它混合了模型"和视图"代码.

The easiest way to link the "Model" and "View" of my program would simply be to change the "Model" class to have an ObservableList<MachineMonitor> and not an ArrayList<MachineMonitor> but (getting to the topic of the question) I feel that this is very messy because it mixes "Model" and "View" code.

一种天真的方法是对 TableView 使用 ObservableList<MachineMonitor>,并保留使用我的 ArrayList<MachineMonitor>.但是,根据 JavaFX 规范,对 ObservableList<MachineMonitor> 所做的更改不会影响基础列表.

A naïve approach would be to use an ObservableList<MachineMonitor> for the TableView and retain the use of my ArrayList<MachineMonitor>. However, changes made to the ObservableList<MachineMonitor> do not affect the underlying List as per the JavaFX specifications.

鉴于此,解决此难题的最佳方法是为 ObservableList<MachineMonitor> 创建一个 ChangeListener,它传播"对 ObservableList 所做的更改<MachineMonitor> 到底层的模型"ArrayList<MachineMonitor>?也许把它放在一个叫做 MachineMonitorController 的类中?

Given this, is the best way to solve this conundrum to make a ChangeListener for the ObservableList<MachineMonitor> that "propagates" the changes made to the ObservableList<MachineMonitor> to the underlying "Model" ArrayList<MachineMonitor>? Perhaps put this in a class called MachineMonitorController?

这种临时解决方案看起来非常混乱且不理想.

This ad-hoc solution seems very messy and non-ideal.

我的问题是:在这种情况下,在模型"和视图"之间保持几乎完全分离的最佳方法是什么?

My question is: What is the best way to retain nearly complete separation between the "Model" and "View" in this scenario?

推荐答案

我不同意在你的模型"中使用 ObservableList类违反了 MVC 分离.ObservableList 是纯粹的数据表示;它是模型的一部分,而不是视图的一部分.我 ( 其他) 使用JavaFX我的应用程序的所有层中模型表示中的属性和集合.其中,我指出了我如何使用绑定到(或至少可以)绑定到 JSF 的 JavaFX 属性.(我应该提一下,并不是每个人都同意在服务器端使用 FX 属性的方法;但是我真的看不出有任何方法可以证明它们在某种程度上是视图的一部分.)

I disagree that using an ObservableList in your "model" class violates MVC separation. An ObservableList is purely data representation; it is part of the model and not part of the view. I (and others) use JavaFX properties and collections in model representations in all tiers of my applications. Among other things in there, I point out how I use JavaFX properties that are (or can be, at least) bound to JSF. (I should mention that not everyone agrees with the approach of using FX properties on the server side; however I don't really see any way to make the argument that they are somehow part of the view.)

另外,如果你这样做

List<MachineMonitor> myNonObservableList = ... ;

ObservableList<MachineMonitor> myObservableList = FXCollections.observableList(myNonObservableList);
myObservableList.add(new MachineMonitor());

可观察列表由不可观察列表支持,因此更改也发生在 myNonObservableList 中.因此,如果您愿意,可以使用这种方法.

the observable list is backed by the non-observable list, so the change occurs in myNonObservableList too. So you can use this approach if you prefer.

这篇关于在 JavaFX 中使用 ObservableList 是否违背了模型-视图-控制器分离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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