简体   繁体   English

JavaFX应用程序-主类的设计

[英]JavaFX application - design of main class

I'm currently trying to develop application using JavaFX and I'm stuck just at the beginning. 我目前正在尝试使用JavaFX开发应用程序,而我只是一开始就陷入了困境。 I want to use MVC pattern in this implementation and also make this implementation as reusable as possible. 我想在此实现中使用MVC模式,并使此实现尽可能可重用。 Basic description of the app: 该应用的基本说明:

1) It uses configuration file in which all database connections parameters (passes, logins) and other parameters used by the app are stored. 1)它使用配置文件,其中存储了应用程序使用的所有数据库连接参数(通过,登录)和其他参数。

2) It's all about configuring of workflow of other application so it has to store two database object to two different databases and make operations through them in databases. 2)所有与配置其他应用程序的工作流有关,因此它必须将两个数据库对象存储到两个不同的数据库中,并通过它们在数据库中进行操作。

3) The design assumes that there are few views, every one has it's own controller and different functionalities can be done through them but all can trigger operations on database or configuration file. 3)该设计假定视图很少,每个视图都有其自己的控制器,并且可以通过它们来实现不同的功能,但是所有视图都可以触发对数据库或配置文件的操作。

4) The goals is also to remember the state of previous view while changing between views during usage. 4)目的还在于在使用过程中在不同视图之间切换时记住前一个视图的状态。

That's why I have this questions: 这就是为什么我有以下问题:

1) While I start the application I test the connections to the database and if successful I connect to them. 1)启动应用程序时,我测试到数据库的连接,如果成功,则连接到它们。 Is it ok that I do this in the Main class or should I have some other class in which I store all these db objects in case of referring to them in this class? 我可以在Main类中这样做吗?还是应该有其他一些类来存储所有这些db对象,以防在此类中引用它们? Is it ok that every controller will use an instance of the Main class just because it has to access these db objects (assuming I decided to not use other class in which I do all the connections)? 每个控制器是否仅因为必须访问这些数据库对象就使用Main类的实例就可以(假设我决定不使用我在其中进行所有连接的其他类)吗?

2) What is the role of the main class? 2)主班的作用是什么? Should it be used as a main view (as in MVC) and should it only launch the application and pass the further responsibility to, for example, LoginController/View...? 是否应该将其用作主视图(如在MVC中一样),并且应该仅启动应用程序并将进一步的责任传递给例如LoginController / View ...?

I would be greatefull for some clear answer which could enlighten me this problem a bit. 我会很高兴得到一些明确的答案,这可能会启发我这个问题。

There are no stupid questions, they say... :D 没有愚蠢的问题,他们说...:D

Your Main class, as pointed out by James_D, should literally be just that 正如James_D所指出的,您的Main class应该就是这样

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("ui_main.fxml"));

    stage.setScene(new Scene(root));
    stage.show();
}

Main class typically doesn't play any role in MVC. Main类通常在MVC中不起作用。 However, if you are not using any dependency injection frameworks, it is common for Main class to perform the wiring between Model, View and Controller. 但是,如果您不使用任何依赖项注入框架,则Main类通常在Model,View和Controller之间执行连接。

Model is where all the (business / domain) logic must be located. 模型是必须放置所有(业务/域)逻辑的位置。 If a controller needs to access a database, it will use an instance of Model. 如果控制器需要访问数据库,它将使用Model的实例。 Model will typically comprise a set of other objects which provide the actual functionality, eg database access, network access. 模型通常将包括一组其他对象,这些对象提供实际的功能,例如数据库访问,网络访问。 This allows maximum flexibility as functionality providing objects become separate modules and are reusable. 由于提供对象的功能成为独立的模块并且可重复使用,因此可以提供最大的灵活性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将对象从主类传递到 JavaFX 应用程序 - Pass object from main class to JavaFX Application 错误:无法找到或加载主类 application.Main JAVAFX - Error: Could not find or load main class application.Main JAVAFX JavaFX 应用程序类必须扩展 javafx.application.Application --> 即使存在 main 方法,也会出现此错误 - JavaFX application class must extend javafx.application.Application --> Getting this error eventhough main method is there JavaFX类设计 - JavaFX class design 如何将在Scene Builder中创建的场景加载到JavaFX应用程序主类中? - How to load scenes created in Scene Builder into JavaFX application main class? exo1.Batiment类中没有找到main方法,请将main方法定义为:public static void main(String[] args) 或JavaFX应用类 - Main method not found in class exo1.Batiment, please define the main method as:public static void main(String[] args) or a JavaFX application class JavaFX应用程序的主要功能不起作用 - JavaFX application main function not working 错误:无法找到或加载主类 application.Main 导致:java.lang.NoClassDefFoundError:javafx/application/Application JDK 11 - Error: Could not find or load main class application.Main Caused by: java.lang.NoClassDefFoundError: javafx/application/Application JDK 11 缺少 JavaFX 应用程序类 - Missing JavaFX application class JavaFX 应用程序类不在 javafx.application 包中 - JavaFX Application class not in javafx.application package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM