java 如何实现mvc,在Java Swing中实现MVC的Controller部分

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:

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.

So my questions are:

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

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

Am I doing this wrong? If so, how would you implement a controller within the 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

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?

IMHO, yes

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.

Food for thought

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.

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...

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值