找到你要的答案

Q:Design pattern for accepting terminal commands

Q:接收终端命令的设计模式

I am in the process of refactoring a Java application that accepts commands from terminal. Currently, there is a huge if-else structure that checks for the command issued and executes the required method. I was wondering if it was possible to replace this logic with a well established design pattern. I looked at Command pattern and Strategy pattern, but cannot seem to get my head around it in this scenario. Please suggest.

Update: I do use JCommander, but that doesn't get rid of the if-else structure.

    MyCommand cmd1 = new MyCommand();
    ...
    ...
    MyCommandN cmdN = new MyCommandN();
    JCommander cmd = new JCommander();
    cmd.addCommand("myCommand1", cmd1);
    ...
    ...
    cmd.addCommand("myCommandN", cmdN);
    try {
        cmd.parse(args);
        //
        if ("myCommand1".equals(cmd.getParsedCommand())) {
           System.out.println("running command1");
        }
        ...
        ...
        else if ("myCommandN".equals(cmd.getParsedCommand())) {
            // Sysout statements only for illustration. I call command specific methods here.
            System.out.println("running commandN");
        } else {
           cmd.usage();
        }
        //
    } catch (ParameterException ex) {
        System.out.println(ex.getMessage());
        cmd.usage();
    }

I am in the process of refactoring a Java application that accepts commands from terminal. Currently, there is a huge if-else structure that checks for the command issued and executes the required method. I was wondering if it was possible to replace this logic with a well established design pattern. I looked at Command pattern and Strategy pattern, but cannot seem to get my head around it in this scenario. Please suggest.

Update: I do use JCommander, but that doesn't get rid of the if-else structure.

    MyCommand cmd1 = new MyCommand();
    ...
    ...
    MyCommandN cmdN = new MyCommandN();
    JCommander cmd = new JCommander();
    cmd.addCommand("myCommand1", cmd1);
    ...
    ...
    cmd.addCommand("myCommandN", cmdN);
    try {
        cmd.parse(args);
        //
        if ("myCommand1".equals(cmd.getParsedCommand())) {
           System.out.println("running command1");
        }
        ...
        ...
        else if ("myCommandN".equals(cmd.getParsedCommand())) {
            // Sysout statements only for illustration. I call command specific methods here.
            System.out.println("running commandN");
        } else {
           cmd.usage();
        }
        //
    } catch (ParameterException ex) {
        System.out.println(ex.getMessage());
        cmd.usage();
    }
answer1: 回答1:

Introduce a common command interface and use a Map

Map<String, Command> commands = new HashMap<String, Command>();
commands.put("myCommand1", new MyCommand());
commands.put("myCommandN", new MyCommandN());

JCommander cmd = new JCommander();
cmd.addCommand("myCommand1", cmd1);
...
cmd.addCommand("myCommandN", cmdN);
try {
    cmd.parse(args);

    Command command = commands.get(cmd.getParsedCommand());
    if(command == null){
       cmd.usage();
    }
    // execute command
}
 ...

介绍一个通用命令接口,并使用一个地图

Map<String, Command> commands = new HashMap<String, Command>();
commands.put("myCommand1", new MyCommand());
commands.put("myCommandN", new MyCommandN());

JCommander cmd = new JCommander();
cmd.addCommand("myCommand1", cmd1);
...
cmd.addCommand("myCommandN", cmdN);
try {
    cmd.parse(args);

    Command command = commands.get(cmd.getParsedCommand());
    if(command == null){
       cmd.usage();
    }
    // execute command
}
 ...
answer2: 回答2:

Use JCommander: http://jcommander.org/#Overview

There are other alternatives as well. See also: Command Line Parsing: Commons CLI alternatives?

使用jcommander:HTTP:/ / jcommander。org / #概述

还有其他的选择。参见:命令行解析:共享CLI的替代品吗?

java  design-patterns  terminal