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:
Interface: An interface acts as a blueprint that multiple classes will implement. In this example,
Food_Drink_Interface
defines afood_drink
( ) method with an empty body. This method is implemented by multiple concrete classes such asBurger
,Pizza
,Coffee
, andTea
.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.
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.
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 .Concrete Products: The FoodFactory and DrinkFactory are responsible for creating concrete objects like
Burger
,Pizza
,Coffee
, andTea
based on the parameters passed from the user interface. Each product implements the Food_Drink_Interface and defines its own version of thefood_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.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?
- 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.
- 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.
- 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.
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
[Accessed 11 Oct. 2024]
Comments
Post a Comment