code_junkie

My Java factory method smells. How do I fix it?

Nov 11th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. /*
  2. Given a command string in which the first 8 characters are the command name
  3. padded on the right with whitespace, construct the appropriate kind of
  4. Command object.
  5. */
  6. public class CommandFactory {
  7. public Command getCommand(String cmd) {
  8. cmdName = cmd.subString(0,8).trim();
  9.  
  10. if(cmdName.equals("START")) {
  11. return new StartCommand(cmd);
  12. }
  13. if(cmdName.equals("END")) {
  14. return new EndCommand(cmd);
  15. }
  16. // ... more commands in more if blocks here
  17. // else it's a bad command.
  18. return new InvalidCommand(cmd);
  19. }
  20. }
  21.  
  22. commandMap = new HashMap();
  23. commandMap.put("START",StartCommand.class);
  24. // ... etc.
  25.  
  26. Class c = commandMap.get(cmdName);
  27. if (c != null)
  28. return c.newInstance();
  29. else
  30. throw new IllegalArgumentException(cmdName + " is not as valid command");
  31.  
  32. public enum Command
  33. {
  34. START(StartCommand.class),
  35. END(EndCommand.class);
  36.  
  37. private Class<? extends CommandInterface> mappedClass;
  38. private Command(Class<? extends CommandInterface> c) { mappedClass = c; }
  39. public CommandInterface getInstance()
  40. {
  41. return mappedClass.newInstance();
  42. }
  43. }
  44.  
  45. public enum CommandFactory {
  46. START {
  47. @Override
  48. Command create(String cmd) {
  49. return new StartCommand(cmd);
  50. }
  51. },
  52. END {
  53. @Override
  54. Command create(String cmd) {
  55. return new EndCommand(cmd);
  56. }
  57. };
  58.  
  59. abstract Command create(String cmd);
  60.  
  61. public static Command getCommand(String cmd) {
  62. String cmdName = cmd.substring(0, 8).trim();
  63.  
  64. CommandFactory factory;
  65. try {
  66. factory = valueOf(cmdName);
  67. }
  68. catch (IllegalArgumentException e) {
  69. return new InvalidCommand(cmd);
  70. }
  71. return factory.create(cmd);
  72. }
  73. }
  74.  
  75. cmd.subString(0,8).trim();
  76.  
  77. public class CommandFactory {
  78. private static List<Command> commands = new ArrayList<Command>();
  79.  
  80. public static void registerCommand(Command cmd) {
  81. commands.add(cmd);
  82. }
  83.  
  84. public Command getCommand(String cmd) {
  85. for(Command instance : commands) {
  86. if(instance.identifier(cmd)) {
  87. return cmd;
  88. }
  89. }
  90. throw new CommandNotRegisteredException(cmd);
  91. }
  92. }
  93.  
  94. commandMap = new HashMap();
  95. commandMap.put("START",new StartCommand());
  96.  
  97. command = ((Command) commandMap.get(cmdName)).clone();
  98.  
  99. command.setCommandString(cmdName);
  100.  
  101. class CreateStartCommands implements CommandCreator {
  102. public bool is_fitting_commandstring(String identifier) {
  103. return identifier == "START"
  104. }
  105. public Startcommand create_instance(cmd) {
  106. return StartCommand(cmd);
  107. }
  108. }
  109.  
  110. public static Member getCommandObjectForRegistration();
  111. public static Member getCommandObjectForChangePassword();
  112.  
  113. return
  114. cmdName.equals("START") ? new StartCommand (cmd) :
  115. cmdName.equals("END" ) ? new EndCommand (cmd) :
  116. new InvalidCommand(cmd);
  117.  
  118. import xx.example.Command.*;
  119.  
  120. Command command = Command.valueOf(commandStr);
  121. return
  122. command == START ? new StartCommand (commandLine) :
  123. command == END ? new EndCommand (commandLine) :
  124. new InvalidCommand(commandLine);
  125.  
  126. public class CommandFactory {
  127. public Command getCommand(String cmd) {
  128. if(cmd == null) {
  129. return new InvalidCommand(cmd);
  130. }
  131.  
  132. Class commandClass = (Class) commandMap.get(cmd);
  133.  
  134. if(commandClass == null) {
  135. return new InvalidCommand(cmd);
  136. }
  137.  
  138. try {
  139. Command newCommand = (Command) commandClass.newInstance();
  140. newCommand.setCommandString(cmd);
  141. return newCommand;
  142. }
  143. catch(Exception e) {
  144. return new InvalidCommand(cmd);
  145. }
  146. }
Add Comment
Please, Sign In to add comment