什么进入“控制器”在“MVC”? [英] What goes into the "Controller" in "MVC"?

查看:106
本文介绍了什么进入“控制器”在“MVC”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我理解MVC的基本概念 - 模型包含应用程序的数据和行为,View负责将其显示给用户,Controller负责处理用户输入。我不确定的是 在控制器中的内容。

I think I understand the basic concepts of MVC - the Model contains the data and behaviour of the application, the View is responsible for displaying it to the user and the Controller deals with user input. What I'm uncertain about is exactly what goes in the Controller.

让我们说例如我有一个相当简单的应用程序(我是特别是思考Java,但我认为相同的原则适用于其他地方)。我将我的代码组织成3个包,名为 app.model app.view app。 controller

Lets say for example I have a fairly simple application (I'm specifically thinking Java, but I suppose the same principles apply elsewhere). I organise my code into 3 packages called app.model, app.view and app.controller.

app.model 包中,我有几个类反映应用程序的实际行为。这些扩展了Observable 并使用 setChanged() notifyObservers()在适当的时候触发视图更新。

Within the app.model package, I have a few classes that reflect the actual behaviour of the application. These extends Observable and use setChanged() and notifyObservers() to trigger the views to update when appropriate.

app.view 包有一个类(或几个类)对于不同类型的显示器),使用 javax.swing 组件来处理显示。其中一些组件需要反馈到模型中。如果我理解正确,View不应该与反馈有任何关系 - 应该由Controller处理。

The app.view package has a class (or several classes for different types of display) that uses javax.swing components to handle the display. Some of these components need to feed back into the Model. If I understand correctly, the View shouldn't have anything to do with the feedback - that should be dealt with by the Controller.

那么我实际上是什么控制器?我只是调用Controller中的方法,在View中放置 public void actionPerformed(ActionEvent e)吗?如果是,是否应在控制器中进行任何验证?如果是这样,我如何将错误消息反馈回View - 如果再次通过Model,或者Controller是否应该直接将其发送回View?

So what do I actually put in the Controller? Do I put the public void actionPerformed(ActionEvent e) in the View with just a call to a method in the Controller? If so, should any validation etc be done in the Controller? If so, how do I feedback error messages back to the View - should that go through the Model again, or should the Controller just send it straight back to View?

如果验证是在View中完成的,我在Controller中放了什么?

If the validation is done in the View, what do I put in the Controller?

对于这个长期问题我很抱歉,我只想记录我对这个过程的理解,希望有人可以为我澄清这个问题!

Sorry for the long question, I just wanted to document my understanding of the process and hopefully someone can clarify this issue for me!

推荐答案

在你建议的例子中,你是对的:用户点击'删除这个界面中的项目按钮基本上只需调用控制器的删除功能。但是,控制器不知道视图是什么样的,因此您的视图必须收集一些信息,例如点击了哪个项目?

In the example you suggested, you're right: "user clicked the 'delete this item' button" in the interface should basically just call the controller's "delete" function. The controller, however, has no idea what the view looks like, and so your view must collect some information such as, "which item was clicked?"

在对话中表格:

查看:嘿,控制器,用户刚刚告诉我他要删除第4项。

控制器:嗯,检查了他的凭据后,他被允许这样做......嘿,模型,我希望你得到第4项并做任何你要做的事情来删除它。 />
模型:第4项......得到它。它被删除了。回到你身边,控制器。

控制器 :在这里,我将收集新的数据集。回到你身边,查看。

查看:很酷,我会将新的设置显示给用户现在。

View: "Hey, controller, the user just told me he wants item 4 deleted."
Controller: "Hmm, having checked his credentials, he is allowed to do that... Hey, model, I want you to get item 4 and do whatever you do to delete it."
Model: "Item 4... got it. It's deleted. Back to you, Controller."
Controller: "Here, I'll collect the new set of data. Back to you, view."
View: "Cool, I'll show the new set to the user now."

在该部分的末尾,您有一个选项:视图可以发出单独的请求,给我最新的数据集,因此更纯粹,或者控制器通过删除o隐式返回新数据集peration。

In the end of that section, you have an option: either the view can make a separate request, "give me the most recent data set", and thus be more pure, or the controller implicitly returns the new data set with the "delete" operation.

这篇关于什么进入“控制器”在“MVC”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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