Note that there are some explanatory texts on larger screens.

plurals
  1. PODo Abstract Factories use "new"?
    text
    copied!<p>I am trying to use Dependency Injection as much as possible, but I am having trouble when it comes to things like short-lived dependencies.</p> <p>For example, let's say I have a blog manager object that would like to generate a list of blogs that it found in the database. The options to do this (as far as I can tell) are:</p> <ol> <li>new Blog(); </li> <li>$this->loader->blog(); <ul> <li>the loader object creates various other types of objects like database objects, text filters, etc.</li> </ul></li> <li>$this->blogEntryFactory->create();</li> </ol> <p>However, #1 is bad because it creates a strong coupling. #2 still seems bad because it means that the object factory has to be previously injected - exposing all the other objects that it can create.</p> <p>Number 3 seems okay, but if I use #3, do I put the "new" keywords in the blogEntryFactory itself, OR, do I inject the loader into the blogEntryFactory and use the loader?</p> <p>If I have many different factories like blogEntryFactory (for example I could have userFactory and commentFactory) it would seem like putting the "new" keyword across all these different factories would be creating dependency problems.</p> <p>I hope this makes sense...</p> <p><strong>NOTE</strong></p> <p>I have had some answers about how this is unnecessary for this specific blog example, but there are, in fact, cases where you should use the Abstract Factory Pattern, and that is the point I am getting at. Do you use "new" in that case, or do something else?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload