简体   繁体   English

客户端-服务器应用程序命令模式

[英]Client-server app command pattern

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. 在我的程序服务器中,从客户端接收String输入命令,在HashMap中为输入找到合适的命令,执行该命令并发回返回值。 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). 我在弄清楚如何编写需要超过1个步骤的命令时遇到了问题(命令必须询问客户额外的参数,然后应返回最终结果)。

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). 命令应返回x个元素的排列数量(客户端应选择)。

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. 我有一个使Command代替String无效的想法,但是那样我将无法通过clientSocketWriter发送与客户端的通讯。 Is there any good way to make commands with more steps? 有什么好方法可以执行更多步骤的命令?

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: 通过允许使用参数指定的命令,您的CommandFactory变为:

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: 现在,您可以创建从抽象的Command类继承的特定命令实现,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM