Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Abstract Factories use "new"?

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.

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:

  1. new Blog();
  2. $this->loader->blog();
    • the loader object creates various other types of objects like database objects, text filters, etc.
  3. $this->blogEntryFactory->create();

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.

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?

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.

I hope this makes sense...

NOTE

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?

like image 966
johnnietheblack Avatar asked Feb 02 '12 18:02

johnnietheblack


People also ask

Does Abstract Factory use factory method?

Abstract factory is an interface for creating related objects but factory method is a method. Abstract factory is implemented by factory method.

How does an Abstract Factory work?

Abstract Factory provides interfaces for creating families of related or dependent objects without specifying their concrete classes. Client software creates a concrete implementation of the abstract factory and then uses the generic interfaces to create the concrete objects that are part of the family of objects.

Where is Abstract Factory pattern used?

The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes. This pattern is found in the sheet metal stamping equipment used in the manufacture of Japanese automobiles.

What is meant by Abstract Factory?

Abstract Factory is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.


1 Answers

I'm no expert, but I'm going to take a crack at this. This assumes that Blog is just a data model object that acts as a container for some data and gets filled by the controller (new Blog is not very meaningful). In this case, Blog is a leaf of the object graph, and using new is okay. If you are going to test methods that need to create a Blog, you have to simultaneously test the creation of the Blog anyway, and using a mock object doesn't make sense .. the Blog does not persist past this method.

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

like image 93
Explosion Pills Avatar answered Sep 20 '22 12:09

Explosion Pills