Class CommandFactory

java.lang.Object
za.co.wethinkcode.robots.server.commands.CommandFactory

public final class CommandFactory extends Object
Factory responsible for constructing concrete Command instances from a parsed CommandRequest.

This class acts as the bridge between the transport layer and the command layer. Incoming client requests are first deserialized into a CommandRequest DTO, after which the factory determines the appropriate concrete Command implementation to instantiate.

By centralising command creation, individual command implementations remain focused solely on business logic and are no longer responsible for parsing JSON or interpreting request payloads.

To add support for a new command:

  1. Create a new Command implementation.
  2. Add a corresponding case to the factory's create() method.
No changes are required to the parsing logic.

  • Constructor Details

    • CommandFactory

      public CommandFactory(ClientID clientId)
      Creates a command factory associated with a specific client connection.
      Parameters:
      clientId - the identifier of the client issuing commands
  • Method Details

    • create

      public static Command create(CommandRequest request)
      Creates the appropriate concrete Command from a parsed client request.

      The factory validates and converts request arguments where necessary before constructing the requested command.

      Parameters:
      request - the parsed client request
      Returns:
      the corresponding concrete Command
      Throws:
      IllegalArgumentException - if the command is unknown or the supplied arguments are invalid