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

查看:188
本文介绍了用什么? 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... :-)

推荐答案

实际上,你要找的最终帖子是 此答案 这个答案 来自Karsten Lentzsch(of JGoodies成名的 Swing Frameworks and Best Practices 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.


您好,

Hello,

我一直在写摇摆应用程序已有好几年了ind
优雅。我教导开发人员有效地使用Swing:如何
来构建和组装应用程序,如何绑定和验证日期,
以及如何查找,设计,布局和实现精心设计的$ b Swing中的$ b屏幕。

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.

在我看来,缺乏良好实践和信息如何建立
a Swing应用程序是开发人员以
Swing开头时面临的最大障碍。我与之合作的大多数开发人员在寻找设计,实现布局,构建面板,绑定数据,处理事件,
以及安排不同的代码部分和代码层方面都很慢。我见过很多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开发
流程围绕3层架构构建,该架构将
域,工具和表示层分开,并基于高效的
布局系统。尽管大多数部件都是以编程方式完成的,但是b $ b开发人员可以快速获得结果,代码和视觉设计
变得非常一致。这可以将开发人员的工作效率提高到
很多 - 在某种程度上,Swing工作需要的项目工作的b $ b小得多。

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.

所以你能做什么?我强烈建议研究Martin Fowler的
草案,以获得更多企业应用程序架构模式。
我发现这些模式在Swing和Swing
团队中运行得非常好 - 即使对于Swing和Swing
体系结构的开发人员也是如此。我首先选择以下模式:Presentation
Model,Separated Domain和Separated Presentation。我个人
赞成演示模型(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-client-tier架构相结合
可以很好地扩展适用于中等大小的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或数据绑定的
的首选;我会说这是一个不错的选择。
另一方面,复制使得同步视图变得更加困难。
适配器链和自动或半自动更新可以
显着减少将域数据
绑定到UI所需的代码量。缺点是,这种方法更难以理解。正如您所指出的,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中实现它。无论如何,对于Smalltalk应用程序开发过程,有一个10年前的
文档,
就是这样做的。 Oracle的JClient架构和文档不是完整的
,但对于Java开发人员来说可能更容易阅读。我提供了
关于数据绑定的演示文稿,该文件涉及Fowler模式,
3层体系结构和这些模式的Swing实现以及
自动数据绑定。我的Binding库
的教程源代码可以帮助您获得适配器链和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)选择一个可以帮助你$的布局系统b $ b快速构建设计良好且一致的屏幕,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平台。 I
提供了基于
开源JGoodies库的商业Swing解决方案套件,并为所有公共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框架的标准警告。 Swing
不使用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模式带给更多受众的环境转移到了新的
架构:Pollock。有兴趣的读者可以谷歌看看
与经常与
演示模型组合的适配器链有何不同。

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.pdf MVP模式
文档 -
http://www.object-arts.com/EducationCentre/Patterns/MVP.htm Sun的JDNC
项目主页 - http://jdnc.dev.java.net/ Oracle的ADF常见问题解答 -
< a href =http://www.oracle.com/technology/products/jdev/htdocs/905/adffaq_otn.html =nofollow noreferrer> 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

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,那么请相信我,这家伙真的知道他在说什么。我热烈建议仔细阅读他的回答他发布的材料作为参考。更具体地说,一定要检查他的桌面模式和数据绑定演示文稿。令人惊讶的是,我更喜欢旧版本到< a href =http://www.jgoodies.com/download/presentations/patterns-and-binding.pdf =nofollow noreferrer> new one 。

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适用于应用程序

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

然后从摘要中:

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

但是我解释这个问题比Karsten做得更好,我最多可以解释他。所以请读他!

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

也许可以看看用于学习的桌面Java演示(更准确地说是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天全站免登陆