SE450: State: Refactored to state using inner class [21/24] Previous pageContentsNext page

file:I.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
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
package state.five;

interface I {
  public int f();
  public int g();
  public void changeDirection();
}

class C implements I {
  private State state = new StateMinus();
  private int i;
  private int j;
  public int f() {
    return state.f();
  }
  public int g() {
    return state.g();
  }
  public void changeDirection() {
    state = state.next();
  }

  interface State {
    public int f();
    public int g();
    public State next();
  }
  class StateMinus implements State {
    public int f() {
      i -= 32;
      return i;
    }
    public int g() {
      j -= 27;
      return j;
    }
    public State next() {
      return new StatePlus();
    }
  }
  class StatePlus implements State {
    public int f() {
      i += 26;
      return i;
    }
    public int g() {
      j += 42;
      return j;
    }
    public State next() {
      return new StateMinus();
    }
  }
}

Previous pageContentsNext page