java 用什么?MVC、MVP 或 MVVM 还是……?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2105121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:28:58  来源:igfitidea点击:

What to use? MVC, MVP or MVVM or…?

javamodel-view-controllermvvmmvp

提问by Andrea

I will start a Java project to develop a desktop application. What to use as presentation layer pattern (MVC, MVP,MVVM or....)?

我将启动一个 Java 项目来开发桌面应用程序。什么用作表示层模式(MVC、MVP、MVVM 或......)?

...if it is possible, with some working small example... :-)

...如果可能的话,用一些有效的小例子... :-)

回答by Pascal Thivent

Actually, the ultimate post you're looking for is this answerthis answerfrom Karsten Lentzsch (of JGoodies fame) in the Swing Frameworks and Best PracticesSwing Frameworks and Best Practicesthread.

其实,你在寻找最终的帖子是这样回答这个答案从卡斯滕Lentzsch共同领导(JGoodies数据的名望)在秋千框架和最佳实践旋启式框架和最佳实践主题。

Hello,

I've been writing Swing apps for several years that many people find elegant. And I teach developers in working with Swing efficiently: how to structure and assemble applications, how to bind and validate date, as well as how to find, design, layout, and implement well-designed screens in Swing.

I'd say there are no "best practices" for Swing available online. And I doubt that anyone can provide "best" practices, because that would require discussions among Swing developers and solution providers about approaches, what works well technically and what is easy to understand, and work with. Also, best practices would explain who can achieve what result quality, and they would describe the production time and production costs; but I could never find these information online or in a Swing book.

In my opinion, the lack of good practices and information how to build a Swing app is the biggest obstacle developers face when starting with Swing. Most developers I worked with were slow in finding design, implementing layouts, building panels, binding data, handling events, and arranging the different code parts and code layers. And many Swing apps I've seen suck; besides their poor visual design they are difficult to understand and even small changes costs a lot. Typically developers had no clue where to put what code?, how to separate concerns?, how to tie things together?, how to work with Actions?, how to launch an app?, and how to store and restore UI state? Most developers lack guidelines to follow.

But I've found that the average developer can work well with Swing, if only taken by the hand - in about 3 to 10 days. Almost all of your questions can be answered or addressed by code, libraries, the application architecture, patterns, general programming practices, well-designed examples, tutorials, etc. I teach a Swing development process that is built around a 3-tier architecture that seperates the domain, tool and presentation layers and that is based on a productive layout system. Even though most parts are done programmatically developers get results quickly and both the code and visual design becomes quite consistent. This can boost a developer's productivity a lot - to the extent that the Swing work takes a significantly smaller fraction of the project work.

So what can you do? I strongly recommend to study Martin Fowler's draft for further "Patterns of Enterprise Application Architecture". I've found that these patterns work really well with Swing and Swing teams - even for developers that are new to Swing and the Swing architecture. I'd pick the following patterns first: "Presentation Model", "Separated Domain", and "Separated Presentation". I personally favor the Presentation Model (Application Model for Smalltalkers), over the Model-View-Presenter pattern (MVP). However, MVP is a true and well studied alternative.

Fowler's patterns can be combined with a 3-client-tier architecture that scales well for moderately large Swing applications. It consists of a domain layer, a presentation layer, and a mediating model layer. I've outlined this architecture in my data binding presentation. MVP-based apps can be structured in these 3 client tiers too.

A key task for Swing applications is the data binding: how to connect domain objects and domain object properties to the Swing components. Basically you can copy your data back and forth, or build chains of adapters from your domain objects to the UI components. The copying approach is easy to understand and often the first choice for those who are new to Swing or data binding; I'd say this is a good choice. On the other hand, copying makes it much harder to synchronize views. Adapter chains and automatic or semi-automatic updates can significantly reduce the amount of code necessary to bind domain data to the UI. The downside is, that this approach is much harder to understand. As you've pointed out, Swing provides no great abstraction for a reusable and flexible model that can be used to bind text fields; the Document interface isn't appropriate for generic data access. There are a few libraries available that provide a ValueModel interface that is just intended to add a generic, powerful, and flexible model for single-valued data: Strings, booleans, numbers, dates, etc.

I'm not aware of a Swing book that explains a true Swing application development process. Ideally such a book would combine the patterns, architecture, and data binding techniques mentioned above and would describe how to implement it in Swing. Anyway, there's a 10-years old documentation for a Smalltalk application development process that does just that. Oracle's JClient architecture and documentation is not that complete but may be easier to read for Java developers. I provide a presentation about data binding that is about the Fowler patterns, a 3-tier architecture and a Swing implementation for these patterns and an automatic data binding. The tutorial sources of my Binding library may help you get aquainted with adapter chains and the ValueModel interface. The best documentation for the MVP pattern can be found in the Dolphin Smalltalk docs.

