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

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

问题描述

这个问题围绕着在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组件在视图类中

  • 事件处理匿名类也在视图类中(用户输入)

  • 控制器类包含对模型和视图对象的引用

  • Data are processed within a model class
  • Swing components are within a view class
  • Event handling anonymous classes are also in the view class (user inputs)
  • A controller class contains references to the model and view objects

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

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的副作用?

  • Am我做错了吗?如果是这样,你将如何在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...


我这样做错了吗?

Am I doing this wrong?

恕我直言,是


如果是这样,你将如何在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 - ActionListeners根据MVC模式属于哪里?

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

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