不同的方式在Java中实现Model-View-Controller(MVC)

huangapple go评论84阅读模式
英文:

different styles of implementing Model-View-Controller in Java

问题

//update 1: 很遗憾的是,对于这个主题的活动并不如我所希望的那样多,更多的内容在下面部分。

首先,我知道这个主题经常被过多地涌入个人观点,因此经常因为与主题无关而被关闭。因此,我将尽量保持客观。我希望这可以避免我被“锁定”。

当你查看维基百科时,基本上是这样说的:用户直接与控制器交互,控制器操作模型,模型更新视图。
在Google上搜索时,可以找到许多其他不同类型的解释:

  • 例如,有一种说法是模型总是独立的,不知道其他内容
  • 另一种说法是控制器是一种粘合剂,但模型和视图彼此不知道

对于这三者的任何组合,都有类似的不同解释。所以,我认为:似乎没有一种(正确的)实现MVC的方式,而是有一些不同的方法都遵循相同的基本原则。

因此,我想要尝试保持“客观”的方式:是否有一些特定的理由来选择一种方法而不是另一种方法?在将其中之一用作其他内容的粘合剂方面是否有优势?还是它们都以某种方式彼此了解?

当使用Swing时,已经在某种程度上使用了MVC的形式 - 有些资源将其称为“模型-(ui)委托”,其中View和Controller结合在一个模型支持下。对于已经在使用Swing的人来说,是否值得尝试复制这种风格?

也许可以稍微阐述一下:
我目前想要为Mixer开发一个API客户端(是的,我有关于YouTube和Twitch的主题,但目前Mixer对我和我的需求来说是最好的平台),并且在如何分离数据(模型),GUI(视图)以及它们之间的所有粘合剂(控制器?)方面遇到了一些困扰。过去当我编写一些小工具时,我没有找到比所谓的“意大利面代码”更好的方法。但是,因为我想要可能将其发布出去(我已经开始使用一个公共GitHub存储库),或者将其提供为一项服务(以便他人在我的项目上使用我的客户端),所以我想按照一些已经确立的原则来构建它。

因此,与其寻求个人观点(因为这似乎在这里并不受欢迎),我更愿意就实际在Java中实现MVC的特定方法以及它们的优缺点寻求一些客观信息。

提前感谢,并对任何可能认为“又来了一个这样的话题”的版主表示歉意。

//edit 1: 因此,尽管在过去的几周里,这个主题的活动并不多,但我并没有闲着,而是进行了一些更多的“研究”:
从我找到的资料中,我得出了这个结论:无论是处理一些本地GUI应用程序,还是可能像我计划的那样通过网络处理不同类型的视图和事件源,最终都要将一些类连接在一起,根据关注点分离的原则,所有这些类都只执行一个特定的任务。
我找到了一些例子,其中视图仅被视为GUI,另一些甚至将其扩展到了控制台甚至打印机。因此,将视图仅视为输入来源可能不是最好的想法,而是将其视为仅显示数据的东西。另一方面,控制器输入也可以被实现为GUI,甚至可以与显示数据的视图结合,但至少应该是模块化的,以便可以与其他类型进行交换。

我想要尝试实现的方式是事件驱动的,因此“操作”是基于事件何时被触发而发生的(如何生成和触发事件可能是另一个话题)。我看到了许多示例中使用了观察者模式 - 所以基本上是观察者和MVC的混合(至少在我理解的范围内)。这是否是实现的一种可能方式:

  • 一些输入生成输入事件(可以是GUI,也可以是控制台,甚至是网络输入),与输入控制器耦合
  • 模型包含了修改数据的数据和逻辑,可能还有自己的模型控制器
  • 视图作为仅输出,由主应用程序或其自己的视图控制器进行操作,而不是在模型上注册自己作为观察者
  • 整体应用程序控制器作为其他控制器之间的粘合剂,带有一些粘合逻辑

根据这个计划,基本的应用程序启动过程将如下所示:main() 首先创建一个仅包含所有其他内容的空列表的主应用程序控制器,然后是模型(或模型控制器,由它创建模型),并将模型控制器与主控制器链接,最后是视图,并将其与主控制器链接。最后,在所有这些都完成后,调用主控制器上的“入口点”来启动它所有。

对于这个想法,有人有意见

英文:

//update 1: so, unfortunately there's not much activity on this topic than I hoped for - more down the bottom

first of: I know this topic is often overrun and hence suffer from personal opinions and therefore often gets closed as off-topic. So, I'll try to keep it objective. I hope this prevents me from getting "locked off".

When you look at Wikipedia, it says basically this: The user directly interacts with the controller, the controller manipulates the model and the model updates the view.
When searching on Google one finds many different other types:

  • one for example states that the model is always on its own and doesn't know anything about the others
  • another one says that the controller is kind of the glue but model and view doesn't know each other

This goes on for pretty much any combination of these three in addition with some "extras". So, as I see it: There seem not really one (the right) way to implement MVC but rather some different approaches all follow the same base principle.

So, the way I want to try to keep it "objective": Are there some specific reason to go one approach or another? Is there any advantage on using one of the three as glue for the others? Or all knowing each other in some way?

When using Swing one already uses some form of MVC - some source called it "model-(ui)delegate" where View and Controller are combined backed by a model. Is it worth try to reproduce this style as one already working with Swing?

Maybe to light it up a bit:
I currently want to develop an API client for Mixer (yes, I have topics about YouTube and Twitch, but currently Mixer seem the best platform for me and my needs) and struggle a bit on how to approach the separation of the data (Model), the GUI (View) and all the glue in between (Controller?). In the past when I wrote some of my small tools I didn't got any better than what's known as spaghetti code. But as I want to maybe publish it (I already started by using a public GitHub repo) or provide it as a service (so others use my client on my project) I thought to structure it by some established principles.

