Creational Design Pattern - Abstract Factory

 There are 5 types of creational design patterns - Abstract Factory, Builder, Factory Method, Prototype and Singleton. 

Abstract Factory Design Pattern

Abstract Factory is used for creation of various types of similar objects. Before explanation about the structure of this pattern, take a look at the following diagram. 


 

The Abstract Factory Pattern may seem complex at first, but once you understand its components, it becomes much easier to work with. Here's a simple breakdown of how it operates.

It consists of six key components:

  1. Interface: An interface acts as a blueprint that multiple classes will implement. In this example, Food_Drink_Interface defines a food_drink( ) method with an empty body. This method is implemented by multiple concrete classes such as Burger, Pizza, Coffee, and Tea.

  2. Abstract Factory: An abstract factory helps create related objects without needing to know which concrete class is being instantiated. In this case, Food_Drink_Factory is the main abstract class, and two other factories - FoodFactory and DrinkFactory extend the Food_Drink_Factory class. 

  3. Concrete Factories: There can be multiple concrete factories that extend the abstract factory. In this example, FoodFactory and DrinkFactory are specialized in creating objects related to either food or drinks, respectively.

  4. Factory Producer: This is a higher-level class that uses the abstract factory. It uses Food_Drink_Factory and the getFactory( ) method to accept an input parameter. Based on the input (either "Food" or "Drink"), the factory producer redirects to either the FoodFactory or the DrinkFactory . 

  5. Concrete Products: The FoodFactory and DrinkFactory are responsible for creating concrete objects like BurgerPizzaCoffee, and Tea based on the parameters passed from the user interface. Each product implements the Food_Drink_Interface and defines its own version of the food_drink( ) method. If the parameter is 'Burger', FoodFactory will create a Burger object. If it is 'Coffee', DrinkFactory will create a Coffee object. In this way, concrete classes are encapsulated and isolated from user interface.

  6. User Interface: The user interface serves as the entry point for the user. This is where the main( ) method is located, allowing users to execute and interact with the system. It interacts with the abstract factory to get specific objects, such as burgers or coffee, without needing to know the exact class of the object being created. 



In which kind of real-world projects, abstract factory pattern is used?

  1. GUI Toolkit & Libraries: You may have already familiar with GUI toolkit and libraries if you use any kind of IDE - Integrated Development Environment such as Eclipse, Visual Studio Code or NetBeans. Developers can access any toolkit and libraries which are platform independent. So when developing these Toolkit & libraries, you can use abstract factory design pattern. This will enable you to expand your toolkit effortlessly, minimizing code complexity and maintaining a loose coupling.
  2. Game Development: The abstract factory pattern can also be applied in game development. It enables the creation of complex game characters and objects while keeping them organized and easily extensible without altering the core concepts.
  3. Database Abstraction: Imagine you have an application or a web-based database editor that needs to interact with various types of databases, such as MySQL, MongoDB, PostgreSQL, Oracle, or Firebase. In this case, you can apply the abstract factory pattern. This approach encapsulates your database concrete classes, allowing the user interface to interact with the interface and factory rather than directly using the concrete classes.
There are still many other real-world applications such as e-commerce platforms, or enterprise software, and many more where you can use abstract factory.


References

Z-Library [Online] Available From:

Gamma, E. et al. (1995) Design Patterns: Elements of Reusable Object-Oriented Software. 1st Ed. New Jersey: Addison-Wesley.

[Accessed: 18.9.2021]


Medium [Online] Available From:

Sharma, A. (2023). Abstract Factory Design Pattern in Java 

https://medium.com/@akshatsharma0610/abstract-factory-design-pattern-in-java-45a326c8fc9f  

[Accessed 11 Oct. 2024]

Comments

Popular posts from this blog

Distributed Version Control System - Part 1

What is Design Pattern ?

Distributed Version Control System - Part 2