-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperature.java
More file actions
51 lines (40 loc) · 1.16 KB
/
Temperature.java
File metadata and controls
51 lines (40 loc) · 1.16 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
package behavioral.command;
import java.util.Objects;
public class Temperature {
private final static Integer MIN = 16;
private final static Integer MAX = 32;
private static Temperature temperature;
private Integer currentTemp;
private Temperature() {
this.currentTemp = 25;
}
public static Temperature getInstance() {
if (Objects.isNull(temperature)) {
synchronized (Temperature.class) {
if (Objects.isNull(temperature)) {
temperature = new Temperature();
}
}
}
return temperature;
}
public synchronized int getCurrentTemp() {
return currentTemp;
}
public synchronized Integer tempUp() {
if (this.currentTemp + 1 <= Temperature.MAX) {
this.currentTemp++;
}
return this.currentTemp;
}
public synchronized Integer tempDown() {
if (this.currentTemp + 1 >= Temperature.MIN) {
this.currentTemp--;
}
return this.currentTemp;
}
@Override
public String toString() {
return "Temperature is set on '" + currentTemp + "'";
}
}