Do abstract factories use the "new"?

I try to use Injection Dependency as much as possible, but I have problems when it comes to things like short-lived dependencies.

For example, let's say I have a blog manager object that would like to create a list of blogs that it found in the database. Possible options (as far as I can tell):

  • new blog ();
  • $ this-> Loader-> blog ();
    • The loader object creates various other types of objects, such as database objects, text filters, etc.
  • $ this-> blogEntryFactory-> create ();

However, # 1 is bad because it creates a strong bond. # 2 still seems bad because it means the factory object must be pre-entered - expose all other objects that it can create.

The number 3 looks fine, but if I use # 3, I put the β€œnew” keywords in blogEntryFactory itself, OR do I insert the loader into blogEntryFactory and use the loader?

If I have many different factories, such as blogEntryFactory (for example, I could have userFactory and commentFactory), it seems that adding the keyword β€œnew” in all these different factories created dependency problems.

Hope this makes sense ...

Note

I had some answers about how this is not necessary for this particular example blog, but there are, in fact, cases where you should use the Abstract factory template, and this is the moment I get it. Are you using the β€œnew” in this case, or are you doing something else?

+8
php design-patterns dependency-injection abstract-factory
source share
1 answer

I am not an expert, but I am going to hack this. This suggests that Blog is just a data model object that acts as a container for some data and is populated with a controller ( new Blog not very significant). In this case, Blog is an object graph sheet, and the use of new is fine. If you are going to test the methods that Blog should create, you should test the creation of Blog at the same time, and using a mock object does not make sense. Blog does not save this method.

As an example, let's say that PHP did not have an array construct, but had a collection object. You would call $this->collectionsFactory->create() or you would be satisfied to say new Array; ?

+4
source share

All Articles