public class StatePatternDemo {
public static void main(String[] args) {
Context context = new Context(new NewState());
context.execute(1);
context.execute(2);
context.execute(3);
}
public interface State {
void execute();
}
public static class NewState implements State {
@Override
public void execute() {
System.out.println("执行销售出库单新建状态的逻辑");
}
}
public static class ApprovingState implements State {
@Override
public void execute() {
System.out.println("执行销售出库单待审批状态的逻辑");
}
}
public static class ApprovedState implements State {
@Override
public void execute() {
System.out.println("执行销售出库单已审批状态的逻辑");
}
}
public static class FinishedState implements State {
@Override
public void execute() {
System.out.println("执行销售出库单已完成状态的逻辑");
}
}
public static class Context {
private State state;
public Context(State state) {
this.state = state;
this.state.execute();
}
public void execute(int stateType) {
if(stateType == 1) {
this.state = new ApprovingState();
this.state.execute();
} else if(stateType == 2) {
this.state = new ApprovedState();
this.state.execute();
} else if(stateType == 3) {
this.state = new FinishedState();
this.state.execute();
}
}
}
}