Once you've choosen your architecture and desktop pattern set, you should address the following more basic Swing tasks: 1) improve the appearance by choosing a set of professional look&feels appropriate for your target platform set, 2) choose a layout system that helps you build well designed and consistent screens quickly, 3) choose a data validation solution, and 4) grab a bag of solutions for everyday Swing tasks.

There are a couple of projects that outline a Swing architecture, address the data binding and typical Swing tasks, for example: Sun's JDNC, Oracle's JClient/ADF, the Spring RCP, the NetBeans platform. I provide a commercial suite of Swing solutions that is based on the open source JGoodies libraries and adds a bag of solutions and sources for all public JGoodies tools and demos. These sources are intended to explain how to tie together all issues mentioned above.

Let me add my standard warning about so called "MVC" frameworks. Swing doesn't use MVC, it uses a modified pattern. MVC is frequently misquoted and misunderstood - especially in the context of Swing. Also, MVC is good for UI components, not for applications. Hence I recommend to look for concepts, solutions, and libraries that reflect and work with the Swing architecture, not MVC.

The inventor community of the MVC pattern introduced the ApplicationModel (now known as Presentation Model) around 1993; MVP followed a bit later. In my opinion these two patterns are much more useful for Swing than MVC. Recently the environment that brought the Presentation Model pattern to a larger audience moved on to a new architecture: "Pollock". Interested readers may google to see how that differs from the adapter chains that are often combined with Presentation Models.

Last but not least a personal statement. I can work much better with Swing than with other toolkits or frameworks I used before; I can do more with less code, the code is better structured, it's easier to maintain, and I get results quickly.

Hope this helps. Best regards, Karsten Lentzsch

References: Fowler's further patterns - http://martinfowler.com/eaaDevData binding presentation - http://www.jgoodies.com/articles/Smalltalk app dev process - http://www.cincom.com/downloads/pdf/AppDevGuide.pdfMVP pattern documentation - http://www.object-arts.com/EducationCentre/Patterns/MVP.htmSun's JDNC project home - http://jdnc.dev.java.net/Oracle's ADF FAQ - http://www.oracle.com/technology/products/jdev/htdocs/905/adffaq_otn.htmlSpring RCP project home - http://www.springframework.org/spring-rcp.htmlNetBeans platform home - http://www.netbeans.org/products/platform/JGoodies Swing Suite - http://www.jgoodies.com/products/index.html

你好,

几年来,我一直在编写许多人认为优雅的 Swing 应用程序。我还教开发人员有效地使用 Swing:如何构建和组装应用程序,如何绑定和验证日期,以及如何在 Swing 中查找、设计、布局和实现精心设计的屏幕。

我想说没有可在线使用的 Swing 的“最佳实践”。而且我怀疑任何人都可以提供“最佳”实践,因为这需要 Swing 开发人员和解决方案提供商之间就方法、技术上有效的方法以及易于理解和使用的方法进行讨论。此外,最佳实践将解释谁可以达到什么样的结果质量,他们将描述生产时间和生产成本;但我永远无法在网上或 Swing 书中找到这些信息。

在我看来,缺乏如何构建 Swing 应用程序的良好实践和信息是开发人员在开始使用 Swing 时面临的最大障碍。我合作过的大多数开发人员在寻找设计、实现布局、构建面板、绑定数据、处理事件以及安排不同的代码部分和代码层方面都很缓慢。我见过的许多 Swing 应用程序都很糟糕;除了它们糟糕的视觉设计之外,它们很难理解,即使是很小的改动也会花费很多。通常,开发人员不知道把什么代码放在哪里?如何分离关注点?如何将事情联系在一起?如何使用 Actions?,如何启动应用程序?以及如何存储和恢复 UI 状态?大多数开发人员缺乏可遵循的指导方针。

但我发现,普通开发人员可以很好地使用 Swing,如果只是亲自动手 - 大约需要 3 到 10 天。几乎所有的问题都可以通过代码、库、应用程序架构、模式、通用编程实践、精心设计的示例、教程等来回答或解决。 我教了一个围绕 3 层架构构建的 Swing 开发过程分离领域、工具和表现层,这是基于高效的布局系统。尽管大多数部分都是以编程方式完成的,但开发人员很快就能获得结果,并且代码和视觉设计都变得非常一致。这可以极大地提高开发人员的工作效率——在某种程度上,Swing 工作只占项目工作的一小部分。

