Why do we need Abstract factory design pattern?

Most of the definition says:

An abstract factory provides an interface for creating families of related objects without specifying their concrete classes

What is the use of Abstract Factory Pattern as we can achieve the task via creating object of concrete class itself. Why do we have a factory method that creates object of Concrete class?

Please provide me any real life example where I must implement abstractFactory pattern?


Abstract Factory is a very central design pattern for Dependency Injection (DI). Here's a list of Stack Overflow questions where application of Abstract Factory has been accepted as the solution.

To the best of my understanding, these questions represent real concerns or problems that people had, so that should get you started with some real-life examples:

  • Is there a pattern for initializing objects created via a DI container
  • Can't combine Factory / DI
  • WCF Dependency injection and abstract factory
  • How to set up IoC when a key class needs Session (or other context-specific variable)
  • How to Resolve type based on end-user configuration value?
  • Strategy Pattern and Dependency Injection using Unity
  • Abstract factory pattern on top of IoC?
  • Is this the correct way to use and test a class that makes use of the factory pattern?
  • DDD Book, Eric Evans: Please explain what is meant by "The FACTORY should be abstracted to the type desired rather than the concrete class(es) created."
  • DI container, factory, or new for ephemeral objects?
  • How to unit test instance creation?
  • What is the best strategy for Dependency Injection of User Input?

  • A real life example for the use of the Abstract Factory pattern is providing data access to two different data sources (eg a SQL Database and a XML file). You have two different data access classes (a gateway to the datastore). Both inherit from a base class that defines the common methods to be implemented (eg Load, Save, Delete).

    Which data source shall be used shouldn't change the way client code retrieves it's data access class. Your Abstract Factory knows which data source shall be used and returns an appropriate instance on request. The factory returns this instance as the base class type.


    If I understand you right - the question is, why do we have both the Factory method and the abstract factory patterns. You need abstract factory when different polymorphic classes has different instantiation procedure. And you want some module to create instances and use them, without knowing any details of object initialization. For example - you want to create Java objects doing some calculations. But some of them are part of the application, while other's bytecode should be read from the DB. In the other hand - why do we need factory method? Agree, that abstract factory overlaps it. But in some cases - it is much less code to write, having less classes and interfaces makes system easier to comprehend.

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

    上一篇: 设计模式:抽象工厂与工厂方法

    下一篇: 为什么我们需要抽象工厂设计模式?