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(){
1700473496
1700473497
this.liftState.run();
1700473498
1700473499
}
1700473500
1700473501
public void stop(){
1700473502
1700473503
this.liftState.stop();
1700473504
1700473505
}
1700473506
1700473507
}
1700473508
1700473509
结合以上3个类,我们可以这样理解:Context是一个环境角色,它的作用是串联各个状态的过渡,在LiftSate抽象类中我们定义并把这个环境角色聚合进来,并传递到子类,也就是4个具体的实现类中自己根据环境来决定如何进行状态的过渡。关闭状态如代码清单26-10所示。
1700473510
1700473511
代码清单26-10 关闭状态
1700473512
1700473513
public class ClosingState extends LiftState{
1700473514
1700473515
//电梯门关闭,这是关闭状态要实现的动作
1700473516
1700473517
@Override
1700473518
1700473519
public void close(){
1700473520
1700473521
System.out.println(“电梯门关闭……”);
1700473522
[
上一页 ]
[ :1.700473473e+09 ]
[
下一页 ]