The idea
I need to create Commands. Commands can be configured with parameters. Not every command can receive the same parameters. So some have to be ignored.
I have an abstract class Command in which I've defined a Builder. By default every append parameter throws 'UnsupportedOperationException'
public abstract class Command {
public static abstract class CommandBuilder {
// TODO instanceof. How to do this better?
public CommandBuilder append(Parameter p)
throws UnsupportedOperationException {
if (p instanceof URLParameter)
return append((URLParameter) p);
if (p instanceof ActionParameter)
return append((ActionParameter) p);
if (p instanceof RepeatParameter)
return append((RepeatParameter) p);
if (p instanceof TimeOutParameter)
return append((TimeOutParameter) p);
return this;
}
public CommandBuilder append(URLParameter p)
throws UnsupportedOperationException {
throw new UnsupportedOperationException(
"URLParameter not applicable");
}
public CommandBuilder append(RepeatParameter p)
throws UnsupportedOperationException {
throw new UnsupportedOperationException(
"RepeatParameter not applicable");
}
...
}
If you want a parameter to be applicable onto a certain concrete Command, lets say an FTPCommand.
You will have to do something like this:
public class FTPCommand extends Command {
public static class Builder extends CommandBuilder {
@Override
public CommandBuilder append(URLParameter p) {
System.out.println("URLParemeter appended");
return this;
}
}
}
So that when a URLParameter is provided it doesn't throw an exception anymore but instead applies it.
But the client of this CommandBuilder might not be able to provide the concrete subclass. So in general a "Parameter" is given. But it needs to go to the right place (method)
Like a URLParameter must arrive at the append(UrlParameter p)
How can I do this in a clean(er) and nice(r) way? Because I'm not really 'enthousiastic' to use instanceof .
This looks like a classic double-dispatch or visitor scenario. From the double-dispatch reference:
a mechanism that dispatches a function call to different concrete functions depending on the runtime types of two objects involved in the call
The Parameter
and the CommandBuilder
need to interact between themselves on what to do.
The CommandBuilder
can call back on the parameter. The Parameter
objects all implement a common interface and the implementation of each subclass will differ.
public CommandBuilder append(Parameter p) {
// the append method called depends on the underlying type of 'p'
p.append(this);
}
I would add a visitor method to your interface
interface Parameter {
public void append(CommandBuilder builder);
}
class CommandBuilder {
public CommandBuilder append(Parameter p) {
p.append(this);
}
}
来源:https://stackoverflow.com/questions/11792950/how-to-get-rid-of-instanceof-in-this-builder-implementation