【问题标题】:Client-server app command pattern客户端-服务器应用程序命令模式
【发布时间】:2017-12-26 20:46:58
【问题描述】:

我正在编写一个客户端-服务器应用程序,但在使用命令模式时遇到了问题。在我的程序中,服务器从客户端接收字符串输入命令,为 HashMap 中的输入找到正确的命令,执行它并发回返回值。我在弄清楚如何编写需要超过 1 个步骤的命令时遇到问题(命令必须向客户端询问额外的参数,然后应该返回最终结果)。

命令界面

public interface Command {
    public String execute();    
}

服务器与客户端通信

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

一步到位命令没问题

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

我不知道如何写一个命令,需要一个额外的参数来执行,它不能在不知道的情况下立即返回结果。命令应返回 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;
    }

}

我有一个想法让命令无效而不是字符串,但是我将无法通过 clientSocketWriter 与客户端发送通信。有没有什么好办法让命令的步骤更多?

【问题讨论】:

  • 命令模式的全部意义在于封装执行的逻辑。您要做的是将该逻辑公开给服务器。

标签: java client-server command-pattern


【解决方案1】:

我认为你在正确的轨道上。为什么不允许命令标识符及其参数在一行中传递?我正在考虑类似的事情:

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

通过允许使用参数指定的命令,您的 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);
    }
}

现在您可以创建继承自抽象 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();
}

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多