Motivation: We want to create several objects belonging to several related abstract types (e.g., UI elements following a consistent theme), without specifying their concrete classes.
Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
This scales up the Factory Method. Instead of one factory method per creator, the abstract factory bundles multiple factory methods sot hat a family of products is always created together. The client depends only on the abstract factory. The result is swapping an entire product family only requires swapping one concrete factory.
interface UIFactory {
fun createButton(): Button
fun createCheckbox(): Checkbox
}
class MaterialUIFactory : UIFactory {
override fun createButton(): Button = MaterialButton()
override fun createCheckbox(): Checkbox = MaterialCheckbox()
}
class CupertinoUIFactory : UIFactory {
override fun createButton(): Button = CupertinoButton()
override fun createCheckbox(): Checkbox = CupertinoCheckbox()
}