forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateAnonymous.java
More file actions
30 lines (26 loc) · 820 Bytes
/
DuplicateAnonymous.java
File metadata and controls
30 lines (26 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// BAD: Duplicate anonymous classes:
button1.addActionListener(new ActionListener() {
public void actionPerfored(ActionEvent e)
{
for (ActionListener listener: listeners)
listeners.actionPerformed(e);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerfored(ActionEvent e)
{
for (ActionListener listener: listeners)
listeners.actionPerformed(e);
}
});
// ... and so on.
// GOOD: Better solution:
class MultiplexingListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
for (ActionListener listener : listeners)
listener.actionPerformed(e);
}
}
button1.addActionListener(new MultiplexingListener());
button2.addActionListener(new MultiplexingListener());
// ... and so on.