简体   繁体   English

寻找适合此应用程序的GUI设计模式

[英]Looking for a GUI design-pattern that fits this application

I'm developing a desktop app in java for a school project and I want to know how should I be designing the code of the GUI, specially because I will later have to run JUnit and functional tests on the app. 我正在为学校项目开发Java桌面应用程序,我想知道我应该如何设计GUI代码,特别是因为稍后我将不得不在该应用程序上运行JUnit和功能测试。 I know it kinda sounds like an opinion-based question, but all I'm not asking for the "best" method, I just want to be pointed to a specific design pattern that can fit the need of my app. 我知道这听起来像是一个基于意见的问题,但是我并不是在要求“最佳”方法,我只是想指出一种可以满足我的应用需求的特定设计模式。

To elaborate a little more on the type of application, it's "stage based", one button leads to a different section of the app. 为了更详细地说明应用程序的类型,它是“基于阶段的”,一个按钮指向应用程序的不同部分。 One main window, only error or warning popups (only one frame). 一个主窗口,只有错误或警告弹出窗口(只有一帧)。

My current approach so far: 到目前为止,我目前的方法:
So far I've designed 3 different JPanel classes using eclipse's WindowBuilder (main menu, admin menu and user menu) and a AppGUI class that creates and empty frame with a CardLayout layout and fills it with one of the 3 panels (starts with main menu). 到目前为止,我已经使用eclipse的WindowBuilder(主菜单,管理菜单和用户菜单)和一个AppGUI类设计了3个不同的JPanel类,该类创建并使用CardLayout布局清空框架,并用3个面板之一填充它(从主菜单开始) )。 Then the buttons on such panels point to the next panel. 然后,此类面板上的按钮指向下一个面板。 (Example, main menu -> user menu -> create profile) (例如,主菜单->用户菜单->创建配置文件)
The panels need to communicate with the AppGUI instance in order to execute the panel changes and later on, the functionality that the other options will have. 面板需要与AppGUI实例进行通信,以便执行面板更改,然后再执行其他选项将具有的功能。 To do this, I've been passing the AppGUI instance as a parameter to the panels on initialization and storing it inside each panel (I first thought of having a singleton AppGUI and have static reference to it, but I later on read that it's bad practice, specially for later mock testing). 为此,我一直在初始化时将AppGUI实例作为参数传递给面板,并将其存储在每个面板中(我首先想到拥有一个Singleton AppGUI并对其进行静态引用,但后来我读到它很糟糕练习,专门用于以后的模拟测试)。
Also I'd like to have the logic for the application on different classes other than the GUI classes and I'm not sure if passing the AppGUI as parameter to every method out there is good practice or not 我也想在GUI类以外的其他类上具有应用程序的逻辑,并且我不确定将AppGUI作为参数传递给每个方法是否存在良好的实践

In conclusion , it would be great if someone could point me to a specific design pattern for this kind of GUI, thanks in advance! 总之 ,如果有人可以向我指出这种GUI的特定设计模式,那就太好了,谢谢!

I would suggest the Model View Controller (MVC) pattern. 我建议使用模型视图控制器(MVC)模式。

The idea of MVC is that the models (your app logic), the views (UI elements), and the controllers all work together. MVC的想法是模型(您的应用程序逻辑),视图(UI元素)和控制器都可以一起工作。 The models and the views are completely independent of each other ie you don't pass a model class object to a view class object or vice versa. 模型和视图彼此完全独立,即,您不会将模型类对象传递给视图类对象,反之亦然。 The models and the views should talk to the controllers and the controllers makes all the decisions. 模型和视图应与控制器对话,然后由控制器做出所有决策。

Controllers 控制器

They contain your app's UI logic, like how to layout the views. 它们包含应用程序的UI逻辑,例如如何布局视图。 They are notified by the model once in a while (eg some data have changed!). 模型会不时通知它们(例如某些数据已更改!)。 They are also notified by the views (eg the user pressed a button!). 视图也会通知它们(例如,用户按下了按钮!)。 And controllers need to respond to these notifications. 控制器需要响应这些通知。 For example, when the user pressed a button, the controller might tell another view to show, or tell the model to do some calculations or some other stuff. 例如,当用户按下按钮时,控制器可能会告诉另一个视图显示,或者告诉模型进行一些计算或其他操作。 In your case, your controller is most likely to have all the ActionListener for the buttons and text boxes on the UI. 在您的情况下,您的控制器很可能具有UI上的按钮和文本框的所有ActionListener

Models 楷模

They contain your app's logic. 它们包含您应用的逻辑。 Try to design your model to be UI independent as much as possible. 尝试将模型设计为尽可能独立于UI。 If designed correctly, your model should still work without the UI ie you should be able to make a command line version of your app without changing the model. 如果设计正确,您的模型仍可以在没有UI的情况下运行,即您应该能够在不更改模型的情况下制作应用程序的命令行版本。 For example, if you are making a calculator, the model should do the actual calculation. 例如,如果要制作计算器,则模型应进行实际计算。

Views 观看次数

Most of the time, you don't need to worry about this part because swing already provides you with lots of view classes, such as JButton , JPanel , JTextArea etc. If you happen to be creating your own views, remember that it should not contain anything related to the model ie the view should work fine with a different model. 大多数时候,您不必担心这部分,因为swing已经为您提供了许多视图类,例如JButtonJPanelJTextArea等。如果您碰巧正在创建自己的视图,请记住,它不应该包含与模型相关的任何内容,即视图应与其他模型一起正常工作。

You can always search for more info on the web. 您始终可以在网络上搜索更多信息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM