实现MVC视图和控制器通信。 (Java) [英] Implementation of MVC View and Controller communication. (Java)

查看:192
本文介绍了实现MVC视图和控制器通信。 (Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的课上,我们一直在学习不同的设计,如MVC和MVP。我目前正在写一个应用程序,显示数据在JTable和自定义图。我的问题是我应该如何进行视图和控制器之间的通信。

In my class we have been learning about different designs such as MVC and MVP. I am currently writing an application that displays data in a JTable and a custom plot. My question is how should I go about communicating between the View and Controller.

例如,我有一个按钮应该从一个文件导入数据到模型。我想我想要的是通知控制器用户想要导入文件的视图。控制器然后执行必要的逻辑来这样做。视图应该如何做呢?我看到几个选项。
1)让控制器创建一个内部类,当用户点击导入按钮时调用。在这种情况下,控制器将不得不调用视图的方法来查看用户想要导入什么文件。
2)让视图检测事件,然后在控制器中调用一个合适的方法传递文件的名称。

As an example, I have a button that should import data from a file into the Model. What I think I want is the View to notify the controller that the user wants to import a file. The controller then does the necessary logic to do so. How should the view do so? I see a couple of options. 1) Have the controller create an innerclass that gets called whenever a user hits the import button. In this case the controller would have to call a method of the view to see what file the user wanted to import. 2) Have the view detect the event and then call an appropriate method in the controller passing with it the name of the file.

这会提出更大的问题的视图是否知道控制器?我知道这些东西没有正确的答案,但最好的方法是什么?

This begs the bigger question of whether or not the view knows about the controller? I know there isn't a correct answer to these things but what would be the best way?

推荐答案

在我作为架构师或程序员参与的项目中,我从来没有将业务逻辑放在一个控制器。因为我从来没有看到任何技术,其中直接与视图通信的层可以移植。

In the projects I participate as a architect or programmer, I never put business logic in a controller. Because I never saw any technology in which the layer that communicates directly to the view can be ported.

控制器层应该充当视图的服务层。所以是的。视图必须知道控制器。

The controller layer should act as a service layer to the view. So yes. The view must know about the controller. And, if the previous statement is true, there's no problem the controller could communicate with the view.

我设计我的业务逻辑(我的@EJB或spring的@Service)在完全POJO基层。这是我的可移植业务层。

I design my business logics (my @EJB or spring's @Service) in a completely POJO-based layer. That's my portable business layer.

控制器只是视图和业务规则层之间的桥梁。它调用业务方法,正确地格式化它们的响应(有时)并发送回视图。在此上下文中,控制器可以是Web服务,受管Bean,测试套件等...

The controller is just a bridge between the view and the business rules layer. It calls business methods, format their responses properly (sometimes) and send back to the view. In this context, the controller can be a web service, a managed bean, a test suite, etc...

这篇关于实现MVC视图和控制器通信。 (Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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