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
1700473844
//行为委托
1700473845
1700473846
public void handle1(){
1700473847
1700473848
this.CurrentState.handle1();
1700473849
1700473850
}
1700473851
1700473852
public void handle2(){
1700473853
1700473854
this.CurrentState.handle2();
1700473855
1700473856
}
1700473857
1700473858
}
1700473859
1700473860
环境角色有两个不成文的约束:
1700473861
1700473862
❑把状态对象声明为静态常量,有几个状态对象就声明几个静态常量。
1700473863
1700473864
❑环境角色具有状态抽象角色定义的所有行为,具体执行使用委托方式。
1700473865
1700473866
我们再来看场景类如何执行,如代码清单26-17所示。
1700473867
1700473868
代码清单26-17 具体环境角色
1700473869
1700473870
public class Client{
1700473871
1700473872
public static void main(String[]args){
[
上一页 ]
[ :1.700473823e+09 ]
[
下一页 ]