用什么?MVC、MVP 或 MVVM 还是……? [英] What to use? MVC, MVP or MVVM or…?

查看:19
本文介绍了用什么?MVC、MVP 或 MVVM 还是……?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

实际上,您正在寻找的最终帖子是 这个答案 这个答案 来自 Swing 框架和最佳实践 Swing 框架和最佳实践 线程.

Actually, the ultimate post you're looking for is this answer this answer from Karsten Lentzsch (of JGoodies fame) in the Swing Frameworks and Best Practices Swing Frameworks and Best Practices thread.

你好

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

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.

我想说没有可用于在线 Swing 的最佳实践".和我怀疑任何人都可以提供最佳"实践,因为那会需要 Swing 开发人员和解决方案提供商之间进行讨论关于方法,什么在技术上有效,什么容易理解,并与之合作.此外,最佳实践将解释谁可以达到什么样的结果质量,他们会描述生产时间和生产成本;但我永远找不到这些信息在线或在 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.

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

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.

但我发现普通开发人员可以很好地使用 Swing,如果仅通过手工 - 在大约 3 到 10 天内.几乎所有你的问题可以通过代码、库、应用程序架构、模式、通用编程实践,精心设计的示例、教程等.我教 Swing 开发围绕三层架构构建的流程,该架构将领域、工具和表示层,并且基于高效的布局系统.尽管大多数部分都是以编程方式完成的开发人员快速获得结果以及代码和视觉设计变得相当一致.这可以提高开发人员的生产力lot - 在某种程度上,Swing 工作需要的时间要小得多项目工作的一部分.

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.

那你能做什么?我强烈建议学习马丁福勒的进一步的企业应用程序架构模式"草案.我发现这些模式非常适合 Swing 和 Swing团队 - 即使对于 Swing 和 Swing 不熟悉的开发人员建筑学.我会首先选择以下模式:演示文稿Model"、Separated Domain"和Separated Presentation".我个人支持 Presentation Model(Smalltalkers 的应用程序模型),模型-视图-演示者模式 (MVP).然而,MVP是一个真正的以及经过充分研究的替代方案.

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 的模式可以与 3 客户端层架构相结合对于中等规模的 Swing 应用程序来说,它可以很好地扩展.它包含域层、表示层和中介模型层.我在我的数据绑定演示中概述了这种架构.基于 MVP 的应用也可以在这 3 个客户端层中构建.

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.

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

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.

我不知道解释真正的 Swing 应用程序的 Swing 书籍发展过程.理想情况下,这样的书将结合这些模式,上面提到的架构和数据绑定技术,并将描述如何在 Swing 中实现它.反正有一个10岁Smalltalk 应用程序开发过程的文档就是这样.Oracle 的 JClient 架构和文档不是这很完整,但对于 Java 开发人员来说可能更容易阅读.我提供关于 Fowler 模式的数据绑定的介绍,这些模式的 3 层架构和 Swing 实现以及自动数据绑定.我的绑定库的教程来源可能会帮助您熟悉适配器链和 ValueModel界面.MVP 模式的最佳文档可以在Dolphin Smalltalk 文档.

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.

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

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.

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

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.

让我添加关于所谓的MVC"框架的标准警告.摇摆不使用 MVC,它使用修改后的模式.MVC 经常出现被错误引用和误解——尤其是在 Swing 的上下文中.此外,MVC 适用于 UI 组件,而不适用于应用程序.因此我建议寻找反映并使用 Swing 架构,而不是 MVC.

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.

MVC 模式的发明者社区介绍了1993 年左右的 ApplicationModel(现在称为 Presentation Model);MVP过了一会儿.在我看来这两种模式要多得多对 Swing 比 MVC 有用.最近带来的环境Presentation Model 模式让更多的观众转向了一个新的架构:波洛克".有兴趣的读者可以谷歌看看如何不同于通常与演示模型.

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.

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

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.

希望这会有所帮助.最好的问候, Karsten Lentzsch

Hope this helps. Best regards, 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.htm Sun 的 JDNC项目主页 - http://jdnc.dev.java.net/ Oracle 的 ADF 常见问题解答 -http://www.oracle.com/technology/products/jdev/htdocs/905/adffaq_otn.htmlSpring RCP 项目首页 -http://www.springframework.org/spring-rcp.html NetBeans 平台主页- http://www.netbeans.org/products/platform/ JGoodies Swing Suite - http://www.jgoodies.com/products/index.html

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

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

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 and the material he posted as reference. More specifically, be sure to check his Desktop Patterns and Data Binding presentation. Surprisingly, I prefer the old version to the new one.

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

Let me quote his slide about MVC vs MVP:

  • 是的,Swing 在内部使用了扩展形式的 MVC
  • 但是MVC 是针对组件的,MVP 是针对应用程序的

然后从摘要:

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

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

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

也可以看看 Desktop Java demos for learning(更准确地说是 Scott Violet 的 答案).

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

这篇关于用什么?MVC、MVP 或 MVVM 还是……?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