欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

状态模式

程序员文章站 2022-06-17 17:09:24
...

状态模式状态模式

package state;
/***
 * 房间对象
 * 持有不同对象
 * @author zw
 *
 */
public class Context {
	private State state;
	
	public void setState(State state) {
		System.out.println("修改状态");
		this.state = state;
		state.handle();
	}
}

package state;

public interface State {
	void handle();
}

package state;
/***
 * 预定状态 
 * @author zw
 *
 */
public class FreeState implements State{

	@Override
	public void handle() {
		// TODO Auto-generated method stub
		System.out.println("房间已经预定,别人不能预定");
	}

}

package state;
/***
 * 入住 状态 
 * @author zw
 *
 */
public class InState2 implements State{

	@Override
	public void handle() {
		// TODO Auto-generated method stub
		System.out.println("房间已经入住,请勿打扰");
	}

}


package state;
/***
 * 空闲状态
 * @author zw
 *
 */
public class Reserve implements State{

	@Override
	public void handle() {
		// TODO Auto-generated method stub
		System.out.println("房间空闲,没人住");
	}

}



package state;

public class Client {
	public static void main(String[] args) {
		State s1 = new FreeState();
		State s2 = new Reserve();
		State s3 = new InState2();
		
		
		Context c = new Context();
		c.setState(s1);
		c.setState(s2);
		c.setState(s3);
		
	}
}


相关标签: 状态模式