Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Given a command string in which the first 8 characters are the command name
- padded on the right with whitespace, construct the appropriate kind of
- Command object.
- */
- public class CommandFactory {
- public Command getCommand(String cmd) {
- cmdName = cmd.subString(0,8).trim();
- if(cmdName.equals("START")) {
- return new StartCommand(cmd);
- }
- if(cmdName.equals("END")) {
- return new EndCommand(cmd);
- }
- // ... more commands in more if blocks here
- // else it's a bad command.
- return new InvalidCommand(cmd);
- }
- }
- commandMap = new HashMap();
- commandMap.put("START",StartCommand.class);
- // ... etc.
- Class c = commandMap.get(cmdName);
- if (c != null)
- return c.newInstance();
- else
- throw new IllegalArgumentException(cmdName + " is not as valid command");
- public enum Command
- {
- START(StartCommand.class),
- END(EndCommand.class);
- private Class<? extends CommandInterface> mappedClass;
- private Command(Class<? extends CommandInterface> c) { mappedClass = c; }
- public CommandInterface getInstance()
- {
- return mappedClass.newInstance();
- }
- }
- public enum CommandFactory {
- START {
- @Override
- Command create(String cmd) {
- return new StartCommand(cmd);
- }
- },
- END {
- @Override
- Command create(String cmd) {
- return new EndCommand(cmd);
- }
- };
- abstract Command create(String cmd);
- public static Command getCommand(String cmd) {
- String cmdName = cmd.substring(0, 8).trim();
- CommandFactory factory;
- try {
- factory = valueOf(cmdName);
- }
- catch (IllegalArgumentException e) {
- return new InvalidCommand(cmd);
- }
- return factory.create(cmd);
- }
- }
- cmd.subString(0,8).trim();
- public class CommandFactory {
- private static List<Command> commands = new ArrayList<Command>();
- public static void registerCommand(Command cmd) {
- commands.add(cmd);
- }
- public Command getCommand(String cmd) {
- for(Command instance : commands) {
- if(instance.identifier(cmd)) {
- return cmd;
- }
- }
- throw new CommandNotRegisteredException(cmd);
- }
- }
- commandMap = new HashMap();
- commandMap.put("START",new StartCommand());
- command = ((Command) commandMap.get(cmdName)).clone();
- command.setCommandString(cmdName);
- class CreateStartCommands implements CommandCreator {
- public bool is_fitting_commandstring(String identifier) {
- return identifier == "START"
- }
- public Startcommand create_instance(cmd) {
- return StartCommand(cmd);
- }
- }
- public static Member getCommandObjectForRegistration();
- public static Member getCommandObjectForChangePassword();
- return
- cmdName.equals("START") ? new StartCommand (cmd) :
- cmdName.equals("END" ) ? new EndCommand (cmd) :
- new InvalidCommand(cmd);
- import xx.example.Command.*;
- Command command = Command.valueOf(commandStr);
- return
- command == START ? new StartCommand (commandLine) :
- command == END ? new EndCommand (commandLine) :
- new InvalidCommand(commandLine);
- public class CommandFactory {
- public Command getCommand(String cmd) {
- if(cmd == null) {
- return new InvalidCommand(cmd);
- }
- Class commandClass = (Class) commandMap.get(cmd);
- if(commandClass == null) {
- return new InvalidCommand(cmd);
- }
- try {
- Command newCommand = (Command) commandClass.newInstance();
- newCommand.setCommandString(cmd);
- return newCommand;
- }
- catch(Exception e) {
- return new InvalidCommand(cmd);
- }
- }
Add Comment
Please, Sign In to add comment