I have a Context class that is a key value pair that is populated gradually at runtime.
I would like to create instances of objects that require some values from the context.
For example:
public interface Task
{
void execute();
}
public interface UiService
{
void moveToHomePage();
}
public class UiServiceImpl implements UiService
{
public UiService(@ContexParam("username") String username, @ContexParam("username") String password)
{
login(username, password);
}
public void navigateToHomePage() {}
private void login(String username, String password)
{
//do login
}
}
public class GetUserDetailsTask implements Task
{
private ContextService context;
@Inject
public GetUserDetailsTask(ContextService context)
{
this.context = context;
}
public void execute()
{
Console c = System.console();
String username = c.readLine("Please enter your username: ");
String password = c.readLine("Please enter your password: ");
context.add("username", username);
context.add("password", password);
}
}
public class UseUiServiceTask implements Task
{
private UiService ui;
@Inject
public UseUiServiceTask(UiService uiService)
public void execute()
{
ui.moveToHomePage();
}
}
I want to be able to create an instance of UseUiServiceTask using Guice. How do I achieve that?