Abstract Factory
Last updated
Last updated
Abstract Factory is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.
In this example:
AnimalFactory
is the abstract factory with methods to create Dog
and Cat
objects.
WildAnimalFactory
and DomesticAnimalFactory
are concrete factories implementing AnimalFactory
.
Dog
and Cat
are abstract product classes with a method sound()
.
WildDog
, DomesticDog
, WildCat
, and DomesticCat
are concrete product classes implementing Dog
and Cat
.
The animalSound()
function acts as the client, which creates Dog
and Cat
objects using the factory and calls their sound()
methods.
Finally, we create instances of concrete factories (wildAnimalFactory
and domesticAnimalFactory
) and pass them to the animalSound()
function to produce sounds of wild and domestic animals.