Factory vs Factory Method vs Abstract Factory Design Pattern

We have three design patterns containing the “factory” word, they are:- Factory Pattern or Simple factory design pattern, Factory method design pattern, Abstract factory design pattern. In their own respective post, we have already discussed this in detail. Now let us see a short comparison between factory pattern vs factory method pattern vs abstract factory pattern.

Factory Design Pattern:- It provides an abstraction on creating one of the several related classes object based on the data we supplied. A simple factory pattern is normally called by the client via a static method and returns one of several objects that all inherit/implement the same parent.

Factory Method Design Pattern:- It makes all factories follow the same standards while creating objects for the same family class. The factory method design pattern is really all about a “create” method that is implemented by subclasses. It provides a set of rules and guidelines to factories that are creating objects for the same family.

Abstract Factory Design Pattern:- It makes the client application get all the related classes objects or group of objects or family of objects coming from the same factory. The abstract factory design pattern is about returning a family of related objects to the client using the same factory. It normally uses the super factory to create one factory class object and this factory class returns other related classes objects.

Factory Design Pattern

It is a simple factory called directly by the class which wants to create an object, the calling class is referred to as the “client”. The simple factory returns one of many different classes. All the classes that a simple factory can return either inherit from the same parent class or implement the same interface. 

Steps in factory design pattern:-

  • We call a method in the factory. Generally this method is declared as static so that we can call then directly without creating object of that class. The parameters pass to this method will tell the factory which class to create.
  • The factory creates the object. The important point which should be noted is that of all the objects it can create, the objects have the same parent class, or implement the same interface.
  • The factory returns the objects. Since client didn’t know what was going to be returned therefore the client is expecting a type that matches the parent class/interface.

Factory Method Design Pattern

The official definition of the factory method design pattern is something like a class that defers the instantiation of an object to subclasses. An important thing to note right away is that when we are discussing the factory pattern, we are not concentrating on the implementation of the factory in the client, but instead, we are examining the manner in which objects are being created.

In this example, the client doesn’t have a direct reference to the classes that are creating the object but instead has a reference to the abstract “Creator” (just because the creator is abstract doesn’t mean this is the abstract factory). It is a factory method because the children of “Creator” are responsible for implementing the “Create” method. Another key point is that the creator is returning only one object. The object could be one of several types, but the types all inherit from the same parent class.

Abstract Factory Design Pattern

An abstract factory is used to create a family of related products, the factory method is used to create one product. Steps involved in abstract factory design pattern:-

  • The client maintains a reference to an abstract factory class, which all factories must implement. The abstract factory is instantiated with a concrete factory.
  • The factory is capable of producing multiple types. This is where the “family of related products” comes into play. The objects which can be created still have a parent class or interface that the client knows about, but the key point is there is more than one type of parent.
  • The concrete factory creates the concrete objects.
  • The concrete objects are returned to the client. Again the client doesn’t really know what the type of the objects are, just that are a children of the parents. 

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *