-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.java
More file actions
97 lines (79 loc) · 3.19 KB
/
Copy pathCar.java
File metadata and controls
97 lines (79 loc) · 3.19 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Note:
- Modified: SpeedEvent does not extend java.util.EventObject, and SpeedListener does not extend java.util.EventListener
- Extend if want to use info of Car, but to make this 'pure' custom event, I've decided not to extend EventObject.
*/
import java.util.ArrayList;
public class Car {
// Inner class
private static class MySpeedListener implements SpeedListener {
@Override
public void speedExceeded(SpeedEvent e) {
if (e.getCurrentSpeed() > e.getMaxSpeed()) {
System.out.println("Alert! You have exceeded " + (e.getCurrentSpeed() - e.getMaxSpeed() + " MPH!"));
}
}
@Override
public void speedGoneBelow(SpeedEvent e) {
if (e.getCurrentSpeed() < e.getMinSpeed()) {
System.out.println("Uhm... you are driving " + e.getCurrentSpeed() + " MPH. Speed up!");
}
}
}
private int maxSpeed;
private int minSpeed;
private int currentSpeed;
private ArrayList<SpeedListener> speedListenerList = new ArrayList<SpeedListener>();
public static void main(String[] args) {
Car myCar = new Car(60, 40, 50);
SpeedListener listener = new MySpeedListener();
myCar.addSpeedListener(listener);
// Add more listeners if you want
myCar.speedUp(50); // fires SpeedEvent
myCar.speedUp(50); // fires SpeedEvent
myCar.slowDown(70);
myCar.slowDown(70); // fires SpeedEvent
}
private Car(int max, int min, int cur) {
this.maxSpeed = max;
this.minSpeed = min;
this.currentSpeed = cur;
}
// Register an event listener
public synchronized void addSpeedListener(SpeedListener listener) {
if (!speedListenerList.contains(listener)) {
speedListenerList.add(listener);
}
}
public void speedUp(int increment) {
this.currentSpeed += increment;
if (this.currentSpeed > this.maxSpeed) {
// fire SpeedEvent
// processSpeedEvent(new SpeedEvent(this, this.maxSpeed, this.minSpeed, this.currentSpeed));
// When SpeedEvent does not extend java.util.EventObject
processSpeedEvent(new SpeedEvent(this.maxSpeed, this.minSpeed, this.currentSpeed));
}
}
public void slowDown(int increment) {
this.currentSpeed -= increment;
if (this.currentSpeed < this.minSpeed) {
// fire SpeedEvent
// processSpeedEvent(new SpeedEvent(this, this.maxSpeed, this.minSpeed, this.currentSpeed));
// When SpeedEvent does not extend java.util.EventObject
processSpeedEvent(new SpeedEvent(this.maxSpeed, this.minSpeed, this.currentSpeed));
}
}
private void processSpeedEvent(SpeedEvent speedEvent) {
ArrayList<SpeedListener> tempSpeedListenerList;
synchronized (this) {
if (speedListenerList.size() == 0) {
return;
}
tempSpeedListenerList = (ArrayList<SpeedListener>) speedListenerList.clone();
}
for (SpeedListener listener : tempSpeedListenerList) {
listener.speedExceeded(speedEvent);
listener.speedGoneBelow(speedEvent);
}
}
}