Abstract Factory Method is a Creational Design pattern that allows you to produce the families of related objects without specifying their concrete classes. Using the abstract factory method, we have the easiest ways to produce a similar type of many objects.
It provides a way to encapsulate a group of individual factories. Basically, here we try to abstract the creation of the objects depending on the logic, business, platform choice, etc.
Problem we face without Abstract Factory Method:
Imagine you want to join one of the elite batches of GeeksforGeeks. So, you will go there and ask about the Courses available, their Fee structure, their timings, and other important things. They will simply look at there system and will give you all the information you required. Looks simple? Think about the developers how they make the system so organized and how their website is so lubricative.
Developers will make unique classes for each course which will contain its properties like Fee structure, timings, and other things. But how they will call them and how will they instantiate their objects ?
Here arises the problem, suppose initially there are only 3-4 courses available at GeeksforGeeks, but later they added 5 new courses.
So, we have to manually instantiate their objects which is not a good thing according to the developer’s side.
Diagrammatic representation of Problems without using Abstract Factory Method
Note: Following code is written without using the abstract factory method
Solution by using Abstract Factory Method:
Its solution is to replace the straight forward object construction calls with calls to the special abstract factory method. Actually there will be no difference in the object creation but they are being called within the factory method.
Now we will create a unique class whose name is Course_At_GFG which will handle all the object instantiation automatically. Now, we don’t have to worry about how many courses we are adding after some time.
Class Diagram for Abstract Factory Method:
Let’s look at the class diagram considering the example of Courses at GeeksforGeeks.
Fee structure of all the available courses at GeeksforGeeks
Timings of all the available courses at GeeksforGeeks
Advantages of using Abstract Factory method:
This pattern is particularly useful when the client doesn’t know exactly what type to create.
- It is easy to introduce the new variants of the products without breaking the existing client code.
- Products which we are getting from factory are surely compatible with each other.
Disadvantages of using Abstract Factory method:
- Our simple code may become complicated due to the existence of lot of classes.
- We end up with huge number of small fies i.e, cluttering of files.
- Most commonly, abstract factory method pattern is found in the sheet metal stamping equipment used in the manufacture of the automobiles.
- It can be used in a system that has to process reports of different categories such as reports related to input, output and intermediate transactions.
- Factory Method - Python Design Patterns
- Command Method - Python Design Patterns
- Facade Method - Python Design Patterns
- Memento Method - Python Design Patterns
- Bridge Method - Python Design Patterns
- Prototype Method - Python Design Patterns
- Proxy Method - Python Design Patterns
- Visitor Method - Python Design Patterns
- Iterator Method - Python Design Patterns
- Flyweight Method - Python Design Patterns
- State Method - Python Design Patterns
- Observer method - Python Design Patterns
- Adapter Method - Python Design Patterns
- Strategy Method - Python Design Patterns
- Builder Method - Python Design Patterns
Further Read: Abstract factory pattern in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.