The abstract factory pattern is about delegating the creation of objects to a
special object called Factory. A client code that asks the factory to create objects
on its behalf is coupled only with the generic interface that a concrete factory implements.
Therefore various concrete factories are interchangeable.
Moreover the concrete classes of created objects are hidden from clients behind their interfaces. So these classes are also interchangeable. Usually the objects created by a concrete factory belong to the same family.
See https://en.wikipedia.org/wiki/Abstract_factory_pattern for more information.
Imagine that some html renderer or any other client code needs to create a set of these html entities :
- ButtonInterface that can be PlainButton, BootstrapButton, etc.
- TextInputInterface that can be PlainTextInput, BootstrapTextInput, etc.
- PageInterface that can be PlainPage, BootstrapPage, etc.
The client code should not be aware of a concrete class implementing these interfaces.
To achieve this the client code does not create these objects directly, instead it asks any object that implements
HTMLFactoryInterface (AbstractFactory) to do it. This interface has methods to create:
- ButtonInterface (
AbstractProductA), - TextInputInterface (
AbstractProductB), - PageInterface (
AbstractProductC). While HTMLFactoryInterface is an abstract factory, a concrete factory is any object that implements that interface, for example: - PlainHTMLFactory (
ConcreteFactory1) that creates PlainButton (ProductA1), PlainTextInput (ProductB1) and PlainPage (ProductC1) - BootstrapHTMLFactory (
ConcreteFactory2) that creates BootstrapButton (ProductB1), BootstrapTextInput (ProductB2) and BootstrapPage (ProductB3).
