001package state.two; 002 003interface I { 004 public int f(); 005 public int g(); 006 public void changeDirection(); 007} 008 009class C implements I { 010 private CState[] states = new CState[] { new CStateMinus(), new CStatePlus() }; 011 private int index; 012 int i; 013 int j; 014 public int f() { 015 return states[index].f(this); 016 } 017 public int g() { 018 return states[index].g(this); 019 } 020 public void changeDirection() { 021 index = (index+1) % 2; 022 } 023} 024 025interface CState { 026 public int f(C x); 027 public int g(C x); 028} 029class CStateMinus implements CState { 030 public int f(C x) { 031 x.i -= 32; 032 return x.i; 033 } 034 public int g(C x) { 035 x.j -= 27; 036 return x.j; 037 } 038} 039class CStatePlus implements CState { 040 public int f(C x) { 041 x.i += 26; 042 return x.i; 043 } 044 public int g(C x) { 045 x.j += 42; 046 return x.j; 047 } 048} 049