LTE Mars Rover - Refactor
public class Rover | |
{ | |
public Direction Orientation {get; set;} | |
public Coordinate Location {get; set;} | |
public Rover() | |
{ | |
Orientation = Direction.North; | |
Location = new Coordinate {X=0, Y=0}; | |
} | |
public void MoveForward() | |
{ | |
if (Orienation == Direction.North) { | |
Location = Location.AdjustYBy(1); | |
} | |
if (Orientation == Direction.South) { | |
Location = Location.AdjustYBy(-1); | |
} | |
if (Orientation == Direction.East) { | |
Location = Location.AdjustXBy(1); | |
} | |
if (Orientation == Direction.West) { | |
Location = Location.AdustXBy(-1); | |
} | |
} | |
public void MoveBackward() | |
{ | |
if (Orienation == Direction.North) { | |
Location = Location.AdjustYBy(-1); | |
} | |
if (Orientation == Direction.South) { | |
Location = Location.AdjustYBy(1); | |
} | |
if (Orientation == Direction.East) { | |
Location = Location.AdjustXBy(-1); | |
} | |
if (Orientation == Direction.West) { | |
Location = Location.AdjustXBy(1); | |
} | |
} | |
public void TurnLeft() | |
{ | |
if (Orienation == Direction.North) { | |
Orienation = Direction.West; | |
} | |
else if (Orientation == Direction.West) { | |
Orientation = Direction.South; | |
} | |
else if (Orientation == Direction.South) { | |
Orientation = Direction.East; | |
} | |
else if (Orientation == Direction.East) { | |
Orienation = Direction.North; | |
} | |
} | |
} |
private void Execute(Action ifNorth, Action ifSouth, Action ifEast, Action ifWest) | |
{ | |
if (Orientation == Direction.North) { | |
ifNorth(); | |
} | |
else if (Orientation == Direction.South) { | |
ifSouth(); | |
} | |
else if (Orientation == Direction.East) { | |
ifEast(); | |
} | |
else if (Orientation == Direction.West) { | |
ifWest(); | |
} | |
} | |
public void TurnLeft() | |
{ | |
Action ifNorth = () => Orienation = Direction.West; | |
Action ifWest = () => Orientation = Direction.South; | |
Action ifSouth = () => Orientation = Direction.East; | |
Action ifEast = () => Orientation = Direction.North; | |
Execute(ifNorth, ifSouth, ifEast, ifWest); | |
} | |
public void MoveForward() | |
{ | |
Action ifNorth = () => Location=Location.AdjustYBy(1); | |
Action ifSouth = () => Location=Location.AdjustYBy(-1); | |
Action ifEast = () => Location=Location.AdjustXBy(1); | |
Action ifWest = () => Location=Location.AdjustXBy(-1); | |
Execute(ifNorth, ifSouth, ifEast, ifWest); | |
} |
internal IMovementStrategy() | |
{ | |
Coordinate MoveForward(Coordinate coordinate); | |
Coordinate MoveBackward(Coordinate coordinate); | |
Direction TurnLeft(); | |
} | |
internal class NorthMovementStrategy : IMovementStrategy | |
{ | |
public Coordinate MoveForward(Coordinate coordinate) | |
{ | |
return coordinate.AdjustYBy(1); | |
} | |
public Coordinate MoveBackward(Coordinate coordinate) | |
{ | |
return coordinate.AdjustYBy(-1); | |
} | |
public Direction TurnLeft() | |
{ | |
return Direction.West; | |
} | |
} | |
private IMovementStrategy GetStrategy() | |
{ | |
if (Orientation == Direction.North) { | |
return new NorthMovementStrategy(); | |
} | |
else if (Orientation == Direction.South) { | |
return new SouthMovementStrategy(); | |
} | |
else if (Orientation == Direction.East) { | |
return new EastMovementStrategy(); | |
} | |
else if (Orientation == Direction.West) { | |
return new WestMovementStrategy(); | |
} | |
} | |
public void TurnLeft() | |
{ | |
var movementStrategy = GetMovementStrategy(); | |
Orientation = movementStrategy.TurnLeft(); | |
} | |
public void MoveForward() | |
{ | |
var movementStrategy = GetMovementStrategy(); | |
Location = movementStrategy.MoveForward(Location); | |
} |
private xxx Execute(xxxx) | |
{ | |
if (Orientation == Direction.North) { | |
// | |
} | |
else if (Orientation == Direction.South) { | |
// | |
} | |
else if (Orientation == Direction.East) { | |
// | |
} | |
else if (Orientation == Direction.West) { | |
// | |
} | |
} |
private void Execute(Action ifNorth, Action ifSouth, Action ifEast, Action ifWest) | |
{ | |
if (Orientation == Direction.North) { | |
ifNorth(); | |
} | |
else if (Orientation == Direction.South) { | |
ifSouth(); | |
} | |
else if (Orientation == Direction.East) { | |
ifEast(); | |
} | |
else if (Orientation == Direction.West) { | |
ifWest(); | |
} | |
} | |
public void TurnLeft() | |
{ | |
Action ifNorth = () => Orienation = Direction.West; | |
Action ifWest = () => Orientation = Direction.South; | |
Action ifSouth = () => Orientation = Direction.East; | |
Action ifEast = () => Orientation = Direction.North; | |
Execute(ifNorth, ifSouth, ifEast, ifWest); | |
} | |
public void MoveForward() | |
{ | |
Action ifNorth = () => Location=Location.AdjustYBy(1); | |
Action ifSouth = () => Location=Location.AdjustYBy(-1); | |
Action ifEast = () => Location=Location.AdjustXBy(1); | |
Action ifWest = () => Location=Location.AdjustXBy(-1); | |
Execute(ifNorth, ifSouth, ifEast, ifWest); | |
} | |
public void MoveBackward() | |
{ | |
Action ifNorth = () => Location=Location.AdjustYBy(-1); | |
Action ifSouth = () => Location=Location.AdjustYBy(1); | |
Action ifEast = () => Location=Location.AdjustXBy(-1); | |
Action ifWest = () => Location=Location.AdjustXBy(1); | |
Execute(ifNorth, ifSouth, ifEast, ifWest); | |
} | |
public void Execute(Action ifNorth, Action ifSouth, Action ifEast, Action ifWest) | |
{ | |
switch(Orienation) | |
{ | |
case Direction.North: ifNorth(); break; | |
case Direction.South: ifSouth(); break; | |
case Direction.East: ifEast(); break; | |
case Direction.West: ifWest(); break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment