hello all I have a question regarding dependency injection.
say i want to create a class call it, WebGetTask
WebGetTask would need a dependency to HttpService
bad code 1 Code:
private HttpService httpService;
...
List<WebGetTask> list = new ArrayList<WebGetTask>();
for(...)
{
list.add(new WebGetTask(httpService));
}
...
ok. i know this is bad, because httpService is injected, but its never used, except for the creation on a new WebGetTask
ok bad code 2 Code:
private WebGetTaskFactory webGetTaskFactory;
...
List<WebGetTask> list = new ArrayList<WebGetTask>();
for(...)
{
list.add(webGetTaskFactory.newTask());
}
...
i think this is better, because we use a factory but... but..
from where i'm standing, i can see that in WebGetTaskFactory we are still injecting a HttpService and not doing anything to it except for the sole purpose of creating a new WebGetTask
so to recap my question is how do i design a factory class (WebGetTaskFactory), that creates new objects (WebGetTask) when the new objects require a dependency (HttpService) on their constructor without simply injecting and passing the dependency (HttpService) ? or rather, is this the way to do it? if so, then it's all good, if its not, then please guide me to how to properly use DI and factory pattern. thanks.