Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A java method with both variable return type and variable input arguments

I have an abstract java class "BaseOperation". This class only has a single abstract method:

public abstract T execute()
{
    ...

    return T;
}

Subclasses of BaseOperation must implement this method:

public class GetUsersOperation extends BaseOperation<GetUsersResponse>
{
    ...

    @Override
    public GetUsersResponse execute()
    {

        ...

        return GetUsersResponse;
    }

}

This is a great way to put all common "operation" logic in the BaseOperation class, but still have every concrete subclass's execute() method have a different return type.

Now I need to change this structure to allow the execute() methods to have a variable amount of arguments. For example, one concrete subclass would require:

execute(String, int)

and another would need:

execute(Date, Date, String)

This is tricky, because the execute method is declared in the base class. Simply overloading the execute methods in the base is not ideal. Firstly, the amount of overloads would be huge. Secondly, every subclass will only ever use one of the execute methods, what's the point of all the others?

The (in my opinion) easiest solution would be to declare the execute method with varargs:

execute(Object... arguments)

And then downcast all arguments in the subclasses:

execute(Object... arguments)
{
    String s = (String) arguments[0];
    ...
}

Obviously this has 2 major downsides:

  • Reduced performance because of all the downcasting operations
  • Calling the execute() methods is no longer strictly typed because any amount of objects can be passed witout compiler warnings.

Are there patterns or other solutions that could don't have these disadvantages?

like image 514
user1884155 Avatar asked Jan 28 '15 09:01

user1884155


People also ask

What is var ARG method in Java?

Variable Arguments (Varargs) in Java is a method that takes a variable number of arguments. Variable Arguments in Java simplifies the creation of methods that need to take a variable number of arguments.

Can a method have multiple arguments?

Multiple ArgumentsYou can actually have your variable arguments along with other arguments. That is, you can pass your method a double, an int, and then a String using varargs. It might seem silly to have multiple arguments in a method that already takes multiple arguments.

How do you pass multiple arguments in Java main method?

Use the String-Array to pass the parameters in. The main-Method has only the args parameter. You can pass all owners into the array, then put a limiter String into it (which can't be an owner or consumer) and then put all consumers into the array. In the main you iterate over the args-array and create two arrays of it.

How does Java deal with data types when passing arguments to methods?

In Java, you can pass an argument of any valid Java data type into a method. This includes simple data types such as doubles, floats and integers as you saw in the computePayment() method above, and complex data types such as objects and arrays. Here's an example of a method that accepts an array as an argument.


1 Answers

You could use a bean holding the parameters:

public interface BaseOperation<T, U> {
    T execute(U input);
}

public class GetUsersOperation implements BaseOperation<GetUsersResponse, UserInput> {

    @Override
    public GetUsersResponse execute(UserInput input) {
        Date date = input.getDate();
        return new GetUsersResponse(date);
    }

}

Your abstract class only has one single abstract method: better use an interface. You can implement several interfaces while you can extend only one class.

like image 71
sp00m Avatar answered Sep 20 '22 10:09

sp00m