ҳ > 򿪷 > > >

״̬ģʽִ֮еװ״̬йص ״ִ̬

2017-02-18

״̬ģʽִ֮еװ״̬йص ״ִ̬Уֹͬ̽ݵĿϵͳ̣۽ýṹķʽдݵĿƳ

״̬ģʽִ֮еװ״̬йص ״ִֹ̬ͬ̽ݵĿϵͳ۽ýṹķʽдݵĿƳ

ϵͳһܸӺӴ󡣺״̬ģʽдһ򵥵ĵݿƳ򣬽õ˼ļ򻯡ұдһ򵥵ĵݵĿƳļܹ

\

״̬ģʽ΢һЩȣǷһݿִеĶУššССֹͣǶһElevatorStateӿڣӿcloseDoor()openDoor()ֵݿִеĶ

ȻǷӦж״̬ŹرաŴ򿪡ССֹͣǶʵElevatorStateӿڵ࣬DoorCloseStateDoorOpenStateMovingUpStateMovingDownStateStopStateֱӦ״̬״̬жһElevator͵ijԱݵࡣ

Elevatorд״̬ijԱElevatorStateӿ͡ElevatorĹ췽Ϊ״̬ijԱӦʵಢֵ£

publicElevator() {

stopState= new StopState(this);

movingUpState= new MovingUpState(this);

movingDownState= new MovingDownState(this);

doorOpenState= new DoorOpenState(this);

doorCloseState= new DoorCloseState(this);

state= stopState;

}

ElevatorлиcallûеݵĶõݵǰ״̬ĹšлСֹͣŵȶ£

publicvoid call(int destFloor) {

if(destFloor != currentFloor) {

state.closeDoor();

if(destFloor > currentFloor) {

state.moveUp(destFloor);

}else if (destFloor < currentFloor) {

state.moveDown(destFloor);

}

state.stop();

state.openDoor();

}else {

System.out.println("Elevatoris at " + destFloor + " floor now.");

}

}

ԿԵִܷеǰִе߼װڵݵ״̬У˶߼жҪ򵥵Ķ࣬ÿǶ״̬µֻǵǰ״̬δǿһµ״̬ʱδŶ

publicvoidopenDoor() {

System.out.println("Can&#39;topen the door during move.");

}

ɼʱҪκ߼жϣֱӸûڵƶвܿš

״̬ģʽԶִеĶ״̬أ״̬кܶʱʹ״̬ģʽ򻯶߼жϣʹô׻ͳ

ȵƼ