状态模式
程序员文章站
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);
}
}
上一篇: Ganglia分布式监控部署
下一篇: 设计模式与应用:状态模式