状态模式

本文通过一个电话状态切换的例子,介绍了状态模式的设计与实现。具体包括定义不同的状态类如连接中、等待和关机,并在这些状态间进行转换。

 

状态模式图 ,最根本的就是状态切换,这里采用gof推荐的在具体的状态中进行状态切换
具体的类如下:
 
publicinterface State {
    publicvoid call();
    publicvoid countTimes();
    public String showState();
}
 
 
publicclass ConnectState implements State {
    private MobileTelephone mobile;
    public ConnectState(MobileTelephone mobile){
       this.mobile=mobile;
    }
    @Override
    publicvoid call() {
       // TODO Auto-generated method stub
       System.out.println("用户正在通话,请稍后再拨....");
       mobile.setState(mobile.getWaitState());
       System.out.println("手机状态进入("+mobile.getWaitState().showState()+")");
    }
 
    @Override
    publicvoid countTimes() {
       // TODO Auto-generated method stub
       System.out.println("计时处理.....");
       System.out.println("通话结束,计时结束");
    }
 
    @Override
    public String showState() {
       // TODO Auto-generated method stub
       return"Mobile is connecting";
    }
 
}
 
 
publicclass WaitState implements State{
    private MobileTelephone mobile;
    public WaitState(MobileTelephone mobile){
    this.mobile=mobile;
    }
    @Override
    publicvoid call() {
       // TODO Auto-generated method stub
       System.out.println("电话连接成功,可以通话.....");
       mobile.setState(mobile.getConnectState());
       System.out.println("手机状态进入("+mobile.getConnectState().showState()+")");
    }
  
    @Override
    publicvoid countTimes() {
       // TODO Auto-generated method stub
       System.out.println("电话不在连接状态,不能计时....");
      
    }
 
    @Override
    public String showState() {
       // TODO Auto-generated method stub
       return"Mobile is waiting";
    }
}
 
 
publicclass StopState implements State {
    private MobileTelephone mobile;
    public StopState(MobileTelephone mobile){
       this.mobile=mobile;
    }
    @Override
    publicvoid call() {
       // TODO Auto-generated method stub
       System.out.println("用户已关机,请稍后在拨....");
       mobile.setState(mobile.getWaitState());
       System.out.println("手机状态进入("+mobile.getWaitState().showState()+")");
    }
 
    @Override
    publicvoid countTimes() {
       // TODO Auto-generated method stub
       System.out.println("手机已关机,计时处理已关闭.....");
    }
 
    @Override
    public String showState() {
       // TODO Auto-generated method stub
       return"手机处于关机状态";
    }
   
 
}
 
 
publicclass MobileTelephone{
    //private State waitState=new WaitState();
    //private State connectState=new ConnectState();
   // private IState stopState=new StopState();
    private State connectState,waitState,stopState;
    private State state;
    //构造函数,初始化mobile状态
    public MobileTelephone(){
        connectState=new ConnectState(this);
       waitState=new WaitState(this);
       stopState=new StopState(this);
       state=stopState;
    }
    //private State state=waitState;
    publicvoid call(){
       state.call();
    }
    publicvoid countTimes(){
       state.countTimes();
    }
    publicvoid showState(State concreteState){
       this.state=concreteState;
    }
    public State getState() {
       returnstate;
    }
    publicvoid setState(State state) {
       this.state = state;
    }
    public State getConnectState() {
       returnconnectState;
    }
    publicvoid setConnectState(State connectState) {
       this.connectState = connectState;
    }
    public State getWaitState() {
       returnwaitState;
    }
    publicvoid setWaitState(State waitState) {
       this.waitState = waitState;
    }
}
 
 
publicclass Test {
    publicstaticvoid main(String[] args){
       MobileTelephone mobile=new MobileTelephone();
       mobile.call();
       mobile.countTimes();
    }
}
运行结果如下:
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值