简体   繁体   English

用什么? MVC,MVP或MVVM还是......?

[英]What to use? MVC, MVP or MVVM or…?

I will start a Java project to develop a desktop application. 我将启动一个Java项目来开发桌面应用程序。 What to use as presentation layer pattern (MVC, MVP,MVVM or....)? 什么用作表示层模式(MVC,MVP,MVVM或....)?

...if it is possible, with some working small example... :-) ......如果有可能,用一些工作小例子...... :-)

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. 其实,你在寻找最终的帖子是这样回答 这个答案从卡斯滕Lentzsch共同领导(的JGoodies数据名望)在秋千框架和最佳实践 摇摆框架和最佳实践主题。

Hello, 你好,

I've been writing Swing apps for several years that many people find elegant. 我已经写了几年Swing应用程序很多人都觉得优雅。 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中查找,设计,布局和实现精心设计的屏幕。

I'd say there are no "best practices" for Swing available online. 我说在网上没有Swing的“最佳实践”。 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. 我怀疑任何人都可以提供“最佳”实践,因为这需要Swing开发人员和解决方案提供商之间就方法进行讨论,技术上有效,易于理解和使用。 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书中找到这些信息。

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. 在我看来,缺乏良好实践和信息如何构建Swing应用程序是开发人员在使用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; 我见过很多Swing应用程序很糟糕; 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? 通常开发人员不知道在哪里放置什么代码?,如何分离关注点?如何将事物联系在一起?,如何使用Actions?,如何启动应用程序?以及如何存储和恢复UI状态? 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. 但我发现普通的开发人员可以很好地使用Swing,如果只是手动 - 大约需要3到10天。 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. 几乎所有问题都可以通过代码,库,应用程序架构,模式,通用编程实践,精心设计的示例,教程等来解决或解决。我教授围绕3层架构构建的Swing开发流程分离域,工具和表示层,并基于高效的布局系统。 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工作只占项目工作的一小部分。

So what can you do? 所以,你可以做什么? I strongly recommend to study Martin Fowler's draft for further "Patterns of Enterprise Application Architecture". 我强烈建议研究Martin Fowler的草案,了解更多“企业应用程序架构模式”。 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. 我发现这些模式在Swing和Swing团队中运行得非常好 - 即使是对Swing和Swing架构不熟悉的开发人员也是如此。 I'd pick the following patterns first: "Presentation Model", "Separated Domain", and "Separated Presentation". 我首先选择以下模式:“Presentation Model”,“Separated Domain”和“Separated Presentation”。 I personally favor the Presentation Model (Application Model for Smalltalkers), over the Model-View-Presenter pattern (MVP). 我个人赞成使用Model-View-Presenter模式(MVP)的Presentation Model(Smalltalkers的应用程序模型)。 However, MVP is a true and well studied alternative. 然而,MVP是一个真正的,研究得很好的选择。

Fowler's patterns can be combined with a 3-client-tier architecture that scales well for moderately large Swing applications. Fowler的模式可以与3客户端层结构相结合,可以适用于中等规模的Swing应用程序。 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. 基于MVP的应用程序也可以在这3个客户端层中构建。

A key task for Swing applications is the data binding: how to connect domain objects and domain object properties to the Swing components. Swing应用程序的一个关键任务是数据绑定:如何将域对象和域对象属性连接到Swing组件。 Basically you can copy your data back and forth, or build chains of adapters from your domain objects to the UI components. 基本上,您可以来回复制数据,或者从域对象到UI组件构建适配器链。 The copying approach is easy to understand and often the first choice for those who are new to Swing or data binding; 复制方法易于理解,并且通常是那些刚接触Swing或数据绑定的人的首选; 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. 适配器链和自动或半自动更新可以显着减少将域数据绑定到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; 正如您所指出的,Swing没有为可重用且灵活的模型提供很好的抽象,可用于绑定文本字段; the Document interface isn't appropriate for generic data access. Document接口不适合通用数据访问。 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. 有一些库可用,提供ValueModel接口,旨在为单值数据添加通用,强大且灵活的模型:字符串,布尔值,数字,日期等。

