机器人传动装置(采用状态模式),主要是补方法体中的代码,Robot中也要改动,自己看着办,这里可能没人能够完成它!!!

SunChen 发布于 2011/12/21 12:12
阅读 769
收藏 0

package 上机实验一;

public class Client {

 /**
  * 机器人的状态模式参考ppt中状态模式的图,当前的设计对图的理解是:
  * 1、实例化机器人,当前状态是NullState空档状态;
  * 2、设置机器人速度为0.0,开始启动,状态没有改变;
  * 3、设置速度为1.0,进入MovementState状态,要执行进入该状态时要执行DoEntry,do两个行为;
  * 3、改变机器人方向,这时委托MovementState状态对象改变方向;
  * 4、继续改变机器人速度和方向,前进和后退的状态就会就像转换;
  * 5、如果处于前进状态,执行upshift或者downshift就会在一档和二档之间切换。
  * 按照以上理解完成本案例所有代码(有可能现有代码也需要更改,请留意):
  * 1、Robot
  * 2、各个状态类的实现
  * 3、方法的具体实现
  * 请分析本状态模式的实现有没有更好的方式,比如状态的超类是否一定要定义为接口呢
  */
 public static void main(String[] args) {
  //机器人总共有哪些状态呢,总共有哪些行为(遥控按钮)呢
  
  //新建一个处于静止状态的历史方向向前的机器人对象
  Robot robot =  new Robot();
  
  /*启动机器人,在start方法中修改机器人的状态,根据方向条件,如果为f,则进入foward的firstgear状态,
   * 进入该状态时要doEntry,do方法
   */
  robot.setSpeed(0.0);
  robot.start();
  robot.setSpeed(1.0);
  robot.changeDir(Robot.ForwardDirection);
  robot.setSpeed(0.0);
  robot.changeDir(Robot.ForwardDirection);
  robot.setSpeed(2.0);
  robot.Upshift();
  robot.Upshift();
  robot.DownShift();
  robot.DownShift();  
  robot.setSpeed(0.0);
  robot.changeDir(Robot.BackwardDirection);
  robot.setSpeed(1.0);
 }

}
package 上机实验一;

public class FirstGearState implements IRobotState {

 @Override
 public void ChangeDir(String direction) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void DownShift() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void OnStateEvent() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void Upshift() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void doState() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onStateEntry() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onStateExit() {
  // TODO Auto-generated method stub
  
 }

}
package 上机实验一;

public interface IRobotState {
 public void onStateEntry();
 public void onStateExit();
 public void doState();
 public void OnStateEvent();
 public void Upshift();
 public void DownShift();
 public void ChangeDir(String direction);
}
package 上机实验一;

public class MovementState implements IRobotState {

 @Override
 public void ChangeDir(String direction) {
  // TODO Auto-generated method stub

 }

 @Override
 public void DownShift() {
  // TODO Auto-generated method stub

 }

 @Override
 public void OnStateEvent() {
  // TODO Auto-generated method stub

 }

 @Override
 public void Upshift() {
  // TODO Auto-generated method stub

 }

 @Override
 public void doState() {
  // TODO Auto-generated method stub

 }

 @Override
 public void onStateEntry() {
  // TODO Auto-generated method stub

 }

 @Override
 public void onStateExit() {
  // TODO Auto-generated method stub

 }

}
package 上机实验一;

public class NullState implements IRobotState {

 @Override
 public void ChangeDir(String direction) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void DownShift() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void OnStateEvent() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void Upshift() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void doState() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onStateEntry() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onStateExit() {
  // TODO Auto-generated method stub
  
 }

}
package 上机实验一;

public class ReverseState implements IRobotState {

 @Override
 public void ChangeDir(String direction) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void DownShift() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void OnStateEvent() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void Upshift() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void doState() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onStateEntry() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onStateExit() {
  // TODO Auto-generated method stub
  
 }

}
package 上机实验一;

public class Robot {
 public static String ForwardDirection = "F";
 public static String BackwardDirection = "B";

 private static IRobotState firstGearState = new FirstGearState();
 private static IRobotState secondGearState = new SecondGearState();
 private static IRobotState reverseState = new ReverseState();
 private static IRobotState movementState = new MovementState();
 
 private  IRobotState currentState = new NullState(); 
 private double speed = 0.0;
 private String direction = ForwardDirection;
 


 public Robot() {
  this.speed = 0.0;
  this.direction = ForwardDirection;
  // TODO Auto-generated constructor stub
 }

 public void start() {
  // TODO Auto-generated method stub
  
 } 

 public void changeDir(String direction) { 
  currentState.ChangeDir(direction);
  // TODO Auto-generated method stub
  /*if (speed>0) {
   soundAlarm();
  }
  if (speed==0) {
   this.direction = direction;
   reverseMotor();
   if (this.direction.equalsIgnoreCase(Robot.ForwardDirection)){    
    this.setCurrentState(firstGearState);   
   }else {
    this.setCurrentState(reverseState); 
   }
  }  */
 } 

 public void Upshift(){  
  currentState.Upshift();
 }
 public void DownShift(){
  currentState.DownShift();
 }
 

 public IRobotState getCurrentState() {
  return currentState;
 }

 public void setCurrentState(IRobotState currentState) {
  this.currentState.onStateExit();
  this.currentState = currentState;
  this.currentState.onStateEntry();
  this.currentState.doState();
 }
 private void reverseMotor() {
  // TODO Auto-generated method stub
  System.out.println("robot has reverseMotored!");
 }
 private void soundAlarm() {
  // TODO Auto-generated method stub
  
 }
 private void turnOnRobot() {
  // TODO Auto-generated method stub
  
 }

 private void blinkLights() {
  // TODO Auto-generated method stub
  
 }

 public double getSpeed() {
  return speed;
 }

 public void setSpeed(double speed) {
  this.speed = speed;
  if (speed>0){
   setCurrentState(movementState);
  }  
 }
}
package 上机实验一;

public class SecondGearState implements IRobotState {

 @Override
 public void ChangeDir(String direction) {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void DownShift() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void OnStateEvent() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void Upshift() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void doState() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onStateEntry() {
  // TODO Auto-generated method stub
  
 }

 @Override
 public void onStateExit() {
  // TODO Auto-generated method stub
  
 }

}
将代码补全,属于单纯的状态模式,对你来说肯定是小KISS

加载中
返回顶部
顶部