1700473446
1700473447
我来解释一下这个类的几个方法,Openning状态是由open()方法产生的,因此,在这个方法中有一个具体的业务逻辑,我们是用print来代替了。在Openning状态下,电梯能过渡到其他什么状态呢?按照现在的定义的是只能过渡到Closing状态,因此我们在Close()中定义了状态变更,同时把Close这个动作也委托了给CloseState类下的Close方法执行,这个可能不好理解,我们再看看Context类可能好理解一点,如代码清单26-9所示。
1700473448
1700473449
代码清单26-9 上下文类
1700473450
1700473451
public class Context{
1700473452
1700473453
//定义出所有的电梯状态
1700473454
1700473455
public final static OpenningState openningState=new OpenningState();
1700473456
1700473457
public final static ClosingState closeingState=new ClosingState();
1700473458
1700473459
public final static RunningState runningState=new RunningState();
1700473460
1700473461
public final static StoppingState stoppingState=new StoppingState();
1700473462
1700473463
//定义一个当前电梯状态
1700473464
1700473465
private LiftState liftState;
1700473466
1700473467
public LiftState getLiftState(){
1700473468
1700473469
return liftState;
1700473470
1700473471
}
1700473472
1700473473
public void setLiftState(LiftState liftState){
1700473474
1700473475
this.liftState=liftState;
1700473476
1700473477
//把当前的环境通知到各个实现类中
1700473478
1700473479
this.liftState.setContext(this);
1700473480
1700473481
}
1700473482
1700473483
public void open(){
1700473484
1700473485
this.liftState.open();
1700473486
1700473487
}
1700473488
1700473489
public void close(){
1700473490
1700473491
this.liftState.close();
1700473492
1700473493
}
1700473494
1700473495
public void run(){
[
上一页 ]
[ :1.700473446e+09 ]
[
下一页 ]