forked from androdev-cft6/thinking-in-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
executable file
·41 lines (35 loc) · 1.08 KB
/
App.java
File metadata and controls
executable file
·41 lines (35 loc) · 1.08 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
/*
* (3) Following the example in Transmogrify.java, create a Starship class
* containing an AlertStatus reference that can indicate three different states.
* Include methods to change the states.
*/
class AlertStatus {
public void alert() {}
}
class NormalStatus extends AlertStatus {
@Override
public void alert() { System.out.println("Normal"); }
}
class AlarmStatus extends AlertStatus {
@Override
public void alert() { System.out.println("Alarm"); }
}
class DangerStatus extends AlertStatus {
@Override
public void alert() { System.out.println("Danger"); }
}
class Starship {
private AlertStatus alertStatus = new NormalStatus();
public void changeStatus(AlertStatus status) { alertStatus = status; }
public void performAlert() { alertStatus.alert(); }
}
public class App {
public static void main(String[] args) {
Starship ship = new Starship();
ship.performAlert();
ship.changeStatus(new AlarmStatus());
ship.performAlert();
ship.changeStatus(new DangerStatus());
ship.performAlert();
}
}