1700473794
super.context.handle1();
1700473795
1700473796
}
1700473797
1700473798
@Override
1700473799
1700473800
public void handle2(){
1700473801
1700473802
//本状态下必须处理的逻辑
1700473803
1700473804
}
1700473805
1700473806
}
1700473807
1700473808
具体环境角色有两个职责:处理本状态必须完成的任务,决定是否可以过渡到其他状态。我们再来看环境角色,如代码清单26-16所示。
1700473809
1700473810
代码清单26-16 具体环境角色
1700473811
1700473812
public class Context{
1700473813
1700473814
//定义状态
1700473815
1700473816
public final static State STATE1=new ConcreteState1();
1700473817
1700473818
public final static State STATE2=new ConcreteState2();
1700473819
1700473820
//当前状态
1700473821
1700473822
private State CurrentState;
1700473823
1700473824
//获得当前状态
1700473825
1700473826
public State getCurrentState(){
1700473827
1700473828
return CurrentState;
1700473829
1700473830
}
1700473831
1700473832
//设置当前状态
1700473833
1700473834
public void setCurrentState(State currentState){
1700473835
1700473836
this.CurrentState=currentState;
1700473837
1700473838
//切换状态
1700473839
1700473840
this.CurrentState.setContext(this);
1700473841
1700473842
}
1700473843
[
上一页 ]
[ :1.700473794e+09 ]
[
下一页 ]