interface State { void operation1(); void operation2(); void operation3(); }
class ServiceProvider { private State state; public ServiceProvider(State state) { this.state = state; } public void changeState(State newState) { state = newState; } // Pass method calls to the implementation: public void service1() { state.operation1(); state.operation3(); } public void service2() { state.operation1(); state.operation2(); } public void service3() { state.operation3(); state.operation2(); } }
class StateA implements State { public void operation1() { System.out.println("StateA.operation1()"); } public void operation2() { System.out.println("StateA.operation2()"); } public void operation3() { System.out.println("StateA.operation3()"); } }
class StateB implements State { public void operation1() { System.out.println("StateB.operation1()"); } public void operation2() { System.out.println("StateB.operation2()"); } public void operation3() { System.out.println("StateB.operation3()"); } }
public class StateDemo extends TestCase { static void run(ServiceProvider sp) { sp.service1(); sp.service2(); sp.service3(); } ServiceProvider sp = new ServiceProvider(new StateA()); public void test() { run(sp); // change state .we can change implemention. sp.changeState(new StateB()); run(sp); } public static void main(String args[]) { junit.textui.TestRunner.run(StateDemo.class); } } ///:~