-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubject.java
More file actions
50 lines (38 loc) · 892 Bytes
/
Subject.java
File metadata and controls
50 lines (38 loc) · 892 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
48
49
50
package simple_implimentation;
import java.util.ArrayList;
import java.util.List;
public class Subject implements Runnable {
private List<Observer> observers = new ArrayList<Observer>();
private int state;
public Subject(int val) {
this.setState(val);
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
notifyAllObservers();
}
public void subscribe(Observer observer){
observers.add(observer);
}
public void unsubscribe(Observer observer){
observers.remove(observer);
}
public void notifyAllObservers(){
for (Observer observer : observers) {
observer.update();
}
}
@Override
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {
this.setState((this.getState() + 1) % 60);
Thread.sleep(2000);
}
} catch (InterruptedException e) {
}
}
}