在 Java Swing 中实现 MVC 的控制器部分 [英] Implementing the Controller part of MVC in Java Swing

查看:22
本文介绍了在 Java Swing 中实现 MVC 的控制器部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是围绕在 Java 中实现 MVC 架构的标准实践 - 特别是 Swing.我知道之前有人问过这个问题,但我想更具体地说明控制器的使用.

This question is around the standard practice for implementing the MVC architecture in Java - Swing particularly. I know this question has been asked before but I want to be a bit more specific around the use of the Controller.

我一直在分离我的模型、视图和控制器,其中:

I've been separating my models, views, and controllers where:

  • 在模型类中处理数据
  • Swing 组件位于视图类中
  • 事件处理匿名类也在视图类中(用户输入)
  • 控制器类包含对模型和视图对象的引用

回到控制器的具体问题——我一直在使用类(静态)变量来表示控制器,以便我的应用程序的任何部分都能轻松访问.所以我的控制器类可能如下所示:

Back to the specific of the controller - I've been using class (static) variables to represent controllers for easy access by any part of my application. So my controller classes may look like this:

public class Controller {
    public static ControllerA controllerA;
    public static ControllerB controllerB;
}

public class ControllerA {
    private JPanel panel1;
    private JPanel panel2;
    private DefaultListModel list;
}

...

每次我需要做某事时,我都会像这样调用控制器:

Every time I need to do something I would call the controller like this:

Controller.controllerA.doSomething

这意味着我的大部分代码都位于控制器中.通常如果我需要做某事,控制器会有一个方法,它通常需要视图或模型中的等效方法来完成任务.我发现这会导致代码重复.例如:

This means that the bulk of my code are located within the controller. Often if I need to do something, the controller would have a method for it, and it often requires an equivalent method in the view or the model to accomplish the task. I find that this leads to code duplication. For example:

Controller.controllerA.removeElement();

将在控制器中调用以下方法:

Will call the following method in the controller:

protected void removeElement() {
    list.removeElement()
}

随着我的应用程序的增长,我看到很多实例,其中控制器只是镜像模型或视图所需的操作,因为它是视图/模型的主要访问点.

As my application grow, I'm seeing a lot of instances where the controller simply mirror the action required by the model or the view because it is the main access point to the view/model.

所以我的问题是:

  • 使用静态变量访问控制器对象是否是一个好习惯?
  • 控制器->模型和控制器->视图之间的代码重复是MVC的副作用吗?
  • 我做错了吗?如果是这样,您将如何在 MVC 中实现控制器?

推荐答案

使用静态变量访问控制器对象是否是一个好习惯?

Is it a good practice to use static variable for accessing controller objects?

没有.你需要意识到你可以有多个控制器用于同一个视图(它的不同实例),所以这很快就会变成意大利面条式的混乱并且难以控制和维护

No. You need to realise that you could have multiple controllers for the same view (different instance of it), so this quickly becomes a spaghetti mess and is difficult to control and maintain

控制器->模型和控制器->视图之间的代码重复是MVC的副作用吗?

Is the code duplication between controller->model and controller->view a side effect of MVC?

不,我会说这是糟糕的设计.您应该有一个意图,即视图可以由满足两者之间合同要求的任何控制器控制(视图知道如何与控制器通信,反之亦然),模型也是如此.一个控制器可能会将许多不同的模型桥接到一个视图...

No, I would say that's bad design. You should have the intention that a view could be controlled by any controller which meets the contractual requirements between the two (the view knows how to communicate with the controller and visa-versa), same goes with the model. A controller might bridge many different models to a view...

我做错了吗?

恕我直言,是的

如果是这样,您将如何在 MVC 中实现控制器?

If so, how would you implement a controller within the MVC?

这是一个非常广泛的问题.

That is a very broad question.

首先关注界面级别,设计描述每个部分的期望的界面、模型、该模型的控制器、视图和该视图的控制器.为了满足模型和视图的要求,控制器很可能需要实现至少两个接口,但这意味着它不关心任何一个的实现.

Start by focusing on at the interface level, design interfaces which describe the expectations of each section, the model, the controller for that model, the view and the controller for that view. A controller will most likely need to implement at least two interfaces in order to satisfy the requirements of the model and the view, but this means that it doesn't care about the implementation of either.

模型和视图也是如此,他们不应该关心控制器的实现,只要它满足他们的合同要求即可.

The same goes for the model and view, they shouldn't care about the implementation of the controller, only that it meets their contractual requirements.

Swing 没有实现纯 MVC,因此尝试围绕 Swing 实现一个有很多问题.Swing控件是VC和模型分离的.这种方法有利有弊.

Swing doesn't implement a pure MVC, hence there are so many problems with trying implement one around Swing. Swing controls are the VC and the model is separated. There are pros and cons to this approach.

话虽如此,您目前开始设置 MVC 的方法是一种很好的方法,视图中的 UI 相关元素、模型中的数据元素以及协调这一切的控制器......

Having said that, your current approach to the way you've started setting up the MVC is a good one, UI related elements in the view, data elements in the model and the controller to coordinate it all...

举个例子,你可以看看 Java 和 GUI - 根据 MVC 模式,ActionListeners 属于哪里?

For an example, you could take a look at Java and GUI - Where do ActionListeners belong according to MVC pattern?

这篇关于在 Java Swing 中实现 MVC 的控制器部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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