I have an abstract Java class "BaseOperation". This class has only one abstract method:
public abstract T execute() { ... return T; }
BaseOperation subclasses must implement this method:
public class GetUsersOperation extends BaseOperation<GetUsersResponse> { ... @Override public GetUsersResponse execute() { ... return GetUsersResponse; } }
This is a great way to put all the usual βworkβ logic in the BaseOperation class, but still, every particular execute() subclass has a different return type.
Now I need to modify this structure to allow the execute () methods to have a variable number of arguments. For example, for one particular subclass, you would need:
execute(String, int)
and the other will need:
execute(Date, Date, String)
This is complicated because the execute method is declared in the base class. Just overloading the execution methods in the database is not ideal. Firstly, the amount of congestion will be huge. Secondly, each subclass will use only one of the execution methods, what is the point of all the others?
(in my opinion) the simplest solution would be to declare the execute method with varargs:
execute(Object... arguments)
And then empty all the arguments in the subclasses:
execute(Object... arguments) { String s = (String) arguments[0]; ... }
Obviously, this has two main disadvantages:
- Slow performance due to all downcasting operations
- Calling the
execute() methods is no longer strongly typed, because any number of objects can be transmitted without compiler warnings.
Are there templates or other solutions that might not have these flaws?