1

There is a huge discussion at my work what a factory pattern is.

The main discussion point is whether one within a factory under creation of the object also may branch out calls for population of its sub classes through other factories.

Or the parent factory needs these details beforehand as a part of its constructor as a parameter along with other data?

Is it ok to call other factories inside a factory? or should a factory be self contained/sustained and completely independant?

codingjoe
  • 707
  • 5
  • 15
  • 32
  • There are several, very different, factory patterns. If you take a look at [this answer](https://stackoverflow.com/a/60285883/1371329) of mine, it will clear up some confusing points and link through to a longer answer that describes a couple of the patterns in great detail. – jaco0646 Mar 05 '20 at 22:01
  • After reading that information, it would be helpful if you could name which factory pattern you are asking about, and add some (pseudo) code demonstrating the alternatives that are causing disagreement at your work. – jaco0646 Mar 05 '20 at 22:05
  • Yes, we are aware of the different flavors of the factory pattern. However the question is whether or not a factory may depend on branches of the object's subclasses. May a factory depend on other factory methods or should a factory be (self sufficient) independent? – codingjoe Mar 06 '20 at 04:37
  • The question does not make sense to me without naming a specific pattern and giving an example of what is (potentially) not allowed. There is too much confusion around these patterns to simply guess one; and the assumption that this question has one answer for all flavors is probably not correct. The flavors are wildly different. The answers may be as well. – jaco0646 Mar 06 '20 at 15:12

1 Answers1

1

This is not a Yes or No question for me, so, here it comes.

I think it is OK to call a factory inside a factory, but, it cannot be done without considering the drawbacks it may cause. The deeper is the chain of factories the more complex is the dependency. Also, it may affect maintainability and testability.

In order to avoid a concrete dependency, you may want to decouple it, applying SOLID principles. It will drive you to a completely flexible and injectable factory family. But, without a DI Container, it can become a pain and with a DI Container, performance should be considered as well.

So, the short answer is yes but, you need to find the balance among flexibility, maintainability, testability, and performance.

Thumb rule: Create the structures if they are really needed, no overengineering.

  • The question does not mention the GoF Abstract Factory Pattern. – jaco0646 Mar 06 '20 at 14:56
  • First of all the question is not about abstract factory, but more the simple factory object creation pattern. Secondly that is exactly my own view on it. At work a dude has made various factories with chain going as deep as 4-5 levels, which makes one loose orientation pretty quickly. I would rather see, that you have independent and self sustained factories. – codingjoe Mar 06 '20 at 20:41
  • I have heard some people referring these deep chaining factories as “magic factories” and not in a positive sense. – codingjoe Mar 06 '20 at 20:50
  • ok @codingjoe, so I can rephrase my answer if you clarify the pattern you are referring to. As it is a "pattern" it should have been named elsewhere. Another option for Factory is the [Factory Method](https://www.dofactory.com/net/factory-method-design-pattern), is it the one you want to discuss? – Wederson Soares Mar 07 '20 at 05:59
  • @wedersonSoares yes, sorry I can see I wasn’t clear about that. It is the factory method, that I am referring to. – codingjoe Mar 07 '20 at 07:48