【发布时间】:2014-11-26 03:51:16
【问题描述】:
public void Model {
private SwingPropertyChangeSupport spcs;
public void updatedb();
public void addPropertyChangeListener(PropertyChangeListener listener)
}
public void Gui {
private JFrame view;
Gui() {
view = new JFrame();
init();// for initilizing ui components
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
view.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
void addListener(ActionListener a);
void init();
void updateGUI();
}
public void Controller implements ActionListener,PropertyChangeListener {
Model m;
Gui g;
public void PropertyChange(PropertyChangeEvent e);
public void actionPerformed(ActionEvent e)
Controller(Model m,Gui g){}
}
-
在 3 个类之间传递消息的正确方式是什么 MVC message passsing Image 如何为多个控制器和视图扩展它?我已阅读 SO 答案 The MVC pattern and SWING 说这种实现效率不高?
以及制作控制器 ActionListener 和 PropertyChangeListener 是否正确?当我在 actionperformed() 中调用 updatedb() 时,它是否会使 GUI 变得迟缓?我还阅读了有关 View 具有 Controller 的引用,它将 gui 操作映射到模型操作.这种方式效率更高吗?
哪部分代码应该在 EventQueue.invokeLater 中?将 init() 函数放在 run() 之外是否正确?或者我应该将整个 gui 类包装在 run 中吗?
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gui view = new Gui();
} catch (Exception e) {
e.printStackTrace();
}
}
});
【问题讨论】:
-
有好几个。您可以创建一系列每个级别都需要实现的合同/接口,这将允许您定义每个元素的期望,您还可以使用Observer Pattern,这将允许向感兴趣的各方提供事件通知作为一对想法
-
但我正在寻找遵循设计原则的方法 - 低耦合和高内聚。
-
好吧,这两种建议机制都没有高耦合,因为它们依赖于接口而不是实现,它们提供了一个高度定义的契约,每个级别都应该遵守并在其中工作。您需要问的问题是,在特定情况下哪一个是正确的......
标签: java swing oop design-patterns model-view-controller