1
0
Fork 0

encapsulate all command classes from outside the module, through a Factory facade

This commit is contained in:
Xavier Fontanet 2024-06-23 22:29:14 +02:00
parent 75956705ad
commit 57bf2d3999
14 changed files with 54 additions and 22 deletions

View File

@ -1,4 +1,4 @@
# MY SOLUTION
## Current design
![Current design](misc/images/mars-rover-uml.png)
![Current design](misc/images/mars-rover design.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

View File

@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>rover-commands</artifactId>
<version>1.0</version>
<version>2.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>

View File

@ -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);
};
}
}

View File

@ -1,6 +1,6 @@
package cat.hack3.codingtests.marsrover;
public class MoveBackwardsCommand implements RoverCommand {
class MoveBackwardsCommand implements RoverCommand {
private final RotableRiderRover rover;
public MoveBackwardsCommand(RotableRiderRover rover) {

View File

@ -1,6 +1,6 @@
package cat.hack3.codingtests.marsrover;
public class MoveForwardCommand implements RoverCommand {
class MoveForwardCommand implements RoverCommand {
private final RotableRiderRover rover;
public MoveForwardCommand(RotableRiderRover rover) {

View File

@ -1,5 +1,7 @@
package cat.hack3.codingtests.marsrover;
public interface RoverCommand {
enum Type {MOVE_FORWARD, MOVE_BACKWARDS, TURN_LEFT, TURN_RIGHT}
void execute();
}

View File

@ -2,7 +2,7 @@ package cat.hack3.codingtests.marsrover;
import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.LEFT;
public class TurnLeftCommand implements RoverCommand {
class TurnLeftCommand implements RoverCommand {
private final RotableRiderRover rover;
public TurnLeftCommand(RotableRiderRover rover) {

View File

@ -2,7 +2,7 @@ package cat.hack3.codingtests.marsrover;
import static cat.hack3.codingtests.marsrover.RotableRiderRover.Rotation.RIGHT;
public class TurnRightCommand implements RoverCommand {
class TurnRightCommand implements RoverCommand {
private final RotableRiderRover rover;
public TurnRightCommand(RotableRiderRover rover) {

View File

@ -8,12 +8,14 @@ import org.testng.annotations.Test;
import java.util.stream.IntStream;
import static cat.hack3.codingtests.marsrover.RoverCommand.Type.*;
import static cat.hack3.codingtests.marsrover.cartography.Direction.*;
import static org.testng.Assert.assertEquals;
public class MarsRoverTest {
private MarsRover rover;
private RoverCommand moveForwardCommand;
private RoverCommand moveBackwardsCommand;
private RoverCommand turnLeftCommand;
@ -29,10 +31,11 @@ public class MarsRoverTest {
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates);
rover = new MarsRover(marsMap, SOUTH);
moveForwardCommand = new MoveForwardCommand(rover);
moveBackwardsCommand = new MoveBackwardsCommand(rover);
turnLeftCommand = new TurnLeftCommand(rover);
turnRightCommand = new TurnRightCommand(rover);
var commandFactory = new CommandFactory(rover);
moveForwardCommand = commandFactory.createCommand(MOVE_FORWARD);
moveBackwardsCommand = commandFactory.createCommand(MOVE_BACKWARDS);
turnLeftCommand = commandFactory.createCommand(TURN_LEFT);
turnRightCommand = commandFactory.createCommand(TURN_RIGHT);
}
@Test

View File

@ -8,6 +8,7 @@ import org.testng.annotations.Test;
import java.util.List;
import static cat.hack3.codingtests.marsrover.RoverCommand.Type.*;
import static cat.hack3.codingtests.marsrover.cartography.Direction.SOUTH;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
@ -15,9 +16,10 @@ import static org.testng.Assert.assertNotEquals;
public class MarsRoverWithObstaclesTest {
private MarsRover rover;
private MoveForwardCommand moveForwardCommand;
private TurnLeftCommand turnLeftCommand;
private TurnRightCommand turnRightCommand;
private RoverCommand moveForwardCommand;
private RoverCommand turnLeftCommand;
private RoverCommand turnRightCommand;
@BeforeMethod
public void setUp() {
@ -36,9 +38,10 @@ public class MarsRoverWithObstaclesTest {
var marsMap = new MarsMap(mapHeight, mapWidth, startingCoordinates, obstaclesLocalizations);
rover = new MarsRover(marsMap, SOUTH);
moveForwardCommand = new MoveForwardCommand(rover);
turnLeftCommand = new TurnLeftCommand(rover);
turnRightCommand = new TurnRightCommand(rover);
var commandFactory = new CommandFactory(rover);
moveForwardCommand = commandFactory.createCommand(MOVE_FORWARD);
turnLeftCommand = commandFactory.createCommand(TURN_LEFT);
turnRightCommand = commandFactory.createCommand(TURN_RIGHT);
}
@Test

View File

@ -25,7 +25,7 @@
<dependency>
<groupId>cat.hack3.codingtests</groupId>
<artifactId>rover-commands</artifactId>
<version>1.0</version>
<version>2.0</version>
</dependency>
</dependencies>

View File

@ -1,14 +1,19 @@
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 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.output;
public class RoverCommandsPerformer {
private final Scanner reader;
CommandFactory commandFactory;
private final RoverCommand moveForwardCommand;
private final RoverCommand moveBackwardsCommand;
private final RoverCommand turnLeftCommand;
@ -16,10 +21,11 @@ public class RoverCommandsPerformer {
public RoverCommandsPerformer(Scanner reader, RotableRiderRover rover) {
this.reader = reader;
moveForwardCommand = new MoveForwardCommand(rover);
moveBackwardsCommand = new MoveBackwardsCommand(rover);
turnLeftCommand = new TurnLeftCommand(rover);
turnRightCommand = new TurnRightCommand(rover);
commandFactory = new CommandFactory(rover);
moveForwardCommand = commandFactory.createCommand(MOVE_FORWARD);
moveBackwardsCommand = commandFactory.createCommand(MOVE_BACKWARDS);
turnLeftCommand = commandFactory.createCommand(TURN_LEFT);
turnRightCommand = commandFactory.createCommand(TURN_RIGHT);
}
public void acceptCommandsUntilExitSignal() {