1700473410
1700473411
//动作委托为CloseState来执行
1700473412
1700473413
super.context.getLiftState().close();
1700473414
1700473415
}
1700473416
1700473417
//打开电梯门
1700473418
1700473419
@Override
1700473420
1700473421
public void open(){
1700473422
1700473423
System.out.println(“电梯门开启……”);
1700473424
1700473425
}
1700473426
1700473427
//门开着时电梯就运行跑,这电梯,吓死你!
1700473428
1700473429
@Override
1700473430
1700473431
public void run(){
1700473432
1700473433
//do nothing;
1700473434
1700473435
}
1700473436
1700473437
//开门还不停止?
1700473438
1700473439
public void stop(){
1700473440
1700473441
//do nothing;
1700473442
1700473443
}
1700473444
1700473445
}
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();
[
上一页 ]
[ :1.70047341e+09 ]
[
下一页 ]