0

I was learning abstract factory pattern on the internet and have one basic question. Most of the class diagrams including one on Wikipedia show that the client refers AbstractFactory and AbstractProduct. The client does not actually have references to concrete factories. However, all most all code I have seen on the internet create the instance of concrete factories in client code or main method. Is that correct implementation? Isn't is true that client only needs to know about the abstract factory and abstract product?

Community
  • 1
  • 1
  • It's impossible to answer on such an abstract question. Factories are creational patterns. They create objects. That's it. Everything else is out of their scope, – zerkms Mar 20 '17 at 02:03
  • Possible duplicate of [Why do we need Abstract factory design pattern?](http://stackoverflow.com/questions/2280170/why-do-we-need-abstract-factory-design-pattern) – Nemus Mar 20 '17 at 02:03

1 Answers1

1

"Do we need to expose concrete factory to client in abstract factory pattern"

Obviously not or that would defeat the purpose of the pattern all together, preventing to easily swap one factory for another (and one object family for another).

allmost all code I have seen on the internet create the instance of concrete factories in client code or main method

There must be a place in the code where the concrete factory gets instantiated, but that would be in the Composition Root. Anywhere else would be wrong.

plalx
  • 42,889
  • 6
  • 74
  • 90
  • +1 for the composition root. Another term I came across was `wiring`. Based on some attribute of the client side enviornment corresponding concrete factory can be used, without client knowing the concrete factory class. The main intent of abstract factory pattern is to hide the actual concrete products belonfing to an environment. This allows the ease of maintaince if the same code is ported to another enviornment or if the underlying product classes are modified. I feel concrete factory classes exposure can be relaxed though best they remain hidden. – nits.kk Mar 20 '17 at 03:10