要使用什么?(What to use? MVC, MVP or MVVM or…?)

编程入门 行业动态 更新时间:2024-10-28 18:33:18
要使用什么?(What to use? MVC, MVP or MVVM or…?)

我将开始一个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... :-)

最满意答案

实际上,您正在寻找的最终帖子是这个回答,来自Karsten Lentzsch(JGoodies的名气)在Swing框架和最佳实践 Swing框架和最佳实践主题中的回答

你好,

我已经写了几年Swing应用程序,许多人觉得优雅。 我教导开发人员与Swing高效合作:如何构建和组装应用程序,如何绑定和验证日期,以及如何在Swing中查找,设计,布局和实现精心设计的屏幕。

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

在我看来,缺乏关于如何构建Swing应用程序的良好实践和信息是开发人员在开始使用Swing时遇到的最大障碍。 与我合作的大多数开发人员在寻找设计,实现布局,构建面板,绑定数据,处理事件以及安排不同的代码部分和代码层方面都很慢。 我见过的很多Swing应用都很糟糕; 除了他们糟糕的视觉设计外,他们很难理解,甚至很小的变化也会花费很多。 通常开发人员不知道在哪里放置什么代码,如何区分顾虑,如何将事情联系在一起,如何使用Actions?,如何启动应用程序?以及如何存储和恢复UI状态? 大多数开发者缺乏遵循的准则。

但我发现一般的开发人员可以和Swing一起工作,如果只是在3到10天内完成的话。 几乎所有的问题都可以通过代码,库,应用程序体系结构,模式,通用编程实践,设计良好的示例,教程等来回答或解决。我教了一个Swing开发过程,该过程围绕三层体系结构构建,分离域,工具和表示层,并基于高效的布局系统。 尽管大部分代码都是以编程方式完成的,开发人员可以快速获得结果,代码和可视化设计也会变得非常一致 这可以大大提高开发人员的生产力 - Swing工作占用了项目工作量的很小一部分。

所以,你可以做什么? 我强烈建议研究Martin Fowler的进一步“企业应用架构模式”草案。 我发现这些模式对Swing和Swing团队非常有用,即使对于Swing和Swing架构的新开发者也是如此。 我首先选择以下模式:“演示模型”,“分离域”和“分离演示文稿”。 我个人喜欢Presentation Model(Smalltalkers的应用程序模型),而不是Model-View-Presenter模式(MVP)。 但是,MVP是一个真正的和充分研究的选择。

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

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

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

一旦你选择了你的架构和桌面模式集,你应该解决以下更基本的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更有用。 最近,将演示模型模式引入更多观众的环境转向了新的架构:“波洛克”。 有兴趣的读者可以通过谷歌了解与经常与演示模型组合的适配器链有何不同。

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

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

参考文献:福勒的进一步模式 - http://martinfowler.com/eaaDev数据绑定演示文稿 - http://www.jgoodies.com/articles/ Smalltalk应用开发流程 - 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

如果你不认识Karsten和JGoodies,那么相信我,这个人真的知道他在说什么。 我热烈推荐仔细阅读他的回答以及他作为参考发布的材料。 更具体地说,一定要检查他的Desktop Patterns和Data Binding演示文稿。 令人惊讶的是,我更喜欢旧版本到新 版本 。

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

是的, Swing在内部使用扩展的MVC形式MVC适用于组件,MVP适用于应用程序

然后从总结:

将该域与演示文稿分开! 这是分开的演示文稿 。 独立的自治视图(如果适用) 选择MVP演示模型

Swing使演示模型变得简单 PM需要有约束力的解决方案

但我不能做比卡尔斯顿更好的解释,我最多可以解释他。 所以只要读他!

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

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.

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/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

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.

Let me quote his slide about MVC vs MVP:

Yes, Swing uses an extended form of MVC internally But MVC is for components, MVP is for applications

And then from the Summary:

Separate the domain from the presentation! That is Separated Presentation. Separate Autonomous View if appropriate Choose MVP or Presentation Model

Swing makes Presentation Model easy PM requires a binding solution

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).

更多推荐

本文发布于:2023-08-07 09:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:要使   MVC   MVVM   MVP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!