单一职责原则和事件监听器
我目前正在尝试开发一个简单的 GUI 记事本来提高我的 OOP 技能。 我在开发时坚持单一职责原则。 我按照原则将应用程序分为几个部分。
// This class runs the whole application
public class Notepad {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new NotepadFrame();
}
});
}
}
// This class is responsible for setting up a frame
public class NotepadFrame extends JFrame {
public NotepadFrame() {
super("Notepad");
MenuBar menuBar = new MenuBar();
setJMenuBar(menuBar.createMenuBar());
pack();
setVisible(true);
}
}
// This class sets up a menu bar
public class MenuBar {
public JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
Buttons buttons = new Buttons();
menuBar.add(buttons.createFileMenuItems());
menuBar.add(buttons.createEditMenuItems());
menuBar.add(buttons.createFormatMenuItems());
menuBar.add(buttons.createHelpMenuItems());
return menuBar;
}
}
还有其他类。 Buttons - 此类用于创建菜单栏的按钮。 MenuItemActionListeners - 此类处理按钮触发的所有事件。
我是否按照简单责任原则正确划分了应用程序?
我还想找出处理动作侦听器、鼠标侦听器等的最佳方法。
到目前为止,我已经使用了一种 actionPerformed(SomeEvent e) 方法来处理所有事件。
private JButton button;
private JButton button2;
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == button) {
} else if (source == button2) {
} // etc.
}
我意识到这是处理事件侦听器的糟糕方法。随着应用程序规模的增大,添加过多的条件语句会导致代码可读性降低,CPU 性能也会下降。
您如何处理应用程序中的事件侦听器? 在开发 MS Office 或 AutoCAD 等大型且重要的应用程序时,您将如何编写事件侦听器?
I'm currently trying to develop a simple GUI notepad to increase my skills in OOP.
I stick to the Single Responsibility Principle while developing.
I've divided the application into several parts following the principle.
// This class runs the whole application
public class Notepad {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new NotepadFrame();
}
});
}
}
// This class is responsible for setting up a frame
public class NotepadFrame extends JFrame {
public NotepadFrame() {
super("Notepad");
MenuBar menuBar = new MenuBar();
setJMenuBar(menuBar.createMenuBar());
pack();
setVisible(true);
}
}
// This class sets up a menu bar
public class MenuBar {
public JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
Buttons buttons = new Buttons();
menuBar.add(buttons.createFileMenuItems());
menuBar.add(buttons.createEditMenuItems());
menuBar.add(buttons.createFormatMenuItems());
menuBar.add(buttons.createHelpMenuItems());
return menuBar;
}
}
There are also the other classes.
Buttons - this class is used to create the buttons of the menu bar.
MenuItemActionListeners - this class handles all of the events fired by the buttons.
Have I divided the application in a correct way according to the Simple Responsibility Principle?
I'd also like to find out the best ways to handle action listeners, mouse listeners etc.
So far, I have used one actionPerformed(SomeEvent e) method to handle all of the events.
private JButton button;
private JButton button2;
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == button) {
} else if (source == button2) {
} // etc.
}
I realize this is the bad way of handling event listeners. As the application's size grows, adding so many conditional statements will make the code lest readable and the CPU performance will decrease.
How do you handle event listeners in your applications?
How would you write event listeners when developing huge and serious applications like MS Office or AutoCAD?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅如何使用操作,其中显示了
Action
“可用于分离功能和来自组件的状态。” Charles Bell 的HTMLDocumentEditor
是该示例展示了如何重用现有操作,以及如何基于AbstractAction
创建您自己的操作。See How to Use Actions, which shows how
Action
"can be used to separate functionality and state from a component." Charles Bell'sHTMLDocumentEditor
is an example that shows how to reuse existing actions, as well as creating your own based onAbstractAction
.您正在使用一个位置来处理所有事件,这是一个问题。在更大的应用程序中,您将以不同的方式处理事件,应用程序中的不同类将处理不同的事件。组件仅监听它们感兴趣的少数事件,而不是所有事件。例如,
撤消事件
仅对段落对象
有意义,而对文件对象
没有意义,保存事件
对于菜单对象
和文档对象
等来说是有意义的。理想情况下,您的类将引发某些事件,并且它将允许任何其他类订阅这些事件。当事件发生时,这些订阅者会收到通知。
我建议您查看观察者设计模式。
You are using one location where you handle all the events, which is a problem. In bigger applications you will handle events differently, different classes in your application will handle different events. Components listen to a few events only in which they are interested and not to all. For exmaple, a
undo event
makes sense to aparagraph object
only and not toFile object
, asave event
would make sense to themenu object
and thedocument object
.. etc.Ideally, you classes would raise certain events and it will allow any other classes to subscribe to those events. Those subscribes are notified when an event occurs.
I would suggest you to look at the Observer Design Pattern.
我很高兴看到您想在编程时使用 SRP。你所做的方式是完全可以接受的。然而,始终存在一个问题:您想要应用这些原则的哪个级别?
此外,在您提供的代码(actionPerformed 方法)中,它不符合 OCP(开闭原则),因此是一个糟糕的设计。相反,我将为您拥有的每个组件中的每种类型的事件提供单独的方法。
为了保持代码干燥,您可以使用一个方法来告诉您在选择组件时要做什么。从您为此组件关联的多个方法(由于不同类型的事件)委托此方法。
希望这有帮助..
I am very happy to see you want to use SRP while programming. The way you have done is perfectly acceptable. However, there is always a question on what level of these principles do you want to apply?
Also, in the code you provided (actionPerformed method), it is not OCP (Open Closed Principle) compliant and hence a bad design. Instead I would have separate methods for each type of event in each of those components that you have.
To keep the code DRY, you can have a method which tells you what to do when a component is selected. Delegate to this method from the several methods (due to different type of events) you associate for this component.
Hope this helps..