DEV Community

Coder
Coder

Posted on • Originally published at itscoderslife.wordpress.com on

Abstract Factory Design pattern [creational]

The motivation behind using the abstract factory design pattern is to create objects which belong to a group but inherit from several different base classes or implement different protocols.

  • The actual classes which are used to create the objects remained hidden, which lets us modify the factory without having to refactor the caller’s code.
  • The abstract factory design pattern should be applied when you need to create a set of related objects.
  • The approach makes it possible to change the factory classes without modifying the callers.
  • To create single objects, choose the factory method pattern instead.

The abstract factory pattern is same a factory pattern but it hides the classes involved in the creation process

The abstract factory defines a method that will return an implementation of a factory protocol or a factory base class. The concrete implementation class is selected, which is a class that implements the factory method design pattern.

Example :

  • A Computer Store – user or store vendor will never know where and how parts are manufactured
  • A car factory – Same applies here as the computer store

Summary : By hiding the concrete factories that are used to create the objects, this pattern allows us to modify the factories without affecting the caller. The abstract factory pattern should be used if we want to create a set of objects which are part of a group, but do not implement the same protocol or base class. The abstract factory defines a method that will return an implementation of a factory protocol or a factory base class. The concrete implementation class is selected, which is a class that implements the factory method design pattern. Finally, the factory method is invoked, and the required instance is created.

Top comments (0)