-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.java
More file actions
38 lines (31 loc) · 760 Bytes
/
Data.java
File metadata and controls
38 lines (31 loc) · 760 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
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Timer;
public class Data implements Subject, ActionListener {
private List<Observer> observers;
private int state;
private Timer timer;
public Data() {
observers = new ArrayList<Observer>();
timer = new Timer(1000, this);
timer.start();
}
public void attach(Observer o) {
observers.add(o);
}
public void detach(Observer o) {
int index = observers.indexOf(o);
observers.remove(index);
}
public void notifyObservers() {
for(Observer o : observers) {
o.update(state);
}
}
public void actionPerformed(ActionEvent e) {
state = (int) (Math.random() * 10);
notifyObservers();
}
}