所以,你可以做什么?我强烈建议研究 Martin Fowler 的草稿,以进一步了解“企业应用程序架构模式”。我发现这些模式非常适合 Swing 和 Swing 团队——即使对于不熟悉 Swing 和 Swing 架构的开发人员也是如此。我会首先选择以下模式:“演示模型”、“分离域”和“分离演示”。我个人更喜欢演示模型(Smalltalkers 的应用程序模型),而不是模型-视图-演示者模式(MVP)。然而,MVP 是一个真正的、经过充分研究的替代方案。

Fowler 的模式可以与 3 客户端层体系结构相结合,该体系结构可以很好地适应中等规模的 Swing 应用程序。它由域层、表示层和中介模型层组成。我在我的数据绑定演示中概述了这种架构。基于 MVP 的应用程序也可以在这 3 个客户端层中构建。

Swing 应用程序的一个关键任务是数据绑定:如何将域对象和域对象属性连接到 Swing 组件。基本上,您可以来回复制数据,或者构建从域对象到 UI 组件的适配器链。复制方法易于理解,通常是刚接触 Swing 或数据绑定的人的首选;我会说这是一个不错的选择。另一方面,复制使得同步视图变得更加困难。适配器链和自动或半自动更新可以显着减少将域数据绑定到 UI 所需的代码量。缺点是,这种方法更难理解。正如您所指出的,Swing 没有为可用于绑定文本字段的可重用且灵活的模型提供很好的抽象;Document 接口不适用于通用数据访问。有一些库提供了 ValueModel 接口,该接口旨在为单值数据添加通用、强大且灵活的模型:字符串、布尔值、数字、日期等。

我不知道解释真正的 Swing 应用程序开发过程的 Swing 书籍。理想情况下,这样的一本书将结合上述模式、体系结构和数据绑定技术,并描述如何在 Swing 中实现它。不管怎样,有一个 10 年前的 Smalltalk 应用程序开发过程文档就是这样做的。Oracle 的 JClient 体系结构和文档并不完整,但对于 Java 开发人员来说可能更容易阅读。我提供了有关 Fowler 模式、3 层体系结构和这些模式的 Swing 实现以及自动数据绑定的数据绑定的演示。我的 Binding 库的教程源可以帮助您熟悉适配器链和 ValueModel 接口。

一旦你选择了你的架构和桌面模式集,你应该解决以下更基本的 Swing 任务:1) 通过选择一组适合你的目标平台集的专业外观来改善外观,2) 选择一个可以帮助你的布局系统快速构建设计良好且一致的屏幕,3) 选择数据验证解决方案,以及 4) 为日常 Swing 任务准备一袋解决方案。

有几个项目概述了 Swing 架构,解决了数据绑定和典型的 Swing 任务,例如:Sun 的 JDNC、Oracle 的 JClient/ADF、Spring RCP、NetBeans 平台。我提供了一套基于开源 JGoodies 库的 Swing 解决方案商业套件,并为所有公共 JGoodies 工具和演示添加了一包解决方案和源代码。这些来源旨在解释如何将上述所有问题联系在一起。

让我添加关于所谓的“MVC”框架的标准警告。Swing 不使用 MVC,它使用修改后的模式。MVC 经常被错误引用和误解——尤其是在 Swing 的上下文中。此外,MVC 适用于 UI 组件,而不适用于应用程序。因此,我建议寻找反映和使用 Swing 架构而不是 MVC 的概念、解决方案和库。

MVC 模式的发明者社区在 1993 年左右引入了 ApplicationModel(现在称为 Presentation Model);MVP 紧随其后。在我看来,这两种模式对于 Swing 比 MVC 有用得多。最近,将 Presentation Model 模式带给更多观众的环境转移到了一种新架构:“Pollock”。有兴趣的读者可以谷歌一下,看看它与经常与 Presentation Models 结合的适配器链有何不同。

最后但并非最不重要的个人陈述。与我以前使用的其他工具包或框架相比,我可以更好地使用 Swing;我可以用更少的代码做更多的事情,代码结构更好,更容易维护,而且我很快就能得到结果。

希望这可以帮助。最好的问候, Karsten Lentzsch

参考资料:Fowler 的进一步模式 - http://martinfowler.com/eaaDev数据绑定演示 - http://www.jgoodies.com/articles/Smalltalk 应用程序开发流程 - http://www.cincom.com/downloads/pdf/ AppDevGuide.pdfMVP 模式文档 - http://www.object-arts.com/EducationCentre/Patterns/MVP.htmSun 的 JDNC 项目主页 - http://jdnc.dev.java.net/Oracle 的 ADF FAQ - http:// /www.oracle.com/technology/products/jdev/htdocs/905/adffaq_otn.htmlSpring RCP 项目主页 - http://www.springframework.org/spring-rcp.htmlNetBeans 平台主页 - http://www. netbeans.org/products/platform/JGoodies Swing Suite -http://www.jgoodies.com/products/index.html

