Why base abstract factory is needed Abstract factory pattern?

In Abstract Factory Pattern the major entities involved are

  • Abstract Base Factory
  • Concrete Factories each extending Abstract base factory
  • Client code
  • Base product
  • 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 createProduct method. 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 classes can put the factories in some queue and later queue can be iterated and Concrete products can be obtained.

    Please provide your valuable feedback.


    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.


    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.

    链接地址: http://www.djcxy.com/p/85120.html

    上一篇: Jquery thickbox与Asp.net mvc中的Url.Action链接一起工作

    下一篇: 为什么需要基地抽象工厂抽象工厂模式?