Question

Given the following interface:

Interface Executor<T extends Form>
{
   public Result execute( Request<T> request);
}

as well as a number of implementations of this interface, such as:

  • LoginExecutor,
  • AddContactExecutor, etc.

For each implementation, I know exactly what the value of T is going to be.

E.g, for LoginExecutor, T will be LoginForm extends Form, etc.

What's the conventional way of defining my implementations in such a case? If I do:

public LoginExecutor<LoginForm> implements Executor<LoginForm>
{
   public Result execute(LoginForm request) {...}
}

that gives me an error. So I'm doing the following:

public LoginExecutor<T extends LoginForm> implements Executor<T>
{
   public Result execute(T request) {...}
}

and that seems to be working, however I'm wondering if there is a a better / more conventional way of doing this.

Was it helpful?

Solution

Typically if you know the types of the generic you can just do the following:

public LoginExecutor implements Executor<LoginForm>
{
   public Result execute(LoginForm request) {...}
}

as the name suggests this approach is used for generic programming. When the type is know and you want to use for a particular type, you just implement using the type and from generic it becomes specific.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top