2

In Abstract Factory Pattern the major entities involved are

  1. Abstract Base Factory
  2. Concrete Factories each extending Abstract base factory
  3. Client code
  4. Base product
  5. Concrete product classes extending Base product

I have seen at various implementations and observed that the Client code knows about the concrete factories. As per the common definition of the pattern I have seen at various places is as below

Defines an interface for creating objects, but let sub-classes to decide which class to instantiate.

As per my observation, createProduct is implemented as abstract method in abstract factory method. It exposes a non abstract public method like getProduct from where it makes call to the createProductmethod. As per the run-time object of concrete factory class createProduct is called accordingly.

Even if the base abstract factory class is not present then client code can simply call the createProduct methods on the object of concrete factory classes as concrete factory classes are visible to client code.

I feel Abstract base class is only useful in below case

If we have code to create the objects of the concrete Factory classes separate from the code calling the getProduct method on those objects. Code instantiating the concrete Factory classescan put the factories in some queue and later queue can be iterated and Concrete products can be obtained.

Please provide your valuable feedback.

nits.kk
  • 5,204
  • 4
  • 33
  • 55

2 Answers2

2

The client should not have knowledge of any concrete factory. The abstract factory should hide factory implementation details, allowing different factories to be swapped in and out without affecting the client.

Answers which suggest the purpose of the AbstractFactory pattern is to produce concrete factories are simply wrong. Unfortunately, that misinformation is deceptively intuitive, so incorrect definitions are rampant on SO, many with hundreds of upvotes.

The answers here give a pretty good explanation of AbstractFactory; but beware the comment suggesting a duplicate. Answers in the linked thread will ruin any understanding of patterns.

Community
  • 1
  • 1
jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • Factory method along with Factory type enum can be used to hide the concrete factory classes from the client code but then the again we will have the issue of if-else or switch cases. If using only simple factory method we would have created products using if-else or switch cases and now we would be creating the concrete factories. I know another way is to use reflection but that is also not a very good option. – nits.kk Mar 14 '16 at 14:35
  • 1
    Factory Method, Simple Factory, and Abstract Factory are three different things. As @Glenner003 states in another answer, Abstract Factory may be an interface whose concrete implementation is injected through DI. – jaco0646 Mar 14 '16 at 14:57
2

The Abstract factory doesn't have to be an abstract class, most of the time it is just an interface.

The concrete factory to use should be injected with DI. This way the consumers of the factory are unaware which one is used.

Glenner003
  • 1,450
  • 10
  • 20