如何在聊天程序中动态处理命令?

时间:2017-03-12 17:19:38

标签: java server command client chat

我的问题更多的是设计问题而不是其他问题。我目前正在用Java开发经典的Server-Client聊天程序。一切都很好,直到我得到命令。我认为用户可以方便地发送命令,然后由服务器处理这些命令以更改其昵称。问题是我想制作灵活的代码,最重要的是,面向对象的代码。为了避免无休止的if / else if语句知道输入了什么命令,我认为最好为从超类Command继承的每个命令创建一个类。然后我可以通过所有子类中的getCommand()函数重写返回特定命令。但它根本无法解决我的问题。服务器仍然需要使用instanceof测试返回了什么命令。动态执行此操作的一种方法是从超类Command中自动向下转换它,然后在服务器类中调用相应的函数。例如:

public void processCommand(CommandNick c) {}
public void processCommand(CommandKick c) {}

但是我还没有找到任何正确的方法,即使我这样做了,我觉得这里仍然存在设计问题。而且我确信有一种很好的灵活方式可以做到这一点但是时间不足以让我弄明白。有任何想法吗?提前致谢! :)

2 个答案:

答案 0 :(得分:1)

我假设您的服务器将消息作为带有发件人和字符串的对象接收。创建您的Command类,并在服务器初始化代码中创建一个HashMap<String, AbstractCommand>,其中String为关键,AbstractCommand类为值。您的命令应该扩展此类。注册所有命令,如下所示:

commandRegistry.put("help", new HelpCommandHandler());

我假设命令是前面带有!的消息。因此,当您收到消息时,请检查它是否是命令:

Message message = (Your Message)
String messageBody = message.getBody();
Sender messageSender = message.getSender();

if(messageBody.startsWith("!")) {
    // Split the message after every space
    String[] commandParts = messageBody.split(" ");
    // The first element is the command base, like: !help
    String baseCommand = commandParts[0];
    // Remove the first character from the base, turns !help into help
    baseCommand = baseCommand.substring(1, baseCommand.length());
    // Creates a new array for the arguments. The length is smaller, because we won't copy the command base
    String[] args = new String[commandParts.length - 1];
    // Copy the elements of the commandParts array from index 1 into args from index 0
    if(args.length > 0) {
        System.arraycopy(commandParts, 1, args, 0, commandParts.length - 1);
    }
    // Your parse method
    processCommand(sender, baseCommand, args);
}

public void processCommand(Sender sender, String base, String[] args) {
    if(commandRegistry.containsKey(base)) {
        commandRegistry.get(base).execute(sender, args);
    } else {
        // Handle unknown command
    }
}

public abstract class AbstractCommand {
    public abstract void execute(Sender sender, String[] args);
}

示例实施。我假设你的服务器是一个Singleton,你可以使用Server.get()或任何类似的方法来获取它的对象。

public class HelpCommandHandler extends AbstractCommand { /* !help */
    @Override
    public void execute(Sender sender, String[] args) {
        sender.sendMessage("You asked for help."); // Your code might not work like this.
    }
}

public class ChangeNickCommandHandler extends AbstractCommand { /* !changenick newNick */
    @Override
    public void execute(Sender sender, String[] args) {
        // I assume you have a List with connected players in your Server class
        String username = sender.getUsername(); // Your code might not work like this
        Server server = Server.get(); // Get Server instance
        server.getUsers().get(username).setNickname(args[0]); // Argument 0. Check if it even exists.
    }
}

// Server class. If it isn't singleton, you can make it one like this:
public class Server {
    private static Server self;
    public static Server init(/* Your args you'd use in a constructor */) { self = new Server(); return get(); }
    public static Server get() { return self; }

    private List<User> users = new List<User>();
    private HashMap<String, AbstractCommand> commandRegitry = new HashMap<>();

    // Make construcor private, use init() instead.
    private Server() {
        commandRegistry.put("help", new HelpCommandHandler());
        commandRegistry.put("changenick", new ChangeNickCommandHandler());
    }

    // Getters
    public List<User> getUsers() {
        return users;
    }

    public HashMap<String, AbstractCommand> getRegistry() {
        return commandRegistry;
    }
}

答案 1 :(得分:-1)

这是一些伪代码,用于说明您的控制器不需要了解命令处理器(不需要实例)。

abstract class CommandProcessor {
    /* return boolean if this Command processed the request */  
    public static boolean processCommand(String command, User user, Properties chatProperties, Chat chat);
}

/* Handle anything */
public class CommandRemainder extends CommandProcessor {
    @Override
    public static boolean processCommand(String command, User user, Properties chatProperties, Chat chat) {
        chat.appendText("[" + user.getName() + "] " + command);
        return true;
    }
}

/* Handle color changing */
public class CommandColorizer extends CommandProcessor {
    protected static List<String> ALLOWED_COLORS = new ArrayList<>(Arrays.asList("red", "blue", "green"));
    @Override
    public static boolean processCommand(String command, User user, Properties chatProperties, Chat chat) {
        if ("fg:".equals(command.trim().substring(0,3)) {
            String color = command.trim().substring(3).trim();
            if (ALLOWED_COLORS.contains(color)) {
                chat.setForeground(color);
            }
            return true;
        }
        return false;
    }
}

public class ChatController {
    protected Chat chat = new Chat();
    protected User user = getUser();
    protected Properties chatProperties = getChatProperties();
    protected List<CommandProcessor> commandProcessors = getCommandProcessors();

    {
        chat.addChatListener(new ChatListener(){
            @Override
            public void userChatted(String userChatString) {
                for (CommandProcessor processor : commandProcessors) {
                    if (processor.processCommand(userChatString, user, chatProperties, chat)) {
                        break;
                    }
                }
            }
        });
    }
    List<CommandProcessor> getCommandProcessors() {
        List<CommandProcessor> commandProcessors = new ArrayList<>();

        commandProcessors.add(new CommandColorizer());
        commandProcessors.add(new CommandRemainder()); // needs to be last

        return commandProcessors;
    }
}