So, rather than seeking for personal opinions (as this seem to not liked be seen here) I rather ask for some objective information about specific ways of actually implementing the MVC in Java with their pros and cons.

Thanks in advance and apologize to any moderators may now think "not another one of those".

//edit 1: So, although there wasn't much activity on this topic in the past weeks I wasn't doing nothing but kept a bit of more "researching":
From what I found I got this: No matter if one deals with only some local GUI based application but may also what I plan different types of Views and event sources over the network it comes down to interconnecting a few classes which, by the principle of separation of concerns, all are only do one specific task.
I found some examples where the View was only seen as a GUI, others also extended it to console and even printers. So, a seeing a View as the only source of input may not be the best idea but see it as something that displays the data only. The controller input on the other side may could also be implemented as a GUI, and maybe even combined with the View displaying the data, but should be at least modular so it could be interchanged with other types.

The way I want to try to implement it is event driven so that "actions" happen based on when an event is fired (how an event is generated and then fired may is for another topic). I've seen many examples using the Observer pattern for this - so basically a mix of Observer and MVC (at least as far as I understood them). Could this be a possible way of implementing it:

  • some input to generate input events (maybe a GUI, maybe a console, maybe network input) coupled to an input controller
  • a model contain the data and logic to modify them, maybe also having its own model controller
  • the view as output-only manipulated either by a main application or its own view controller instead of register itself as an Observer on the model
  • an overall application controller as the glue between the other controllers with some glue logic

By this plan the basic application startup would look like this: main() first creates the main application controller with just empty lists for all the others, then the model(s) (or model controller which by it creates the model) and link the model controllers with the main controller, and in the end the view and also linking it with the main controller. Finally, after this is all done some "entry point" on the main controller is called to start it all of.

Anyone have some input on this idea?

答案1

得分: 1

以下是您要翻译的内容:

有一个模式在大多数项目中我都会遵循,受到了Spring Boot MVC,SOA以及许多UI框架(如JSF)的启发。

1- 如果有的话,您的视图应始终是哑组件,它们从控制器中获取经过格式化和准备好展示的数据,它们不维护状态。

2- 控制器或视图模型应向视图提供所需的数据,并以适合视图的方式维护其状态,但不执行实际的服务工作,它只是以适合视图的方式格式化数据。控制器可以通过服务从模型中获取此状态或数据。

3- 模型层负责领域对象的状态,这些对象可能来自数据存储,比如数据库,一旦它们以适合服务或应用CRUD操作的方式获取了数据,它们的角色就结束了,模型不知道控制器或视图的任何信息,模型不关心是否没有视图。

4- 一个很好的方法是将核心业务逻辑封装在需要的服务中。服务包含核心业务逻辑,您可以遵循SOA原则来设计良好的服务,以及像SOLID、DRY这样的设计原则,以及一些设计模式。服务应始终保持清晰且设计良好,以便应用程序不会因更改而失败,它们可以完成所有工作并提供适合任何UI的结果,而不仅仅是特定的控制器或UI,因此您应该遵循SOA原则。

那么模型和视图模型/控制器之间的区别是什么?视图模型为视图提供所需的内容... 查看这个答案,获取更清晰的解释

对于API部分,为了确保其干净且可重用,请遵循与您将要使用的内容相关的设计原则,例如,如果您将使用REST,有惯例和最佳实践可确保您的API干净且设计良好。

简而言之,考虑每个层和类的单一职责。首先构建模型和服务,它们应对大部分修改封闭。然后像使用API一样使用它们,构建控制器来使用它们。

您需要很多方法来避免意大利面代码反模式。如果您想要学习更多有关此方面的优质资源,您可以阅读《编写干净的代码》、《清晰架构》等书籍,以及来自《Head First设计模式》书籍和/或refactoring.guru的一些设计模式和重构技术,或者您习惯的任何其他资源。这个主题非常广泛,关于它有很多内容可以谈论。

英文:

There is one pattern that i follow in most of the projects, and is inspired by Spring Boot MVC, SOA, and many frameworks of UI like JSF.

1- Your views if any should always be dumb components they took the data from controllers formatted and ready to be displayed, they don't maintain a state.

2- Controller or view model, should provide the views with the data they need and maintain their state in a way that's suitable for view but don't do the actual work of the service, it just format the data in the way suitable for the views. controllers can get this state or data from model through services.

3- The model layer is responsible for the state of the domain objects that may come from data store like a database, once they got the data in a way suitable for service or to apply CRUD operations on, their role is finished, the model don't know anything about the controller or the view, the model don't care if their is no view at all.

4- One good approach to go with is to encapsulate your core business logic in Services if needed. Services contain the core business logic, and you can follow SOA principles do design good services along with design principles like SOLID, DRY, and some design patterns. Services should always be clean and designed well so the app don't fail by changes, they can do all the work and provide results that suits any UI not just a specific controller or UI that's why you should follow SOA principles.

So what is the difference between model and view model/controller ? the view model gives the view what they need ... see this answer, for more clear explanation

For the API part, to make sure it's clean and reusable follow the design principles related to what you will use, for example if you will use REST, there are conventions and best practices to make sure your API is clean and well-designed.

In a nutshell, Think about the single responsibility of each layer and class. You build your model and services first and they should be closed for most of modifications. Start use them as if you are using them through API so build your controller to use them.

You need a lot of things to avoid spaghetti code anti-pattern. If you want good resources to learn about this, you can read clean code, clean architecture books, and some design patterns and refactoring techniques from the head first design pattern book and/or refactoring.guru or any other source you are comfortable with. This topic is so wide and have a lot of things to say about.

huangapple
  • 本文由 发表于 2020年4月5日 04:41:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61034504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定