public interface PhoneState {
void handle(Phone phone);
}
public class NormalState implements PhoneState {
@Override
public void handle(Phone phone) {
System.out.println("系统关机");
phone.setPhoneState(new ShutdownState());
}
}
public class ShutdownState implements PhoneState {
@Override
public void handle(Phone phone) {
System.out.println("系统开机");
phone.setPhoneState(new NormalState());
}
}
public class Phone {
private PhoneState phoneState;
public Phone(){
this.phoneState=new ShutdownState();
}
public void clickButton(){
phoneState.handle(this);
}
public void setPhoneState(PhoneState phoneState) {
System.out.println("状态改变:"+phoneState.toString());
this.phoneState = phoneState;
}
}
public class App {
public static void main(String[] args) {
Phone phone=new Phone();
phone.clickButton();
phone.clickButton();
}
}