-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS21_State.java
More file actions
55 lines (39 loc) · 962 Bytes
/
DS21_State.java
File metadata and controls
55 lines (39 loc) · 962 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
51
52
53
54
55
//Context
class Context {
private Weather weather;
public void setWeather(Weather weather) {
this.weather = weather;
}
public Weather getWeather() {
return this.weather;
}
public String weatherMessage() {
return weather.getWeather();
}
}
//state
interface Weather {
String getWeather();
}
//Concrete*tatesubclasses
class Rain implements Weather {
public String getWeather() {
return "下雨";
}
}
class Sunshine implements Weather {
public String getWeather() {
return "阳光";
}
}
class DS21_State{
public static void main(String[] args) {
Context ctx1 = new Context();
ctx1.setWeather(new Sunshine());
System.out.println(ctx1.weatherMessage());
System.out.println("===============");
Context ctx2 = new Context();
ctx2.setWeather(new Rain());
System.out.println(ctx2.weatherMessage());
}
}