enumを使う(Java)
『Javaで学ぶリファクタリング入門』p225〜p227 のコードを enum を使って書き直してみた。
Robot.java
import java.util.*;
public class Robot {
private final String _name;
private final Position _position = new Position( 0, 0 );
private final Direction _direction = new Direction( 0, 1 );
public Robot (String name) {
_name = name;
}
/**
* @param
* String commandSequence -- 例 "forward right forward left"
*/
public void execute (String commandSequence) {
StringTokenizer tokenizer = new StringTokenizer( commandSequence );
try {
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
executeCommand( token );
}
} catch (InvalidCommandException e) {
System.out.println("不正なコマンドです command: " + e.getMessage());
}
}
/**
* @param
* String commandString -- 例 "forward", "right" ...
*/
public void executeCommand (String commandString) throws InvalidCommandException {
Command command = Command.parseCommand( commandString );
System.out.println("command:" + command);
// System.out.println("Robot: _name: " + this._name );
// System.out.println("Robot: _position: " + this._position._x + " " + this._position._y );
// System.out.println("Robot: _direction: " + this._direction._x + " " + this._direction._y );
// this -- Robot robot
command.execute( this );
}
public void forward () {
_position.relativeMove( _direction._x, _direction._y );
}
public void backward () {
_position.relativeMove( -_direction._x, -_direction._y );
}
public void right () {
_position.relativeMove( _direction._y, -_direction._x );
}
public void left () {
_position.relativeMove( -_direction._y, _direction._x );
}
public String toString() {
return "[ Robot: " + _name + " " +
"position(" + _position._x + ", " + _position._y + "), " +
"direction(" + _direction._x + ", " + _direction._y + ")"+ "]";
}
}
Command.java
import java.util.HashMap;
public enum Command implements CommandExecute {
FORWARD ("forward"){
@Override public void execute (Robot robot) { robot.forward(); }
},
BACKWARD ("backward"){
@Override public void execute (Robot robot) { robot.backward(); }
},
TURN_RIGHT ("right"){
@Override public void execute (Robot robot) { robot.right(); }
},
TURN_LEFT ("left"){
@Override public void execute (Robot robot) { robot.left(); }
};
private String name;
private Command (String name) { this.name = name; }
public String getName() { return this.name; }
private static final HashMap<String, Command> _commandNameMap = new HashMap <> ();
static {
_commandNameMap.put( Command.FORWARD.getName(), FORWARD );
_commandNameMap.put( Command.BACKWARD.getName(), BACKWARD );
_commandNameMap.put( Command.TURN_RIGHT.getName(), TURN_RIGHT );
_commandNameMap.put( Command.TURN_LEFT.getName(), TURN_LEFT );
}
public static Command parseCommand (String name) throws InvalidCommandException {
_commandNameMap.forEach( (str, command) -> {
// System.out.println("str:" + str + " Command:" + command.name());
});
if (! _commandNameMap.containsKey( name )) {
throw new InvalidCommandException( name );
}
return _commandNameMap.get( name );
}
}
Command.java
public interface CommandExecute {
public void execute( Robot robot );
}
InvalidCommandException.java
public class InvalidCommandException extends Exception {
public InvalidCommandException( String name ) {
super( name );
}
public InvalidCommandException () {}
}
Direction.java
public class Direction {
public int _x;
public int _y;
public Direction (int x, int y) {
_x = x;
_y = y;
}
public void setDirection (int x, int y) {
_x = x;
_y = y;
}
}
Position.java
public class Position {
public int _x;
public int _y;
public Position (int x, int y) {
_x = x;
_y = y;
}
public void relativeMove (int dx, int dy) {
_x += dx;
_y += dy;
}
}
Main.java
public class Main {
public static void main (String[] args) {
Robot robot = new Robot( "Andrew" );
System.out.println( robot.toString() );
robot.execute( "forward right forward" );
System.out.println( robot.toString() );
robot.execute( "left backward left forward" );
System.out.println( robot.toString() );
robot.execute( "right backward forward farward" );
System.out.println( robot.toString() );
}
}
カテゴリー: Java, memo
タグ: enum
カウント: 267