package com.coderbean.state;
/**
* Created by Chang on 15/10/5.
*/publicinterfaceState {void handle();
}
FreeState
package com.coderbean.state;
/**
* Created by Chang on 15/10/5.
*/publicclassFreeStateimplementsState {@Overridepublicvoidhandle() {
System.out.println("房间空闲,没人住");
}
}
BookedState
package com.coderbean.state;
/**
* Created by Chang on 15/10/5.
*/publicclassBookedStateimplementsState {@Overridepublicvoidhandle() {
System.out.println("房间已预定,别人不能定");
}
}
CheckedState
package com.coderbean.state;
/**
* Created by Chang on 15/10/5.
*/publicclassCheckedStateimplementsState {@Overridepublicvoidhandle() {
System.out.println("房间已入住,请勿打扰");
}
}
Context
package com.coderbean.state;
/**
* Created by Chang on 15/10/5.
*/publicclassContext {private State state;
publicvoidsetState(State state) {
System.out.println("修改状态");
this.state = state;
state.handle();
}
}
Client
package com.coderbean.state;
/**
* Created by Chang on 15/10/5.
*/publicclassClient {publicstaticvoidmain(String[] args) {
Context context = new Context();
context.setState(new FreeState());
context.setState(new BookedState());
}
}