encapsulate all command classes from outside the module, through a Factory facade
This commit is contained in:
parent
75956705ad
commit
57bf2d3999
|
@ -1,4 +1,4 @@
|
||||||
# MY SOLUTION
|
# MY SOLUTION
|
||||||
## Current design
|
## Current design
|
||||||
|
|
||||||

|

|
Binary file not shown.
After Width: | Height: | Size: 41 KiB |
Binary file not shown.
Before Width: | Height: | Size: 38 KiB |
|
@ -10,7 +10,7 @@
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>rover-commands</artifactId>
|
<artifactId>rover-commands</artifactId>
|
||||||
<version>1.0</version>
|
<version>2.0</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>17</maven.compiler.source>
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
|
public class CommandFactory {
|
||||||
|
private final RotableRiderRover rover;
|
||||||
|
|
||||||
|
public CommandFactory(RotableRiderRover rover) {
|
||||||
|
this.rover = rover;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RoverCommand createCommand(RoverCommand.Type type) {
|
||||||
|
return switch (type) {
|
||||||
|
case MOVE_FORWARD -> new MoveForwardCommand(rover);
|
||||||
|
case MOVE_BACKWARDS -> new MoveBackwardsCommand(rover);
|
||||||
|
case TURN_LEFT -> new TurnLeftCommand(rover);
|
||||||
|
case TURN_RIGHT -> new TurnRightCommand(rover);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
package cat.hack3.codingtests.marsrover;
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
public class MoveBackwardsCommand implements RoverCommand {
|
class MoveBackwardsCommand implements RoverCommand {
|
||||||
private final RotableRiderRover rover;
|
private final RotableRiderRover rover;
|
||||||
|
|
||||||
public MoveBackwardsCommand(RotableRiderRover rover) {
|
public MoveBackwardsCommand(RotableRiderRover rover) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package cat.hack3.codingtests.marsrover;
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
public class MoveForwardCommand implements RoverCommand {
|
class MoveForwardCommand implements RoverCommand {
|
||||||
private final RotableRiderRover rover;
|
private final RotableRiderRover rover;
|
||||||
|
|
||||||
public MoveForwardCommand(RotableRiderRover rover) {
|
public MoveForwardCommand(RotableRiderRover rover) {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package cat.hack3.codingtests.marsrover;
|
package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
public interface RoverCommand {
|
public interface RoverCommand {
|
||||||
|
enum Type {MOVE_FORWARD, MOVE_BACKWARDS, TURN_LEFT, TURN_RIGHT}
|
||||||
|
|
||||||
void execute();
|
void execute();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.LEFT;
|
import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.LEFT;
|
||||||
|
|
||||||
public class TurnLeftCommand implements RoverCommand {
|
class TurnLeftCommand implements RoverCommand {
|
||||||
private final RotableRiderRover rover;
|
private final RotableRiderRover rover;
|
||||||
|
|
||||||
public TurnLeftCommand(RotableRiderRover rover) {
|
public TurnLeftCommand(RotableRiderRover rover) {
|
||||||
|
|
|
@ -2,7 +2,7 @@ package cat.hack3.codingtests.marsrover;
|
||||||
|
|
||||||
import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.RIGHT;
|
import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.RIGHT;
|
||||||
|
|
||||||
public class TurnRightCommand implements RoverCommand {
|
class TurnRightCommand implements RoverCommand {
|
||||||
private final RotableRiderRover rover;
|
private final RotableRiderRover rover;
|
||||||
|
|
||||||
public TurnRightCommand(RotableRiderRover rover) {
|
public TurnRightCommand(RotableRiderRover rover) {
|
||||||
|
|
|
@ -8,12 +8,14 @@ import org.testng.annotations.Test;
|
||||||
|
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
import static cat.hack3.codingtests.marsrover.RoverCommand.Type.*;
|
||||||
import static cat.hack3.codingtests.marsrover.cartography.Direction.*;
|
import static cat.hack3.codingtests.marsrover.cartography.Direction.*;
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
public class MarsRoverTest {
|
public class MarsRoverTest {
|
||||||
|
|
||||||
private MarsRover rover;
|
private MarsRover rover;
|
||||||
|
|
||||||
private RoverCommand moveForwardCommand;
|
private RoverCommand moveForwardCommand;
|
||||||
private RoverCommand moveBackwardsCommand;
|
private RoverCommand moveBackwardsCommand;
|
||||||
private RoverCommand turnLeftCommand;
|
private RoverCommand turnLeftCommand;
|
||||||
|
@ -29,10 +31,11 @@ public class MarsRoverTest {
|
||||||
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates);
|
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates);
|
||||||
rover = new MarsRover(marsMap, SOUTH);
|
rover = new MarsRover(marsMap, SOUTH);
|
||||||
|
|
||||||
moveForwardCommand = new MoveForwardCommand(rover);
|
var commandFactory = new CommandFactory(rover);
|
||||||
moveBackwardsCommand = new MoveBackwardsCommand(rover);
|
moveForwardCommand = commandFactory.createCommand(MOVE_FORWARD);
|
||||||
turnLeftCommand = new TurnLeftCommand(rover);
|
moveBackwardsCommand = commandFactory.createCommand(MOVE_BACKWARDS);
|
||||||
turnRightCommand = new TurnRightCommand(rover);
|
turnLeftCommand = commandFactory.createCommand(TURN_LEFT);
|
||||||
|
turnRightCommand = commandFactory.createCommand(TURN_RIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -8,6 +8,7 @@ import org.testng.annotations.Test;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static cat.hack3.codingtests.marsrover.RoverCommand.Type.*;
|
||||||
import static cat.hack3.codingtests.marsrover.cartography.Direction.SOUTH;
|
import static cat.hack3.codingtests.marsrover.cartography.Direction.SOUTH;
|
||||||
import static org.testng.Assert.assertEquals;
|
import static org.testng.Assert.assertEquals;
|
||||||
import static org.testng.Assert.assertNotEquals;
|
import static org.testng.Assert.assertNotEquals;
|
||||||
|
@ -15,9 +16,10 @@ import static org.testng.Assert.assertNotEquals;
|
||||||
public class MarsRoverWithObstaclesTest {
|
public class MarsRoverWithObstaclesTest {
|
||||||
|
|
||||||
private MarsRover rover;
|
private MarsRover rover;
|
||||||
private MoveForwardCommand moveForwardCommand;
|
|
||||||
private TurnLeftCommand turnLeftCommand;
|
private RoverCommand moveForwardCommand;
|
||||||
private TurnRightCommand turnRightCommand;
|
private RoverCommand turnLeftCommand;
|
||||||
|
private RoverCommand turnRightCommand;
|
||||||
|
|
||||||
@BeforeMethod
|
@BeforeMethod
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
|
@ -36,9 +38,10 @@ public class MarsRoverWithObstaclesTest {
|
||||||
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates, obstaclesLocalizations);
|
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates, obstaclesLocalizations);
|
||||||
rover = new MarsRover(marsMap, SOUTH);
|
rover = new MarsRover(marsMap, SOUTH);
|
||||||
|
|
||||||
moveForwardCommand = new MoveForwardCommand(rover);
|
var commandFactory = new CommandFactory(rover);
|
||||||
turnLeftCommand = new TurnLeftCommand(rover);
|
moveForwardCommand = commandFactory.createCommand(MOVE_FORWARD);
|
||||||
turnRightCommand = new TurnRightCommand(rover);
|
turnLeftCommand = commandFactory.createCommand(TURN_LEFT);
|
||||||
|
turnRightCommand = commandFactory.createCommand(TURN_RIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cat.hack3.codingtests</groupId>
|
<groupId>cat.hack3.codingtests</groupId>
|
||||||
<artifactId>rover-commands</artifactId>
|
<artifactId>rover-commands</artifactId>
|
||||||
<version>1.0</version>
|
<version>2.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,19 @@
|
||||||
package cat.hack3.codingtests.marsrover.ui.console;
|
package cat.hack3.codingtests.marsrover.ui.console;
|
||||||
|
|
||||||
import cat.hack3.codingtests.marsrover.*;
|
import cat.hack3.codingtests.marsrover.CommandFactory;
|
||||||
|
import cat.hack3.codingtests.marsrover.RotableRiderRover;
|
||||||
|
import cat.hack3.codingtests.marsrover.RoverCommand;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
import static cat.hack3.codingtests.marsrover.RoverCommand.Type.*;
|
||||||
import static cat.hack3.codingtests.marsrover.ui.console.UICommons.isNotExitSignal;
|
import static cat.hack3.codingtests.marsrover.ui.console.UICommons.isNotExitSignal;
|
||||||
import static cat.hack3.codingtests.marsrover.ui.console.UICommons.output;
|
import static cat.hack3.codingtests.marsrover.ui.console.UICommons.output;
|
||||||
|
|
||||||
public class RoverCommandsPerformer {
|
public class RoverCommandsPerformer {
|
||||||
private final Scanner reader;
|
private final Scanner reader;
|
||||||
|
|
||||||
|
CommandFactory commandFactory;
|
||||||
private final RoverCommand moveForwardCommand;
|
private final RoverCommand moveForwardCommand;
|
||||||
private final RoverCommand moveBackwardsCommand;
|
private final RoverCommand moveBackwardsCommand;
|
||||||
private final RoverCommand turnLeftCommand;
|
private final RoverCommand turnLeftCommand;
|
||||||
|
@ -16,10 +21,11 @@ public class RoverCommandsPerformer {
|
||||||
|
|
||||||
public RoverCommandsPerformer(Scanner reader, RotableRiderRover rover) {
|
public RoverCommandsPerformer(Scanner reader, RotableRiderRover rover) {
|
||||||
this.reader = reader;
|
this.reader = reader;
|
||||||
moveForwardCommand = new MoveForwardCommand(rover);
|
commandFactory = new CommandFactory(rover);
|
||||||
moveBackwardsCommand = new MoveBackwardsCommand(rover);
|
moveForwardCommand = commandFactory.createCommand(MOVE_FORWARD);
|
||||||
turnLeftCommand = new TurnLeftCommand(rover);
|
moveBackwardsCommand = commandFactory.createCommand(MOVE_BACKWARDS);
|
||||||
turnRightCommand = new TurnRightCommand(rover);
|
turnLeftCommand = commandFactory.createCommand(TURN_LEFT);
|
||||||
|
turnRightCommand = commandFactory.createCommand(TURN_RIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void acceptCommandsUntilExitSignal() {
|
public void acceptCommandsUntilExitSignal() {
|
||||||
|
|
Loading…
Reference in New Issue