Law Of Demeter on Factory Pattern and Dependency Injection -
I have a question about dependency injection.
I want to make it a class call, WebGetTask
WebGetTask will need dependencies for HttpService
Bad code 1 code:
Private HttpService httpService; ... list & lt; WebGetTask & gt; List = New Arrestist & lt; WebGetTask & gt; (); For (...) {list.add (New WebGetTask (httpService)); } ... Ok I know that this is bad because httpService is injected, but it is never used, except for creating a new webgate tab for < / P>
OK bad code 2 code:
Personal WebGetTaskFactory webGetTaskFactory; ... list & lt; WebGetTask & gt; List = New Arrestist & lt; WebGetTask & gt; (); For (...) {list.add (webGetTaskFactory.newTask ()); } ... I think this is better, because we use a factory but ... but ..
from where I stand I can see that we are still doing an HttpService injection in WebGetTaskFactory and do nothing for this except for the sole purpose of creating a new WebGetTask
then to recap my question How do I design a factory class (WebGetTaskFactory), that the new objects (WebGetTask) When does the dependency on new object (HTPSY) require a dependency (HTPS) on your manufacturer without injection and passing? Or rather, this is the way to do it? If so, it's all good, if not, then please give me instructions on how to properly use di and factory patterns. Thank you.
I'm assuming that the code you have shown is part of a DownloadManager class, and That you inject your dependencies through the constructor. In this case, I look forward to the start-up code that looks all together to look like this:
IHttpService httpService = new HttpService (); IWebGetTaskFactory webGetTaskFactory = New WebGetTaskFactory (httpService); IDownloadManager downloadManager = New DownloadManager (webGetTaskFactory); DownloadManager class only knows about the IWebGetTaskFactory interface. It does not know about IHttpService, thus satisfying the law of dematrice.
Edit: After reading your question again, it seems that you are worried that you "use" the HTTPS service in your factory, pass it on to a new WebGetTask Apart from it's fine. Both WebGetTaskFactory and WebGetTask require an HttpService instance to do their job. This is not a violation of demetor's law.
Comments
Post a Comment