-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathStateLesson.java
More file actions
46 lines (38 loc) · 966 Bytes
/
Copy pathStateLesson.java
File metadata and controls
46 lines (38 loc) · 966 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
package patterns.behavioral;
public class StateLesson {
public static void main(String[] args) {
Context context = new Context(new LeverCaseState(), "Max");
context.doAction();
context.setState(new UpperCaseState());
context.doAction();
}
}
interface State {
void doAction(Context context);
}
class LeverCaseState implements State {
@Override
public void doAction(Context context) {
System.out.println(context.name.toLowerCase());
}
}
class UpperCaseState implements State {
@Override
public void doAction(Context context) {
System.out.println(context.name.toUpperCase());
}
}
class Context {
State state;
String name;
public Context(State state, String name) {
this.state = state;
this.name = name;
}
public void setState(State state) {
this.state = state;
}
public void doAction() {
state.doAction(this);
}
}