Abstract Factory

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

The abstract factory pattern is used to provide a client with a set of related or dependant objects. The "family" of objects created by the factory are determined at run-time.

An abstract factory is called a factory of factories. In this pattern, you provide a way to encapsulate a group of individual factories that have a common theme.

In this process, you do not mention or specify their concrete classes. This pattern helps you to interchange specific implementations without changing the code that uses them, even at runtime.

Please note these points: In abstract factory pattern -

  • Factories create different types of concrete objects

  • A Factory now represents a “family” of objects that it can create.

  • Factories may have more than one factory method.

Abstract Factory pattern is almost similar to Factory Pattern. It is considered as another layer of abstraction over factory pattern. Abstract Factory patterns work around a super-factory which creates other factories.

Below example should make it clear -