问题描述
我正在使用MVC设计模式编写GUI程序。我有一个关于使用ActionListeners的问题。
I am writing program with GUI using MVC design pattern. I have a question concerning using ActionListeners.
根据MVC模式,所有ActionListener都应该包含在Controller中。通常情况下,我认为它们将被实现为内部类,如。
According to the MVC pattern, all of the ActionListeners should be included into Controller. Normally, as I believe they will be implemented into inner classes like.
然而,如果有许多按钮等,将这些内部类移动到单独的文件是个好主意在同一个包裹?我知道他们不再是内班了,所以这是一个好的设计吗?因为我需要使用Controller类中的局部变量,我可以简单地将它们设置为默认访问吗?
However in case of many buttons etc. is it a good idea to move those inner classes to separate files in the same package? I know that they will not be inner classes any more, so is it a good design? And as I would need to use local variable from the Controller class can I simply set them a default access?
控制器类:
public class Controller{
GoogleMaps gMaps = null; // model
GUI gui = null; // view
public Controller(GoogleMaps gMaps, GUI gui) {
super();
this.gMaps = gMaps;
this.gui = gui;
this.gui.addButtonDownListener(new ButtonDownListener(this));
}
}
ButtonDownLister类:
ButtonDownLister class:
class ButtonDownListener implements ActionListener{
private BudgetController buttonDownListener;
public ButtonDownListener(BudgetController buttonDownListener) {
super();
this.buttonDownListener = buttonDownListener;
}
@Override
public void actionPerformed(ActionEvent e) {
// some ActionEvent handler
}
}
推荐答案
可能不是完美的MVC,但你仍然可以拥有内部类ActionListeners只是调用实际执行工作的控制器。
Might not be perfect MVC, but you can still have inner class ActionListeners that just make calls to the controller that actually carries out the work.
所以 //一些ActionEvent处理程序
仍然是在财务主任中完成。
So the // some ActionEvent handler
is still completed in the Controller.
这篇关于具有许多ActionListeners的MVC模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!