forked from aofeng/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventSource.java
More file actions
executable file
·47 lines (39 loc) · 992 Bytes
/
EventSource.java
File metadata and controls
executable file
·47 lines (39 loc) · 992 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package cn.aofeng.demo.eventdriver_normal;
import java.util.ArrayList;
import java.util.List;
/**
* 事件源(事件发送者)
*
* @author aofeng <aofengblog@163.com>
*/
public class EventSource {
private List<EventListener> listeners = new ArrayList<EventListener>();
public EventSource() {
}
/**
* 添加事件监听器
*
* @param listener 事件监听器
*/
public boolean addListener(EventListener listener) {
return listeners.add(listener);
}
/**
* 移除事件监听器
*
* @param listener 移除事件监听器
*/
public boolean removeListener(EventListener listener) {
return listeners.remove(listener);
}
/**
* 派发事件
*
* @param data 事件
*/
public void fire(Object data) {
for (EventListener listener : listeners) {
listener.execute(new Event(data));
}
}
}