Java MVC 模式中视图与控制器的解耦

发布于 2024-10-10 21:14:51 字数 411 浏览 2 评论 0原文

第一次在 StackOverflow 上发布问题,所以请对我宽容一点:)

据我了解,正确使用模型-视图-控制器模式需要我们将视图和控制器解耦,以便视图对控制器一无所知。我在理解如何使用 Java Swing 执行此操作时遇到了一些问题。

假设我有一个视图(某个扩展 JFrame 的类),并且该视图有一个按钮。可以肯定地说我想将控制器注册为按钮的 ActionListener 吗?或者我让它成为整个视图本身的监听者。

我如何在不执行类似以下操作的情况下执行此操作:

在视图中添加按钮.addActionListener(myController)

,因为如果我在视图代码中执行此操作,它现在不会依赖于控制器吗?

我没有发布任何代码,因为坦率地说,我目前没有太多要做的事情。

任何帮助表示赞赏!

First time posting a question on StackOverflow, so please go easy on me :)

From what I understand, proper use of the model-view-controller pattern requires that we decouple the view and controller such that the view knows nothing about the controller. I'm having a bit of a problem understanding how to do this using Java Swing.

Say I have a view (some class that would extend JFrame), and this view has a button. Is it safe to say that I would want to register the controller as an ActionListener of the button? Or do I make it a listener of the entire view itself.

And how do I go about doing this without doing something like:

button.addActionListener(myController)

in the view, because if I were to do this in the view code, wouldn't it now have a dependency on the controller?

I didn't post any code because, frankly I don't have much to go on at the moment.

any help is appreicated!

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

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

发布评论

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

评论(3

谢绝鈎搭 2024-10-17 21:14:51

不要将视图视为按钮等,而将其视为界面,这可能会有所帮助。该界面使得可以编写 Web UI、命令行控制台等并履行视图的角色。

对于按钮事件,按钮代表对控制器执行的某些命令的调用。

因此,您可以有一个像这样的接口:

public interface MyViewIf {
    // used by the controller to register its self as a listener of the view
    public addViewListener(ViewListener vl);
    ...
}

并且:

public interface ViewListenerIf {
    // used by the View to notify any listeners of control events etc.
    public onViewEvent(ViewEvent ve);
}

然后您的控制器将实现 ViewListenerIf 并将其自身注册到工厂生成的 MyViewIf 实例。这样控制器就不需要知道有关视图类的任何细节。

然后,您的视图类将在内部处理它自己的按钮事件,将它们转换为 ViewEvent 对象,并在向视图注册其自身的控制器上调用 onViewEvent() ,使视图 100% 不知道控制器的存在。

It might help to not think of the view in terms of buttons etc. so much as an interface. The interface makes it possible for web ui's, command line consoles, etc. to be written and fulfill the role of the view.

In the case of your button event, the button represents a call to some command carried out by the controller.

So, you could have an interface like this:

public interface MyViewIf {
    // used by the controller to register its self as a listener of the view
    public addViewListener(ViewListener vl);
    ...
}

and:

public interface ViewListenerIf {
    // used by the View to notify any listeners of control events etc.
    public onViewEvent(ViewEvent ve);
}

Then your controller would implement ViewListenerIf and register it's self with a factory generated instance of MyViewIf. That way the controller doesnt need to know any specifics about your view class(es).

Your view class would then internally handle it's own button events, turn them into ViewEvent objects and call onViewEvent() on the controller that registered it's self with the view, leaving the View 100% oblivious to the existence of the Controller.

梦幻的味道 2024-10-17 21:14:51

在你的视图类中创建一个动作监听器。从你的动作监听器中你可以调用你的控制器。

ActionListener 的代码:

controller.doButtonClick();

这意味着您需要将模型和控制器注入到视图中。我就是这样做的。

Make an actionlistener in your view class. From your actionlistener you call your controller.

Code for actionlistener:

controller.doButtonClick();

This means you need to inject your model and controller to the view. That is how I do it.

累赘 2024-10-17 21:14:51

查看 Spring 框架,深入了解 MVC 模式的实现。
Spring 简要教程 教程

Take a look at the Spring Framework to get an insight in implementing the MVC pattern.
Brief Spring tutorial Tutorial

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