简体   繁体   English

桌面应用程序中的MVC和Swing

[英]MVC and Swing in desktop application

After realizing that I have completely ignored the MVC pattern I have tried to utilize the concept in an application with a Swing view. 在意识到我完全忽略了MVC模式后,我试图在具有Swing视图的应用程序中使用该概念。 I have now read most of the posts on the subject of MVC with Swing but am still a bit confused, because it is too complicated for me to grasp, and I think I need some basic clarifications so I don't set off on the wrong path. 我现在已经阅读了关于MVC主题的大部分帖子,但仍然有点困惑,因为它对我来说太复杂了,我想我需要一些基本的澄清,所以我不会出错路径。

I also wonder how common it is to use MVC in real projects. 我也想知道在实际项目中使用MVC有多常见。 Many online tutorials seem to leave out the controller and mix it with the model, while I was confused by XSTL:s business logic capabilities. 许多在线教程似乎忽略了控制器并将其与模型混合在一起,而我对XSTL的业务逻辑功能感到困惑。 Why would you want to address a datasource from a JSP view? 为什么要从JSP视图中寻址数据源?

These thoughts aside, my proper question is this: 抛开这些想法,我的正确问题是:

If you have a Swing component, should event listener in that Swing class update the component state through calling (static perhaps?) methods in a POJO controller class, which in turn gets the appropriate business logic from the model, which is made up by POJO class hierarchy and associated persistence? 如果你有一个Swing组件,那么该Swing类中的事件监听器是否应该通过在POJO控制器类中调用(可能是静态的?)方法来更新组件状态,而POJO控制器类又从模型中获取适当的业务逻辑,该模型由POJO组成类层次结构和相关的持久性?

I've worked as a freelance for a long time and almost 90% of the projects were about Java Swing (Desktop applications). 我作为自由职业者工作了很长时间,几乎90%的项目都是关于Java Swing(桌面应用程序)。 Also a lot of projects involved migration from languages like Visual Fox Pro to Java, it was a pain, because the hard part is not think in the logic which is already done, the hard part is take the code that is a mess and turn it into a good-looking code following the good practices and using design patterns, that's why it is a good idea to make a schema or a map in your mind how you can separate your code following the concepts of Model, View, Controller. 还有很多项目涉及从Visual Fox Pro等语言迁移到Java,这很痛苦,因为困难的部分不是在已经完成的逻辑中思考,困难的部分是把代码弄得乱七八糟地把它转过来根据良好实践和使用设计模式制作好看的代码,这就是为什么在脑海中制作模式或地图是如何根据模型,视图,控制器的概念分离代码的好主意。

MVC as mentioned helps you to have a good-looking, maintainable and easy to read code, as well as you follow the programming paradigms and good practices. 所提到的MVC可以帮助您获得外观漂亮,易于维护且易于阅读的代码,以及遵循编程范例和良好实践。

View: Obviously, the part that interacts with the user (user interface), in case of Swing, your windows, frames, panels and all the code that involves the graphic components you need for your app. 视图:显然,在Swing的情况下,与用户(用户界面)交互的部分,您的窗口,框架,面板以及涉及应用程序所需图形组件的所有代码。

Controller: Involves the core or business logic you stablish for your application, in this "layer" you should include the functionality and the "how my application will achieve the goals?". 控制器:涉及您为应用程序建立的核心或业务逻辑,在这个“层”中,您应该包括功能和“我的应用程序将如何实现目标?”。

Model: Related with the data you manage, for example, your entities and classes that represents the data you want to manage or give maintenance. 模型:与您管理的数据相关,例如,您的实体和类,它们代表您要管理或提供维护的数据。

Applying MVC is not so hard, but as I mentioned, it could be sometimes a pain when you have to migrate your code from a not-applying-MVC structure to a MVC structured application. 应用MVC并不是那么难,但正如我所提到的,当您必须将代码从非应用MVC结构迁移到MVC结构化应用程序时,有时可能会很痛苦。 It is easier to start coding using MVC. 使用MVC开始编码更容易。

A way I get used to it is by using maven and separate my application into little "modules", of course, you don't need maven, I just found it useful in that moment, but in any case you can try practicing or get used to MVC by separating your application into little projects, for instance: 我习惯的一种方法是使用maven并将我的应用程序分成小的“模块”,当然,你不需要maven,我只是觉得它在那一刻很有用,但无论如何你可以尝试练习或获得通过将应用程序分成小项目来用于MVC,例如:

Java Project 1: application-data-model (contains all the code related with data management: entities, dtos, beans, daos) Java Project 1:应用程序数据模型(包含与数据管理相关的所有代码:实体,dtos,bean,daos)

Java Project 2: application-core-controller (contains all the business logic and functionality, you can use a facade pattern here if you want to make your code more "transparent" when you relate with your view) Java Project 2:application-core-controller(包含所有业务逻辑和功能,如果您希望在与视图相关时使代码更“透明”),可以在此处使用Facade模式)

Java Project 3: application-view-ui (contains all the panels, frames and graphic components) Java Project 3:application-view-ui(包含所有面板,框架和图形组件)

Working this way helped me (and forced me) to get used to separate my code and keep an eye on what really matters to the project I'm working on. 以这种方式工作帮助我(并强迫我)习惯于分离我的代码,并密切关注我正在研究的项目真正重要的事情。 For instance, if I'm on application-data-model I'm focused in data model, I'm not thinking in business logic nor graphic interface. 例如,如果我在应用程序数据模型上,我专注于数据模型,我不会考虑业务逻辑或图形界面。

Long explanation, maybe somebody could do it better, but hope I could have helped you or at least gave you a hand with this. 很长的解释,也许有人可以做得更好,但希望我能帮助你或至少帮助你。

Best regards. 最好的祝福。

Firs the URL for basic understanding of MVC Firs URL用于基本理解MVC

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Now the approach to implement it in Swing applications. 现在是在Swing应用程序中实现它的方法。 Don't get confused with controller functionality with the listeners functionality. 不要将控制器功能与侦听器功能混淆。

  1. UI controls and listeners attached to them should be defined in your view classes. 应在视图类中定义附加到它们的UI控件和侦听器。
  2. On any event, whenever you need to invoke a business logic, then you need to call the controller class. 在任何情况下,无论何时需要调用业务逻辑,都需要调用控制器类。 Like fetching some value from the database. 就像从数据库中获取一些值一样。
  3. Controller class should talk to your model to fetch the data and manipulate it if required. 控制器类应与您的模型通信以获取数据并在需要时对其进行操作。
  4. Model classes should work on the data. 模型类应该处理数据。

The idea of using MVC is to reduce redundant code and more manageable code. 使用MVC的想法是减少冗余代码和更易于管理的代码。 So if you are doing some calculations/manipulations then those can be moved to Controllers. 因此,如果您正在进行一些计算/操作,那么可以将这些计算/操作移动到控制器。 Controllers can be called from different views requiring the same stuff. 可以从需要相同内容的不同视图调用控制器。 Similarly model can be used by multiple controllers to fetch the data. 多个控制器可以使用类似的模型来获取数据。

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

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