lundi 14 mars 2016

Why base abstract factory is needed Abstract factory pattern?

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 instantiate the Factory classes separate from the code calling the getProduct method on the objects of concrete Factory classes. 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.

Aucun commentaire:

Enregistrer un commentaire