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

查看:1641
本文介绍了是否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模式的示范的一部分;也就是说,几乎所有我的code是二OO-RE $ P $抽象的阶级意识psentation,以及所有code是合乎逻辑的code。

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中。

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&LT; MachineMonitor >让所有的个人 MachineMonitor 的的轨道。现在延伸到MVC的查看的一部分,我希望用户能够操纵一个TableView中显示的 MachineMonitor 如此,他们可以,例如,创建和删除新的<我的列表> 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.

所以,我可以跟踪哪些我的程序的用户想要做的(比如,创建一个 MachineMonitor 的机床#5,检查,看看是否齿轮的转/秒的低于0.5)我使用ObservableList&LT; 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&LT; MachineMonitor >,而不是一个ArrayList&LT; <我> MachineMonitor >但(让这个问题的话题),我觉得这是非常混乱的,因为它混合模式和查看code。

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.

一个幼稚的方法是使用一个ObservableList&LT; MachineMonitor >为的TableView并保留使用ArrayList的我和LT的; MachineMonitor >。然而,改变ObservableList&下作​​出。的 MachineMonitor >不影响下层列表作为每JavaFX的规格

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&LT一个ChangeListener的最佳途径; MachineMonitor >是传播到ObservableList&LT所做的更改; MachineMonitor >底层模型的ArrayList&LT; 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?

响应非常AP preciated。

Response very much appreciated.

推荐答案

我不同意你的模型类使用 ObservableList 违反了MVC的分离。一个 ObservableList 纯粹是数据重新presentation;它是视图的模型,而不是部分的一部分。我(<一href=\"https://github.com/AdamBien/airhacks-control/blob/master/src/main/java/com/airhacks/control/business/registrations/entity/Attendee.java\">and <一href=\"http://asipofjava.blogspot.be/2013/05/javafx-properties-in-jpa-entity-classes.html\">others) 使用模型重新presentations JavaFX的属性和集合在,其中在有其他的事情,我指出我如何使用是JavaFX的属性我的应用程序的所有层(还是可以的,至少)绑定到JSF(我应该指出,不是每个人都用的方法一致在服务器端使用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.)

由于现在的JavaFX是Java标准版的完全成熟的一部分,您可以添加任何额外的依赖无论是。 (安ObservableList是毫不逊色的Java SE比一个ArrayList的一部分。)

Since JavaFX is now a fully fledged part of the Java standard edition, you add no additional dependencies either. (An ObservableList is no less a part of Java SE than an ArrayList.)

此外,如果你做

List<MachineMonitor> myNonObservableList = ... ;

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

可观察名单是由不可观察列表支持,所以在 myNonObservableList 发生变化了。所以,如果你preFER你可以使用这种方法。

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天全站免登陆