001package state.five; 002 003interface I { 004 public int f(); 005 public int g(); 006 public void changeDirection(); 007} 008 009class C implements I { 010 private State state = new StateMinus(); 011 private int i; 012 private int j; 013 public int f() { 014 return state.f(); 015 } 016 public int g() { 017 return state.g(); 018 } 019 public void changeDirection() { 020 state = state.next(); 021 } 022 023 interface State { 024 public int f(); 025 public int g(); 026 public State next(); 027 } 028 class StateMinus implements State { 029 public int f() { 030 i -= 32; 031 return i; 032 } 033 public int g() { 034 j -= 27; 035 return j; 036 } 037 public State next() { 038 return new StatePlus(); 039 } 040 } 041 class StatePlus implements State { 042 public int f() { 043 i += 26; 044 return i; 045 } 046 public int g() { 047 j += 42; 048 return j; 049 } 050 public State next() { 051 return new StateMinus(); 052 } 053 } 054} 055