public class Context { private State state; public void setState(State state) { System.out.println("修改状态"); this.state = state; this.state.handle(); } } public interface State { void handle(); } public class BookState implements State{ @Override public void handle() { // TODO Auto-generated method stub System.out.println("房间已预订"); } } public class CheckedState implements State{ @Override public void handle() { // TODO Auto-generated method stub System.out.println("房间已入住"); } } public class FreeState implements State{ @Override public void handle() { // TODO Auto-generated method stub System.out.println("房间空闲,没人住"); } }
原文地址:http://blog.csdn.net/lovesomnus/article/details/45750039