I'm not aware of a Swing book that explains a true Swing application development process. 我不知道Swing书解释了真正的Swing应用程序开发过程。 Ideally such a book would combine the patterns, architecture, and data binding techniques mentioned above and would describe how to implement it in Swing. 理想情况下,这样一本书将结合上面提到的模式,体系结构和数据绑定技术,并将描述如何在Swing中实现它。 Anyway, there's a 10-years old documentation for a Smalltalk application development process that does just that. 无论如何,有一个10年前的Smalltalk应用程序开发过程的文档就是这样做的。 Oracle's JClient architecture and documentation is not that complete but may be easier to read for Java developers. Oracle的JClient体系结构和文档并不完整,但对于Java开发人员来说可能更容易阅读。 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. 我提供了有关Fowler模式的数据绑定的演示文稿,这些模式的3层体系结构和Swing实现以及自动数据绑定。 The tutorial sources of my Binding library may help you get aquainted with adapter chains and the ValueModel interface. 我的Binding库的教程资源可以帮助您获得适配器链和ValueModel接口。 The best documentation for the MVP pattern can be found in the Dolphin Smalltalk docs. 可以在Dolphin Smalltalk文档中找到MVP模式的最佳文档。

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任务:1)通过选择适合您的目标平台集的专业外观来改善外观,2)选择可以帮助您的布局系统快速构建设计良好且一致的屏幕,3)选择数据验证解决方案,4)为日常Swing任务获取一袋解决方案。

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. 有几个项目概述了Swing架构,解决了数据绑定和典型的Swing任务,例如:Sun的JDNC,Oracle的JClient / ADF,Spring RCP,NetBeans平台。 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. 我提供了一个基于开源JGoodies库的商业Swing解决方案套件,并为所有公共JGoodies工具和演示添加了一包解决方案和资源。 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. 让我添加关于所谓的“MVC”框架的标准警告。 Swing doesn't use MVC, it uses a modified pattern. Swing不使用MVC,它使用修改后的模式。 MVC is frequently misquoted and misunderstood - especially in the context of Swing. MVC经常被错误引用和误解 - 特别是在Swing的背景下。 Also, MVC is good for UI components, not for applications. 此外,MVC适用于UI组件,而不适用于应用程序。 Hence I recommend to look for concepts, solutions, and libraries that reflect and work with the Swing architecture, not MVC. 因此,我建议寻找反映和使用Swing架构的概念,解决方案和库,而不是MVC。

The inventor community of the MVC pattern introduced the ApplicationModel (now known as Presentation Model) around 1993; MVC模式的发明者社区在1993年左右引入了ApplicationModel(现在称为Presentation Model); MVP followed a bit later. MVP跟进了一会儿。 In my opinion these two patterns are much more useful for Swing than MVC. 在我看来,这两种模式对于Swing比MVC更有用。 Recently the environment that brought the Presentation Model pattern to a larger audience moved on to a new architecture: "Pollock". 最近,将Presentation Model模式带给更多受众的环境转移到了新的架构:“Pollock”。 Interested readers may google to see how that differs from the adapter chains that are often combined with Presentation Models. 有兴趣的读者可以谷歌看看它与经常与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; 我可以使用Swing比使用之前使用的其他工具包或框架更好地工作; 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 最诚挚的问候,Karsten Lentzsch

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 参考文献:Fowler的进一步模式 - http://martinfowler.com/eaaDev数据绑定演示 - http://www.jgoodies.com/articles/ Smalltalk app dev进程 - http://www.cincom.com/downloads/pdf/ AppDevGuide.pdf MVP模式文档 - 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.html Spring 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

If you don't know Karsten and JGoodies, then believe me, this guy really knows what he's talking about. 如果你不认识Karsten和JGoodies,那么请相信我,这家伙真的知道他在说什么。 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 . 令人惊讶的是,我更喜欢旧版本 版本

Let me quote his slide about MVC vs MVP: 让我引用他关于MVC与MVP的幻灯片:

  • Yes, Swing uses an extended form of MVC internally 是的, Swing在内部使用扩展形式的MVC
  • But MVC is for components, MVP is for applications MVC用于组件,MVP用于应用程序

And then from the Summary: 然后从摘要中:

But I can't do a better job than Karsten at explaining this, I can at best paraphrase him. 但是,我不能比卡斯滕更好地解释这一点,我最多可以解释他。 So just read him! 所以只要读他!

Also maybe have a look at Desktop Java demos for learning (more precisely Scott Violet's answer ). 也许可以看看用于学习的Desktop Java演示 (更确切地说是Scott Violet的答案 )。

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__ monikers关于。

Clearly ZK is a web or mobile desktop-like programming environment but the patterns are equally applicable to Swing or another fat client framework. 显然,ZK是一个类似Web或移动桌面的编程环境,但这些模式同样适用于Swing或其他胖客户端框架。 However without a good databinding framework for a certain desktop environment it is not very feasible to attempt MVVM. 但是,如果某个桌面环境没有良好的数据绑定框架,那么尝试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": 因此,在这个早期的演示中,“Presentation Model”模式被赋予了Model-View-Binder的名字“MVB”,作为将其称为“MVVM”的替代方案:

Design Patterns in ZK: Java MVVM as Model-View-Binder, Simon Massey ZK中的设计模式:Java MVVM作为Model-View-Binder,Simon Massey

You could take a look at this article: Hello ZK MVVM . 你可以看一下这篇文章: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. ZK支持MVP和MVVM,本文可能会为您的选择提供一些见解 - 编程模型类似于Swing,认为它是一个Web框架。

I would also recommend you to use Google to find more information since your question is really vague. 我还建议您使用Google查找更多信息,因为您的问题非常模糊。 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. 无论如何,既然你想用Java做你的桌面应用程序,并且你可能会使用Swing,我只想指出Swing的设计考虑了MVC模式。 Anyway you can easily modify it to a MVP using some extra interfaces. 无论如何,您可以使用一些额外的接口轻松地将其修改为MVP。

If you're unsure about the pattern, I would recommend to use the MVC. 如果您不确定该模式,我建议使用MVC。 You'll find much more examples. 你会发现更多的例子。

My 2 cents. 我的2美分。

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

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