我正在准备面向对象建模和设计的考试,但无法弄清楚这个问题。
设计违反开闭原则;你不能在不修改类的情况下添加更多的 JButton。重做设计,使之成为可能。设计应包含三个按钮和事件管理。避免重复代码。用类图展示新设计。
//other code
private Application application;
private JButton b1, b2, b3;
class ActionHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
application.action1();
} else if (e.getSource() == b2) {
application.action2();
} else {
application.action3();
}
}
}