views:

1974

answers:

18

There's something very unsatisfactory about this code:

/*
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);
     }
}

I'm unrepentant about the multiple exit points - the structure is clear. But I'm not happy about the series of near-identical if statements. I've considered making a Map of Strings to Commands:

commandMap = new HashMap();
commandMap.put("START",StartCommand.class);
// ... etc.

... then using Reflection to make instances of the appropriate class looked up from the Map. However while conceptually elegant, this involves a fair amount of Reflection code that whoever inherits this code might not appreciate - although that cost might be offset by the benefits. All the lines hardcoding values into the commandMap smell almost as bad as the if block.

Even better would be if the factory's constructor could scan the classpath for subclasses of Command, query them for String representations, and automatically add them them to its repertoire.

So - how should I go about refactoring this?

I guess some of the frameworks out there give me this kind of thing for free. Let's assume I'm not in a position to migrate this stuff into such a framework.

A: 

At the very least, your command should have a getCommandString() -- where StartCommand overrides to return "START". Then you can just register or discover the classes.

Lou Franco
+1  A: 

Taking a Convetion vs Configuration approach and using reflection to scan for available Command objects and loading them into your map would be the way to go. You then have the ability to expose new Commands without a recompile of the factory.

Gareth D
+2  A: 

Its not directly an answer to your question, but why don't you throw an InvalidCommandException (or something similar), rather then returning an object of type InvalidCommand?

Einar
Command has an execute() method, which subclasses override. InvalidCommand's execute() method is responsible for doing the right thing with an invalid command.
slim
+10  A: 

Your map of strings to commands I think is good. You could even factor out the string command name to the constructor (i.e. shouldn't StartCommand know that its command is "START"?) If you could do this, instantiation of your command objects is much simpler:

Class c = commandMap.get(cmdName);
if (c != null)
    return c.newInstance();
else
    throw new IllegalArgumentException(cmdName + " is not as valid command");

Another option is to create an enum of all your commands with links to the classes (assume all your command objects implement CommandInterface):

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();
    }
}

since the toString of an enum is its name, you can use EnumSet to locate the right object and get the class from within.

davetron5000
Using a HashMap seems like the simplest implementation and is easy to maintain and understand.
18Rabbit
A: 

Having this repetitive object creation code all hidden in the factory is not so bad. If it has to be done somewhere, at least it's all here, so I'd not worry about it too much.

If you really want to do something about it, maybe go for the Map, but configure it from a properties file, and build the map from that props file.

Without going the classpath discovery route (about which I don't know), you'll always be modifying 2 places: writing a class, and then adding a mapping somewhere (factory, map init, or properties file).

John Flinchbaugh
+3  A: 

With the exception of the

cmd.subString(0,8).trim();

part, this doesn't look too bad to me. You could go with the Map and use reflection, but, depending on how often you add/change commands, this might not buy you much.

You should probably document why you only want the first 8 characters, or maybe change the protocol so it's easier to figure out which part of that string is the command (e.g. put a marker like ':' or ';' after the command key-word).

Outlaw Programmer
+2  A: 

I like your idea, but if you want to avoid reflection you could add instead instances to the HashMap:

commandMap = new HashMap();
commandMap.put("START",new StartCommand());

Whenever you need a command, you just clone it:

command = ((Command) commandMap.get(cmdName)).clone();

And afterwards, you set the command string:

command.setCommandString(cmdName);

But using clone() doesn't sound as elegant as using reflection :(

Auron
The beauty of this answer is that each command can have its own set of constructor parameters, as opposed to the original design, which forces all commands to have a no-arg constructor. This solves the problem of how to inject commands with their dependencies.
Andrew Swan
A: 

Thinking about this, You could create little instantiation classes, like:

class CreateStartCommands implements CommandCreator {
    public bool is_fitting_commandstring(String identifier) {
        return identifier == "START"
    }
    public Startcommand create_instance(cmd) {
        return StartCommand(cmd);
    }
}

Of course, this adds a whole bunch if tiny classes that can't do much more than say "yes, thats start, give me that" or "nope, don't like that", however, you can now rework the factory to contain a list of those CommandCreators and just ask each of it: "you like this command?" and return the result of create_instance of the first accepting CommandCreator. Of course it now looks kind of akward to extract the first 8 characters outside of the CommandCreator, so I would rework that so you pass the entire command string into the CommandCreator.

I think I applied some "Replace switch with polymorphism"-Refactoring here, in case anyone wonders about that.

Tetha
A: 

I'd go for the map and creation via reflection. If scanning the class path is too slow, you can always add a custom annotation to the class, have an annotation processor running at compile time and store all class names in the jar metadata.

Then, the only mistake you can do is forgetting the annotation.

I did something like this a while ago, using maven and APT.

Torsten Marek
+2  A: 

Unless there is a reason they can't be I always try to make my command implementations stateless. If that's the case you can add a method boolean identifier(String id) method to your command interface which would tell whether this instance could be used for the given string identifier. Then your factory could look something like this (note: I did not compile or test this):

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);
    }
}
Mike Deck
+1  A: 

Another approach to dynamically finding the class to load, would be to omit the explicit map, and just try to build the class name from the command string. A title case and concatenate algorithm could turn "START" -> "com.mypackage.commands.StartCommand", and just use reflection to try to instantiate it. Fail somehow (InvalidCommand instance or an Exception of your own) if you can't find the class.

Then you add commands just by adding one object and start using it.

John Flinchbaugh
+9  A: 

How about the following code:

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);
    }
}

The valueOf(String) of the enum is used to find the correct factory method. If the factory doesn't exist it will throw an IllegalArgumentException. We can use this as a signal to create the InvalidCommand object.

An extra benefit is that if you can make the method create(String cmd) public if you would also make this way of constructing a Command object compile time checked available to the rest of your code. You could then use CommandFactory.START.create(String cmd) to create a Command object.

The last benefit is that you can easily create a list of all available command in your Javadoc documentation.

Roel Spilker
This had introduced me to enum, which is great.Except I'm on Java 1.4 for the timebeing (I know - it's out of my hands). Hence the absence of generics on my Map.
slim
I realize that Java 1.4 is still out there. It's a pity.
Roel Spilker
+1  A: 

One option would be for each command type to have its own factory. This gives you two advantages:

1) Your generic factory wouldn't call new. So each command type could in future return an object of a different class according to the arguments following the space padding in the string.

2) In your HashMap scheme, you could avoid reflection by, for each command class, mapping to an object implementing a SpecialisedCommandFactory interface, instead of mapping to the class itself. This object in practice would probably be a singleton, but need not be specified as such. Your generic getCommand then calls the specialised getCommand.

That said, factory proliferation can get out of hand, and the code you have is the simplest thing that does the job. Personally I'd probably leave it as it is: you can compare command lists in source and spec without non-local considerations like what might have previously called CommandFactory.registerCommand, or what classes have been discovered through reflection. It's not confusing. It's very unlikely to be slow for less than a thousand commands. The only problem is that you can't add new command types without modifying the factory. But the modification you'd make is simple and repetitive, and if you forget to make it you get an obvious error for command lines containing the new type, so it's not onerous.

Steve Jessop
A: 

The way I do it is to not have a generic Factory method.

I like to use Domain Objects as my command objects. Since I use Spring MVC this is a great approach since the DataBinder.setAllowedFields method allows me a great deal of flexibility to use a single domain object for several different forms.

To get a command object, I have a static factory method on the Domain object class. For example, in the member class I'd have methods like -

public static Member getCommandObjectForRegistration();
public static Member getCommandObjectForChangePassword();

And so on.

I'm not sure that this is a great approach, I never saw it suggested anywhere and kind of just came up with it on my own b/c I like the idea of keeping things like this in one place. If anybody sees any reason to object please let me know in the comments...

bpapa
A: 

+1 on the reflection suggestion, it will give you a more sane structure in your class.

Actually you could do the following (if you haven't thought about it already) create methods corresponding to the String you'd be expecting as an argument to your getCommand() factory method, then all you have to do is reflect and invoke() these methods and return the correct object.

Yiannis
A: 

I would suggest avoiding reflection if at all possible. It is somewhat evil.

You can make your code more concise by using the ternary operator:

 return 
     cmdName.equals("START") ? new StartCommand  (cmd) :
     cmdName.equals("END"  ) ? new EndCommand    (cmd) :
                               new InvalidCommand(cmd);

You could introduce an enum. Making each enum constant a factory is verbose and also has some runtime memory cost. But you can eaily lookup an enum and then use that with == or switch.

 import xx.example.Command.*;

 Command command = Command.valueOf(commandStr);
 return 
     command == START ? new StartCommand  (commandLine) :
     command == END   ? new EndCommand    (commandLine) :
                        new InvalidCommand(commandLine);
Tom Hawtin - tackline
A: 

Go with your gut, and reflect. However, in this solution, your Command interface is now assumed to have the setCommandString(String s) method accessible, so that newInstance is easily useable. Also, commandMap is any map with String keys (cmd) to Command class instances that they correspond to.

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);
     }
}
MetroidFan2002
A: 

Hmm, browsing, and only just came across this. Can I still comment?

IMHO there's nothing wrong with the original if/else block code. This is simple, and simplicity must always be our first call in design (http://c2.com/cgi/wiki?DoTheSimplestThingThatCouldPossiblyWork)

This seems esp true as all the solutions offered are much less self documenting than the original code...I mean shouldn't we write our code for reading rather than translation...

StripLight