forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateDemo.java
More file actions
68 lines (64 loc) · 1.6 KB
/
StateDemo.java
File metadata and controls
68 lines (64 loc) · 1.6 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
// patterns/StateDemo.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Basic demonstration of the State pattern.
class State {
private State implementation;
protected State() {}
public State(State imp) {
implementation = imp;
}
public void change(State newImp) {
implementation = newImp;
}
// Forward method calls to the implementation:
public void f() { implementation.f(); }
public void g() { implementation.g(); }
public void h() { implementation.h(); }
}
class Implementation1 extends State {
@Override public void f() {
System.out.println("Implementation1.f()");
}
@Override public void g() {
System.out.println("Implementation1.g()");
}
@Override public void h() {
System.out.println("Implementation1.h()");
}
}
class Implementation2 extends State {
@Override public void f() {
System.out.println("Implementation2.f()");
}
@Override public void g() {
System.out.println("Implementation2.g()");
}
@Override public void h() {
System.out.println("Implementation2.h()");
}
}
public class StateDemo {
static void test(State s) {
s.f();
s.g();
s.h();
}
public static void main(String[] args) {
State s = new State(new Implementation1());
test(s);
System.out.println("Changing implementation");
s.change(new Implementation2());
test(s);
}
}
/* Output:
Implementation1.f()
Implementation1.g()
Implementation1.h()
Changing implementation
Implementation2.f()
Implementation2.g()
Implementation2.h()
*/