标签:
State接口类
public interface State { void handle(); }
/** * 已预订状态 * @author Administrator * */ public class BookedState implements State { @Override public void handle() { System.out.println("房间已预订!别人不能定!"); } }CheckedInState类
/** * 已入住状态 * @author Administrator * */ public class CheckedInState implements State { @Override public void handle() { System.out.println("房间已入住!请勿打扰!"); } }FreeState类
/** * 空闲状态 * @author Administrator * */ public class FreeState implements State { @Override public void handle() { System.out.println("房间空闲!!!没人住!"); } }HomeContext类
/** * 房间对象 * @author Administrator * */ public class HomeContext { //如果是银行系统,这个Context类就是账号。根据金额不同,切换不同的状态! private State state; public void setState(State s){ System.out.println("修改状态!"); state = s; state.handle(); } }Client测试类
public class Client { public static void main(String[] args) { HomeContext ctx = new HomeContext(); ctx.setState(new FreeState()); ctx.setState(new BookedState()); } }
【行为型模式】状态模式(state)之23种java设计模式
标签:
原文地址:http://blog.csdn.net/liusocg520/article/details/51328890