forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrafficLight.java
55 lines (44 loc) · 1.34 KB
/
TrafficLight.java
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
51
52
53
54
55
package trafficsignalsystem;
public class TrafficLight {
private final String id;
private Signal currentSignal;
private int redDuration;
private int yellowDuration;
private int greenDuration;
public TrafficLight(String id, int redDuration, int yellowDuration, int greenDuration) {
this.id = id;
this.redDuration = redDuration;
this.yellowDuration = yellowDuration;
this.greenDuration = greenDuration;
this.currentSignal = Signal.RED;
}
public synchronized void changeSignal(Signal newSignal) {
currentSignal = newSignal;
notifyObservers();
}
public Signal getCurrentSignal() {
return currentSignal;
}
public int getRedDuration() {
return redDuration;
}
public void setRedDuration(int redDuration) {
this.redDuration = redDuration;
}
public int getYellowDuration() {
return yellowDuration;
}
public void setYellowDuration(int yellowDuration) {
this.yellowDuration = yellowDuration;
}
public int getGreenDuration() {
return greenDuration;
}
public void setGreenDuration(int greenDuration) {
this.greenDuration = greenDuration;
}
private void notifyObservers() {
// Notify observers (e.g., roads) about the signal change
// ...
}
}