001package state.four; 002 003interface I { 004 public int f(); 005 public int g(); 006 public void changeDirection(); 007} 008 009class C implements I { 010 private CState state = new CStateMinus(this); 011 int i; 012 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 024interface CState { 025 public int f(); 026 public int g(); 027 public CState next(); 028} 029class CStateMinus implements CState { 030 C x; 031 CStateMinus(C x) { this.x = x; } 032 public int f() { 033 x.i -= 32; 034 return x.i; 035 } 036 public int g() { 037 x.j -= 27; 038 return x.j; 039 } 040 public CState next() { 041 return new CStatePlus(x); 042 } 043} 044class CStatePlus implements CState { 045 C x; 046 CStatePlus(C x) { this.x = x; } 047 public int f() { 048 x.i += 26; 049 return x.i; 050 } 051 public int g() { 052 x.j += 42; 053 return x.j; 054 } 055 public CState next() { 056 return new CStateMinus(x); 057 } 058} 059