-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvasCommandInvoker.java
More file actions
47 lines (39 loc) · 2.2 KB
/
CanvasCommandInvoker.java
File metadata and controls
47 lines (39 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.all.sandboxjava.designpatterns.command;
import java.util.List;
public class CanvasCommandInvoker {
private final List<Command> registeredCommands;
private final CommandHistory commandHistory;
public CanvasCommandInvoker(List<Command> allCommands) {
this.registeredCommands = allCommands;
this.commandHistory = new CommandHistory();
}
/* If this function seems complex, here's why.
* I wanted a way for the user to call a command by simply passing a command name and the command arguments as Strings.
* The naive way of doing this would be a huge switch statement that turns command names into the corresponding command objects.
* And since each command needs to be unique, we can either use reflection to create a new command object,
* or we can store every type of command in a list, figure out the desired command, clone that command, and execute that command.
* Reflection would be too complex for this article (which is how this should be implemented), so I went the second route.
* This does mean we cannot send arguments over to the command in the command constructor, but the flexibility this solution offers is worth it.
* */
public void executeCommand(String commandName, String args) {
for(Command command : registeredCommands) {
if(command.getCommandName().toLowerCase().replaceAll(" ", "").equals(commandName.toLowerCase().replaceAll(" ", ""))) {
try {
Command newCommand = (Command) command.clone();
newCommand.setArgs(args);
newCommand.execute();
commandHistory.addCommandToHistory(newCommand);
return;
} catch (Exception e) {
CustomLogger.warn("Invoker", "Couldn't execute command for some reason. Check logs for more details.");
e.printStackTrace();
return;
}
}
}
CustomLogger.warn("CanvasCommandInvoker", "\"" + commandName + "\"" + " command not found.");
}
public void undoLastCommand() {
commandHistory.undoLastCommand();
}
}