hummingV
hummingV

Reputation: 1074

Factory to create Factory?

I refer to GoF Abstract Factory and Factory Method patterns. I understand their purpose but what bugs me is that both patterns omits the discussion of how the client gets an instance of the "factory object". For example, in the following abstract factory pattern, the discussion assumes that some other framework or driver code has already constructed the hierarchy, and that Application instance already holds an instance of GUIFactory (either WinFactory or OSXFactory)

(source: https://en.wikipedia.org/wiki/Abstract_factory_pattern) Abstract factory pattern

Similar argument goes towards Factory Method pattern. The discussion assumes something else outside has already instantiated MagicMazeGame.

(source:https://en.wikipedia.org/wiki/Factory_method_pattern) Factory method pattern

Why does it bother me? Because we then need another pattern to construct these object hierarchies and that pattern is the one that many people actually mean when they say Factory. Basically the factory pattern is the following:

(source: Domain Driven Design, Eric Evans 2003) factory pattern

This is a different pattern from either Abstract Factory or Factory Method, in my opinion. The client specifies requirements and the factory creates objects based on the requirements as well as based on configuration and context. As an hypothetical example, client specifies that it wants an abstract Connection object, the factory creates a MySQLConnection or OracleConnection depending on the database driver configured in the system.

So, my questions are:

I know that this question is just asking about how to name things, but using a Ubiquitous Language (source: Domain Driven Design, Eric Evans 2003. And I completely agree) is also very important in software engineering in my opinion.

Upvotes: 0

Views: 276

Answers (1)

jaco0646
jaco0646

Reputation: 17066

The pattern used to construct object hierarchies is known as Composition Root. This pattern applies to the construction of any concrete objects intended to fulfill an interface, including concrete factories intended to fulfill factory interfaces.

Composition Root is typically used in conjunction with dependency injection, so it's interesting to note that Erich Gamma mentioned adding dependency injection when he was asked in 2009 about how he might "refactor" the GoF book.

The diagram from Eric Evans appears to be an architectural view where the term factory is used as a placeholder for any factory pattern. In other words, it could represent Abstract Factory, Factory Method, or some other factory pattern outside the GoF. The particular factory pattern doesn't matter at the architectural level, only the idea that the client defers instantiation logic to some other component.

Upvotes: 3

Related Questions