An Abstract Factory is aimed at creating groups of related objects. The interface of an Abstract Factory will typically contain a number of Factory Methods, one for each type of object to be created.
The main difference between a “factory method” and an “abstract factory” is that the factory method is a single method, and an abstract factory is an object.
The main difference between factory and Abstract factory is factory method uses
inheritance to decide which object has to be instantiated while abstract factory uses
delegation to decide instantiation of object. We can say Abstract factory uses factory
method to complete the architecture. Abstract Factory is one level higher in abstraction
- Factory Method is used to create one product only but Abstract Factory is about creating families of related or dependent products.
- Factory Method pattern exposes a method to the client for creating the object whereas in case ofAbstract Factory they expose a family of related objects which may consist of these Factory methods.
- Factory Method pattern hides the construction of single object where as Abstract factory method hides the construction of a family of related objects. Abstract factories are usually implemented using (a set of) factory methods.
- AbstractFactory pattern uses composition to delegate responsibility of creating object to another class while Factory design pattern uses inheritance and relies on derived class or sub class to create object.
- The idea behind the Factory Method pattern is that it allows for the case where a client doesn’t know what concrete classes it will be required to create at runtime, but just wants to get a class that will do the job while AbstractFactory pattern is best utilised when your system has to create multiple families of products or you want to provide a library of products without exposing the implementation details.!
Factory Pattern Implementation:
AbstractFactory Pattern Implementation: