Abstract Factory Design Pattern
An interface for creating families of related or dependent objects without specifying their concrete classes. We can say it is just an object maker which can create more than one type of object.
The object it produces is known to the client only by that object's interface, not by the object's actual concrete implementation.
Contents
When to use it?
We use it when we have a requirement to create a set of related objects, or dependent objects which must be used together as families of objects. Concrete classes should be decoupled from clients.
How does it differ from Factory Method?
First of all, both of them fall under Creational category and it means both will solve the problem relating to object creation. Factory Method and Abstract Factory design pattern are about creating objects.
Factory Method Design Pattern
Here, we define an interface which will expose a method which will create objects for us. Return type of that method is never a concrete type; rather, it will be some interface (or may be an abstract class).
- Creates object through inheritance
- Produce only one product
- Implements code in the abstract creator that makes use of the concrete type that sub class produces
Abstract Factory Design Pattern
Here, we define an interface which will create families of related or dependent objects. In simple words, interface will expose multiple methods each of which will create some object. Again, here method return types will be generic interfaces. All these objects will together become part of some important functionality.
- Creates object through composition
- Produce families of products
- Concrete factories implements factory method to create product
UML
The classes and objects participating in the above UML class diagram are as follow.
- AbstractFactory
This is an interface for operations which is used to create abstract product.
- ConcreteFactory
This is a class which implements the AbstractFactory interface operations to create concrete products.
- AbstractProduct
This declares an interface for a type of product object
- Product
This defines a product object to be created by the corresponding concrete factory also implements the AbstractProduct interface
- Client
This is a class which uses AbstractFactory and AbstractProduct interfaces to create a family of related objects.