If you don't know Karsten and JGoodies, then believe me, this guy really knows what he's talking about. I warmly recommend to read carefully his answer andthe material he posted as reference. More specifically, be sure to check his Desktop Patterns and Data Bindingpresentation. Surprisingly, I prefer the old versionto the new one.

如果你不知道 Karsten 和 JGoodies,那么相信我,这家伙真的知道他在说什么。我热烈建议您仔细阅读他的回答他发布的材料作为参考。更具体地说,一定要检查他的桌面模式和数据绑定演示。令人惊讶的是,我更喜欢旧版本而不是版本

Let me quote his slide about MVC vs MVP:

让我引用他关于 MVC 与 MVP 的幻灯片:

  • Yes, Swing uses an extendedform of MVC internally
  • But MVC is for components, MVP is for applications
  • 是的,Swing在内部使用了MVC的扩展形式
  • 但是MVC是针对组件的,MVP是针对应用的

And then from the Summary:

然后从摘要:

  • Swing 使演示模型变得简单
  • PM 需要绑定解决方案

But I can't do a better job than Karsten at explaining this, I can at best paraphrase him. So just read him!

但是在解释这一点方面,我不能比 Karsten 做得更好,我充其量只能解释他。所以就读他吧!

Also maybe have a look at Desktop Java demos for learning(more precisely Scott Violet's answer).

也可以看看桌面 Java 演示以进行学习(更准确地说是 Scott Violet 的回答)。

回答by simbo1905

Here is an article which compares three GUI patterns in Java used to implement the same screen three times:

这里有一篇文章比较了 Java 中用于实现相同屏幕三次的三种 GUI 模式:

Implementing event-driven GUI patterns using the ZK Java AJAX framework, Simon Massey, Sachin K Mahajan

使用 ZK Java AJAX 框架实现事件驱动的 GUI 模式,Simon Massey,Sachin K Mahajan

Since different frameworks and languages have slight variations on what people feel is MVP, MVC, MVVM and MVVMP etc etc the article is careful to refer to the patterns by the long names given by Martin Fowler not the short hand M__ monikers which are easy to argue about.

由于不同的框架和语言在人们对 MVP、MVC、MVVM 和 MVVMP 等的看法上略有不同,本文谨慎地通过 Martin Fowler 给出的长名称而不是容易争论的简写 M__ 绰号来引用模式关于。

Clearly ZK is a web or mobile desktop-like programming environment but the patterns are equally applicable to Swing or another fat client framework. However without a good databinding framework for a certain desktop environment it is not very feasible to attempt MVVM. Hence in this early presentation the "Presentation Model" pattern is given the moniker "MVB" for Model-View-Binder as an alternative to calling it "MVVM":

显然,ZK 是一个类似 Web 或移动桌面的编程环境,但这些模式同样适用于 Swing 或其他胖客户端框架。但是,如果没有针对某个桌面环境的良好数据绑定框架,则尝试 MVVM 不太可行。因此,在这个早期的演示中,“演示模型”模式被赋予了 Model-View-Binder 的绰号“MVB”,作为将其称为“MVVM”的替代方案:

Design Patterns in ZK: Java MVVM as Model-View-Binder, Simon Massey

ZK 中的设计模式:Java MVVM 作为模型-视图-绑定器,Simon Massey

回答by Tom Yeh

You could take a look at this article: Hello ZK MVVM. ZK supports both MVP and MVVM, and this article might provide some insights for your choice -- the programming model is similar to Swing, thought it is a Web framework.

你可以看看这篇文章:Hello ZK MVVM。ZK同时支持MVP和MVVM,本文可能会为你的选择提供一些见解——编程模型类似于Swing,认为是一个Web框架。

回答by Carlos Tasada

I would also recommend you to use Google to find more information since your question is really vague. Anyway, since you want to do your desktop application in Java, and you'll probably use Swing, I just want to point that Swing is designed with a MVC pattern in mind. Anyway you can easily modify it to a MVP using some extra interfaces.

我还建议您使用 Google 查找更多信息,因为您的问题非常含糊。无论如何,既然您想用 Java 来开发桌面应用程序,并且您可能会使用 Swing,我只想指出 Swing 的设计考虑到了 MVC 模式。无论如何,您可以使用一些额外的接口轻松地将其修改为 MVP。

If you're unsure about the pattern, I would recommend to use the MVC. You'll find much more examples.

如果您不确定该模式,我建议您使用 MVC。你会发现更多的例子。

My 2 cents.

我的 2 美分。