JavaFX软件设计 [英] JavaFX software design
问题描述
编辑:
更具体地说,我想使用观察者模式,所以我的java应用程序在文档加载时通知,如下所示:
public class MyDocumentLoader extends Application
{
private ChangeListener< ; Worker.State>的ChangeListener;
public void setChangeListener(ChangeListener< Worker.State> changeListener)
{
this.changeListener = changeListener;
}
...
public void loadDocument(String url)
{
webEngine.getLoadWorker()。stateProperty()。的addListener(的ChangeListener);
webEngine.load(url);
}
...
}
$ b $我需要几个方法的回调成员,理想情况下,我可以有多个加载文档的类的实例,所以我可以为不同的URL设置不同的ChangeListener。
JavaFX支持大量的部署和打包策略,参考。 https://docs.oracle.com/javase/8 /docs/technotes/guides/deploy/toc.html ,并具有标准化的生命周期入口点和出口点,简化了所有这些策略的支持。
如果您正在努力初始化您的主要应用程序类,由于它由JavaFX启动器实现,您最好的选择是使用Application.init()和Application.stop()方法,如James_D指出的。
In a JavaFX application, javafx.application.Application must be subclassed, and the inherited launch() method, although it's public, must be called from within this derived class, otherwise an exception is thrown. The launch() method then uses reflection to instantiate the derived class, making it difficult to set values for the class members without losing them when launching. All that appears totally unusual to me, and I was wondering why starting a JavaFX application is so complicated, if that kind of software design (design pattern?) has a name, or if it's just bad design?
EDIT:
To be more specific, I want to use the observer pattern, so my java application gets notified when a document was loaded, like this:
public class MyDocumentLoader extends Application
{
private ChangeListener<Worker.State> changeListener;
public void setChangeListener(ChangeListener<Worker.State> changeListener)
{
this.changeListener = changeListener;
}
...
public void loadDocument(String url)
{
webEngine.getLoadWorker().stateProperty().addListener(changeListener);
webEngine.load(url);
}
...
}
I need the callback member in several methods, and ideally I can have more than one instances of the class that loads documents, so I can set different ChangeListeners for different URLs.
JavaFX supports a great number of deployment and packaging strategies, ref. https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/toc.html, and having a standardized lifecycle entry- and exit-point simplifies supporting all these strategies.
If you are struggling to initialize your main application class, due to it being instanciated by the JavaFX launcher, your best option is to use the Application.init() and Application.stop() methods, as James_D points out.
这篇关于JavaFX软件设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!