package com.designepattern.statepattern.v2;
/**
* @Author:glx
* @Date:2020/12/10
* @Content:
*/
public class MainDemo {
public static void main(String[] args) {
Context context=new Context();
context.handle();
context.handle();
context.handle();
}
}
class Context{
State state=new StateImplA();
void handle(){
state.handle(this);
}
public void setState(State state) {
this.state = state;
}
}
interface State{
void handle(Context context);
}
class StateImplA implements State{
@Override
public void handle(Context context) {
System.out.println("開始傳入---"+"new StateImplB");
context.setState(new StateImplB());
}
}
class StateImplB implements State{
@Override
public void handle(Context context) {
System.out.println("開始傳入---"+"new StateImplA");
context.setState(new StateImplA());
}
}