Tuesday, 13 August 2013

Java Co-Variance

Java Co-Variance

For a project I want to provide a generic interface to resemble a
workflow, like
public interface IWorkflow
{
public void start();
public void doWork();
public void end();
}
For that, I have lots of implementation, like
public class CoffeeWorkflow implements IWorkflow
{
public void start()
{
// setup coffee
// prepare dishes
// ...
}
public void doWork()
{
// drink coffee
}
public void end()
{
// wash dishes
}
}
Now I want to provide more information to those functions, like
public interface IWorkflowStartArgs
{}
And especially:
public class CoffeeWorkflowStartArgs implements IWorkflowArgs
To give it into the method
public interface IWorkflow
{
public void start(IWorkflowStartArgs args);
public void doWork();
public void end();
}
respectivly:
public class CoffeeWorkflow implements IWorkflow
{
public void start(CoffeeWorkflowStartArgs args)
{
}
}
But this does not work, as it is not recognized as implementation of the
interface.
Should I pass in an IWorkflowStartArgs and cast it inside?
Is there a better solution to that?

No comments:

Post a Comment