0
votes

I'm writing a client-server app and I'm having a problem with using command pattern. In my program server recieves a String input command from a client, finds proper command for the input in the HashMap, executes it and sends back the return value. I'm having a problem with figuring out, how to write the commands that require more than 1 step(command has to ask ask a client for extra paramether/s and then should return final result).

command interface

public interface Command {
    public String execute();    
}

server communication with a client

 try  {
        ServerSocket serverSocket = new ServerSocket(port);
        Command c;

        while(true) {

            Socket clientSocket = serverSocket.accept();
            PrintWriter clientSocketWriter = new PrintWriter(clientSocket.getOutputStream(), true);

            BufferedReader clientSocketReader = new BufferedReader(new 
                  InputStreamReader(clientSocket.getInputStream()));

            String inputLine;

        //server recieves String command from client and sends back the result while semaphore is true
            while (Main.semaphore) {
                inputLine = clientSocketReader.readLine();
                c = CommandFactory.getCommandByName(inputLine);
                clientSocketWriter.println(c.execute());
            }

            serverSocket.close();
            clientSocketWriter.close();
            clientSocketReader.close();
            clientSocket.close();
        }

    } catch(Exception e) {
        e.printStackTrace();
    }

in one step commands there in no problem

public class CommandHelp implements Command {
    @Override
    public String execute() {
        //returns string of all commands
        return CommandFactory.getCommandByNames();
} 

I don't know how to write a command, that needs an extra paramether for executing, it can't return the result right away without knowing it. Command should return number of permutations from x elements(client should choose).

public class CommandPermutationsCount implements Command {

    @Override
    public String execute() {
      //can't return anything yet
    }


    public long getPermutations(int elementsCount) {
         long result = 1;

         for (int factor = 2; factor <= elementsCount; factor++) {
            result *= factor;
         }
        return result;
    }

}

I've had an idea to make the Command void instead of String, but then I wouldn't be able to send comunicate with a client via clientSocketWriter. Is there any good way to make commands with more steps?

1
The whole point of command pattern is to encapsulate the logic of execution. What you are trying to do is expose that logic to server. - tsolakp

1 Answers

0
votes

I think you're in the right track. Why don't you allow the command identifier and its arguments to be passed in one line? I'm thinking of something like:

>> permute -a 12
>> shuffle -a [1, 2, 7, 6, 5, 3, 1]
>> add -a 4 -a 5

By allowing commands specified with arguments, your CommandFactory becomes something like:

class CommandFactory {
    public static Command parseCommand(String commandLine) {
        // Tokenize commandLine:
        // - First word is commandId
        // - Tokens following '-a' string are arguments
        // Return CommandHelp if no command with provided commandId is found
        return CommandHelp(commandLine);
    }
}

Now you can create specific command implementations inheriting from an abstract Command class that looks like this:

abstract class Command {
    private final String commandId;
    private final String commandArguments;

    Command(String commandId, List<String> commandArguments) {
        this.commandId = commandId;
        this.commandArguments = commandArguments;
    }

    abstract String execute();
}