简体   繁体   English

结合MVC,DAO /存储库模式和针对Java GUI应用程序的Swing

[英]Combining MVC, DAO/Repository Pattern and Swing for Java GUI Applications

I am trying to create a graphical flashcards app from scratch. 我正在尝试从头开始创建图形抽认卡应用程序。 I have a few questions: 我有几个问题:

a. 一种。 I have used Swing to build some apps in the past, calculator app. 我过去曾使用Swing构建一些应用程序,即计算器应用程序。 But I felt that was a trivial application so I want to ramp up my skills as a Java developer. 但是我觉得那是一个琐碎的应用程序,所以我想提高自己作为Java开发人员的技能。

b. b。 I have been told that a gold standard is to build a small application that uses one of these: MVC, MVM, MVVM and so on.And since I am learning design patterns, I was hoping to use it in the application. 有人告诉我一个黄金标准是构建一个使用以下其中一个的小型应用程序:MVC,MVM,MVVM等。由于我正在学习设计模式,所以我希望在应用程序中使用它。

c. C。 My classes are such: 我的课程是这样的:

  • A model: Flashcard.java(It has a list of answers, a list of pictures, a question), A FlashCard Manager(to perform CRUD) 一个模型:Flashcard.java(具有答案列表,图片列表,问题),FlashCard Manager(用于执行CRUD)
  • Different view classes: GUI interface 不同的视图类:GUI界面
  • Controller: All the event listeners 控制器:所有事件监听器
  • App: To initialize the app 应用:初始化应用

d. d。 I have tried to read online examples of such a combination and what was proposed in for the Manager or DAO, was to have it connect to JDBC for the database. 我已经尝试在线阅读这种组合的示例,并且为Manager或DAO提出的建议是将其连接到数据库的JDBC。 I am trying to simulate that by using a HashMap> for the same purpose.Is that correct or do I have to use JDBC or SQLite? 我试图通过出于相同目的使用HashMap>进行模拟。这是正确的还是必须使用JDBC或SQLite?

e. e。 Also am I supposed to have a controller-view pair, that is for every JComponent that uses an event listener or a controller for windows( startup window, main application window, child windows)? 我是否也应该有一个控制器-视图对,即每个使用事件侦听器或Windows(启动窗口,主应用程序窗口,子窗口)控制器的JComponent?

f. F。 Also should the controller class be an interface for all these controllers? 控制器类也应该是所有这些控制器的接口吗?

I do not have code because I am still developing it but I just wanted to get a general idea. 我没有代码,因为我仍在开发它,但我只是想获得一个总体思路。 Thanks for answering my questions. 谢谢回答我的问题。

b. b。 I have been told that a gold standard is to build a small application that uses one of these: MVC, MVM, MVVM and so on.And since I am learning design patterns, I was hoping to use it in the application. 有人告诉我一个黄金标准是构建一个使用以下其中一个的小型应用程序:MVC,MVM,MVVM等。由于我正在学习设计模式,所以我希望在应用程序中使用它。

This both right and wrong. 这是对是非。 Some API's simply don't support the notion of a pure MVC (or variation). 某些API根本不支持纯MVC(或变体)的概念。 Swing for example implements a form of MVC of it's own which is more like M-VC, where the model is separate, but the view and controller are bound. 例如,Swing实现了自己的MVC形式,更类似于M-VC,其中模型是独立的,但是视图和控制器是绑定的。

Think about a JButton . 考虑一下JButton Do you ever apply a controller to it? 您是否曾经将控制器应用于该控制器? You can add listeners to it, you can even add listeners directly to it's model, but it has it's own internal control mechanism, which generates events based on keyboard and mouse interaction. 您可以向其添加侦听器,甚至可以直接向其模型添加侦听器,但是它具有自己的内部控制机制,该机制根据键盘和鼠标的交互作用生成事件。

You need to beware of when a MVC might not be suitable or where the amount of work to implement a pure MVC is not worth the effort. 您需要注意什么时候MVC可能不合适,或者在什么地方实现纯MVC的工作量是不值得的。

Having said that, you can wrap Swing UI's in another MVC layer, but you need to think about it at a different level. 话虽如此,您可以将Swing UI包裹在另一个MVC层中,但是您需要在另一个层次上加以考虑。 Instead of each controller been considered a "view", you look at the container, which might contain multiple controls and see it as the "view", which a controller then manages. 而不是将每个控制器都视为“视图”,而是查看容器,该容器可能包含多个控件,并将其视为“视图”,然后由控制器进行管理。

d. d。 I have tried to read online examples of such a combination and what was proposed in for the Manager or DAO, was to have it connect to JDBC for the database. 我已经尝试在线阅读这种组合的示例,并且为Manager或DAO提出的建议是将其连接到数据库的JDBC。 I am trying to simulate that by using a HashMap> for the same purpose.Is that correct or do I have to use JDBC or SQLite? 我试图通过出于相同目的使用HashMap>进行模拟。这是正确的还是必须使用JDBC或SQLite?

This is not an unreasonable idea, however, you want to ensure that the public interface remains constant. 这不是一个不合理的想法,但是,您要确保公共接口保持不变。 This would allow you to change the implementation at a later date to use JDBC or a web service without having to rewrite the code that depends on it 这样一来,您以后就可以更改实现以使用JDBC或Web服务,而不必重写依赖于它的代码。

e. e。 Also am I supposed to have a controller-view pair, that is for every JComponent that uses an event listener or a controller for windows( startup window, main application window, child windows)? 我是否也应该有一个控制器-视图对,即每个使用事件侦听器或Windows(启动窗口,主应用程序窗口,子窗口)控制器的JComponent?

This will depend. 这将取决于。 I tend to avoid exposing individual components, as this exposes them to unwanted modifications (you may not want a controller to be able to change the text of a button for example) 我倾向于避免暴露单个组件,因为这会使它们遭受不必要的修改(例如,您可能不希望控制器能够更改按钮的文本)

As a general rule of thumb, I try to make the relationship between the controller and the view as vanilla as possible, meaning that the controller shouldn't care about how the view is implemented, only that it upholds the contract between the it subscribes to. 作为一般的经验法则,我尝试使控制器和视图之间的关系尽可能地亲切,这意味着控制器不应该在乎视图的实现方式,而只是维护它所订阅的视图之间的契约。 。

This generally means I have a functional view, which is contracted to produce certain events/data and allows certain interaction (from the controller) 通常这意味着我拥有一个功能视图,该视图被收缩以生成某些事件/数据并允许某些交互(来自控制器)

f. F。 Also should the controller class be an interface for all these controllers? 控制器类也应该是所有这些控制器的接口吗?

IMHO, yes. 恕我直言,是的。 This allows a view/controller/model to be implemented differently, but allows the rest of the code to continue working with it without the need to be modified. 这允许以不同的方式实现视图/控制器/模型,但是无需修改即可允许其余代码继续使用它。

The bottom line is, implementing a MVC over Swing is not as simple as people think it is. 最重要的是,在Swing上实现MVC并不像人们认为的那么简单。 It takes some planning and consideration, but it is doable. 它需要一些计划和考虑,但是是可行的。

For a more detailed example, have a look at Java and GUI - Where do ActionListeners belong according to MVC pattern? 有关更详细的示例,请查看Java和GUI-根据MVC模式,ActionListeners在哪里?

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

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