Announcement Announcement Module
Collapse
No announcement yet.
Factory and injection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Factory and injection

    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.

  • #2
    Why are you trying to avoid using the injected HttpService? Not sure what you are trying to accomplish.

    Comment


    • #3
      Originally posted by jonnio View Post
      Why are you trying to avoid using the injected HttpService? Not sure what you are trying to accomplish.
      i think its something to do with the law of demeter. don't ask for things you don't need. or...
      have i misunderstood the law of demeter?

      i mean
      inject a, then use a.b().c().d();
      that's wrong.
      but

      inject a,
      but don't do anything to a, just pass it around as a constructor,
      is that wrong?

      what i'm trying to accomplish is "not to break the law of demeter"

      Comment


      • #4
        You example 2 only depends on the factory. This looks ok to me, what do you think is still wrong with it?.

        Depending on the context you could make a more general interface like I(Get)TaskFactory and let WebGetTask implement it, but you could also choose not to do this indirection. As long as the code inside WebGetTask is not duplicated, you can always refactor your code and have WebGetTask implement a new interface.

        I do agree that example 1 is not very nice because you depend on the specific http implementation of get tasks, while you may want to swap this implementation for example for a cached one, or an RMI one.

        Comment

        Working...
        X