interface Command<I, O> {
O process(I i);
}
interface Undo<I> {
void undo(I i);
}
public class CommandRunner {
public static <I, O> O process(Command<I, O> command, I request) {
O result = null;
try {
result = command.process(request);
} catch(Exception ex) {
if (command instanceof Undo) {
((Undo<I>) command).undo(request); // <-- unchecked cast
}
}
return result;
}
}
如何避免未经检查的投放警告?
答案 0 :(得分:0)
1)如果Undo
是专业 Command
,则将Undo
扩展Command
:-
interface Undo<I> extends Command {...}
2)否则,您可以创建第三个界面:-
interface ReversibleCommand extends Command, Undo {...}
并切换到if (command instanceof ReversibleCommand) {...}