OnJava8-Examples/patterns/StateDemo.java
Bruce Eckel 49edcc8b17 reorg
2015-06-15 17:47:35 -07:00

86 lines
1.7 KiB
Java

//: patterns/StateDemo.java
// ©2015 MindView LLC: see Copyright.txt
// Simple demonstration of the State pattern.
import static com.mindviewinc.util.Print.*;
interface StateBase {
void f();
void g();
void h();
void changeImp(StateBase newImp);
}
class State implements StateBase {
private StateBase implementation;
public State(StateBase imp) {
implementation = imp;
}
@Override
public void changeImp(StateBase newImp) {
implementation = newImp;
}
// Pass method calls to the implementation:
@Override
public void f() { implementation.f(); }
@Override
public void g() { implementation.g(); }
@Override
public void h() { implementation.h(); }
}
class Implementation1 implements StateBase {
@Override
public void f() {
print("Implementation1.f()");
}
@Override
public void g() {
print("Implementation1.g()");
}
@Override
public void h() {
print("Implementation1.h()");
}
@Override
public void changeImp(StateBase newImp) {}
}
class Implementation2 implements StateBase {
@Override
public void f() {
print("Implementation2.f()");
}
@Override
public void g() {
print("Implementation2.g()");
}
@Override
public void h() {
print("Implementation2.h()");
}
@Override
public void changeImp(StateBase newImp) {}
}
public class StateDemo {
static void test(StateBase b) {
b.f();
b.g();
b.h();
}
public static void main(String args[]) {
StateBase b =
new State(new Implementation1());
test(b);
b.changeImp(new Implementation2());
test(b);
}
} /* Output:
Implementation1.f()
Implementation1.g()
Implementation1.h()
Implementation2.f()
Implementation2.g()
Implementation2.h()
*///:~