MVC 架构中的用户输入来自哪里?

发布于 2024-07-29 00:46:23 字数 241 浏览 14 评论 0原文

我想知道控制器从哪里获取用户输入(以供模型使用)。 因为输入媒体与用户密切相关,视图不应该知道获取用户数据的具体方式吗? 但是我怎样才能将控制器与视图分开呢? 是否有可能使两者完全独立,正如它们的目的所暗示的那样?

示例: 当我有一个使用curses库作为视图的应用程序时,这意味着它只能通过终端访问。 使用curses方法读取控制器中的用户数据会破坏封装,但在视图上调用方法与显示模型无关。

I'd like to know where the controller gets the user input from (to feed the model with). Because input media is strongly related to the user shouldn't the view be aware of the concrete way to get the user's data? But how can I separate the controller from the view then? Is it possible to make both completely independent of each other as their purposes suggest?

Example:
When I have an application which uses the curses library for the view it implies that the it's only accessible through the terminal. Using curses methods to read user data in the controller would break encapsulation but calling methods on the view would have nothing to do with displaying the model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

唐婉 2024-08-05 00:46:23

在 MVC 中,控制器从视图获取用户输入。

IN MVC, the controller gets its user input from the View.

携君以终年 2024-08-05 00:46:23

考虑让视图和控制器通过观察者模式进行通信。 控制器将自己注册为视图的观察者。 当用户将数据输入到视图中并按 Enter 键时,视图将解释数据并通知其观察者有可用数据。 然后Controller可以通过公共方法从View获取数据。

Consider having the View and Controller communicate through the Observer pattern. The Controller registers itself as an Observer with the View. When the user inputs data into the View and presses Enter, then the View interprets the data and notifies its observers that there is data available. The Controller can then get the data from the View through a public method.

梦太阳 2024-08-05 00:46:23

我认为视图实际上与输入数据没有太大关系。 我发现如果您看到用户直接与控制器通信,MVC 更容易可视化。 控制器从用户接收数据并发送回视图。 在许多系统中,视图引擎具有一些有限的自我更新方式(即文本输入在发送到控制器之前显示键入的内容)。 但对于任何 MVC 类型的架构,您可以将任何视图替换为任何其他视图,前提是它们都能够处理相同的数据。

例如。 输入用户名可以在任何支持输入字符串的系统上完成。 该控制器接受字符串,因此可以在 Web 应用程序、终端应用程序或 GUI 应用程序中使用。

I dont think that the view really has much to do with inputting data actually. I find MVC much easier to visualise if you see the user as communicating with the controller directly. A controller receives data from the user and sends views back. In many systems the view engine has some limited way of updating itself (ie text inputs display what is typed before it is sent to the controller). But for any MVC type architecture you can replace any view with any other view provided they are both capable of handling the same data.

For example. Entering a username can be done on any system that supports entering strings. The controller accepts a string, and so can be used in a web application, a terminal application, or a GUI application.

辞别 2024-08-05 00:46:23

我认为视图应该在控制器上有一个回调来发送用户输入。 在 Web 架构中,回调是通过通过 http 请求将用户输入发送回服务器的能力来提供的。

在您的情况下,您的 ncurse 前端可能应该有某种控制器组件的回调方法来发回用户输入。

I think the view should have a callback on the controller to send over user input. In web architecture, the callback is provided through the ability to send the user input back to the server through http requests.

In your case, your ncurse front should probably have some kind of callback method to the controller component to send back user input.

寄风 2024-08-05 00:46:23

好吧,

我会尽力为您提供更具体的信息。 为你能看到的人提供模糊/抽象的答案,不能掌握主题,也没有帮助。

MVC-> 模型视图控制器

MVC 有很多实现,我不知道你的情况,但我给你一个。

最常见的 MVC 实现是这样的..

view <-> 控制器<-> 模型

在 Web 场景中......

视图将是您的 HTML 页面,数据输入将发生在表单中。

<form action=/home/createuser method=post>
...code goes here...
</form>

Home 将是您的控制器(一个名为 home 的类),并且 createuser 是 home 中的一个方法。

public class Home extends Controller {

   public void createUser(Userform f){
      ...create user...
   }
}

该表单会将数据作为参数提交到方法中。 Createuser 将处理它们以与模型对话,然后保存数据(如果是这种情况)。

Well,

I'll try to be more specific for you. Giving vague/abstract answers for ppl that you can see, doesn't master the subject, doesnt help.

MVC -> Model View Controler

There are many implementation of MVC, I don't know your case, but I'll give you one.

The most common MVC implementation acts like this..

view <-> Controler <-> Model

In a web scenario..

The view would be your HTML pages and data input would happen in a form.

<form action=/home/createuser method=post>
...code goes here...
</form>

Home would be your controller (a class named home), and createuser a method in home.

public class Home extends Controller {

   public void createUser(Userform f){
      ...create user...
   }
}

This form would submit data into the method as parameters. Createuser would them processed to talk to the model and later persist the data if thats the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文