What is the Difference Between Factory Pattern and Abstract Factory Pattern

The main difference between factory pattern and abstract factory pattern is that the factory pattern provides a method of creating objects without specifying the exact class used to create it while the abstract factory pattern provides a method to combine a group of individual factories without specifying their concrete classes.

Design patterns are the solutions to general problems software developers face when developing software. They are generic and do not depend on the system type or the programming language. Moreover, they are proven solutions and provide best practices. There are three types of design patterns as creational, structural and behavioral. Creational patterns provide a method to create objects while hiding the creational logic. In addition, they make the program more flexible. Factory pattern and abstract factory pattern are two such creational design patterns.

Key Areas Covered

1. What is Factory Pattern
     – Definition, Functionality
2. What is Abstract Factory Pattern
     – Definition, Functionality
3. What is the Difference Between Factory Pattern and Abstract Factory Pattern
     – Comparison of Key Differences

Key Terms

Abstract Factory Pattern, Factory Pattern

Difference Between Factory Pattern and Abstract Factory Pattern - Comparison Summary

What is Factory Pattern

A factory pattern provides a good method to create objects. Using this method, the programmer can create objects without exposing the creational logic. It is also possible to refer the newly created object using a common interface. Refer below program.

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 1

Figure 1: Shape Interface

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 2

Figure 2: Rectangle Class

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 3

Figure 3: Square class

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 4

Figure 4: Circle class

Difference Between Factory Pattern and Abstract Factory Pattern

Figure 5: ShapeFactory class

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 6

Figure 6: Main class

The Shape interface has an abstract method called draw. Rectangle, Square and Circle are classes. They implement Shape interface. Therefore, these classes provide an implementation for the method draw. The draw method in each class has their own implementation. The ShapeFacotry class has a method called getShape. It gets a string.  This method will return an object according to the received string.

In the main method, there is an object called shape. It is of type ShapeFactory. The getShape method is called using that object. The string “Rectangle” is passed to it. The returning object stores to shape1, which is of Type Shape. Likewise, the strings “SQUARE” and “CIRCLE” are passed to getShape. The return objects are stored to shape2 and shape3. Each object calls the draw method and the corresponding draw method executes according to the passed string.

The main program uses the ShapeFactory class to get a Shape object. It passes the required string to ShapeFactory to get the type of object it needs.

What is Abstract Factory Pattern

Abstract Factory Pattern uses a super factory and creates other factories. It is also known as a factory of factories. In this pattern, an interface creates a factory of related objects without explicitly specifying their classes. In other words, after creating the super factory, it can create other factories. Refer below program.

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 7

Figure 7: Color interface

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 8

Figure 8: Yellow class

Main Difference - Factory Pattern vs Abstract Factory Pattern

Figure 9: Orange class

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 10

Figure 10:  Purple class

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 11

Figure 11:  AbstractFactory class

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 12

Figure 12: ShapeFactory class

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 13

Figure 13: ColorFactory class

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 14

Figure 14: FactoryProducer class

Difference Between Factory Pattern and Abstract Factory Pattern_Figure 15

Figure 15:  Main program

Interface Shape and the classes Rectangle, Square, Circle are same as the previous section. The Color is an interface; it has an abstract method called fill. The classes Yellow, Orange and Purple implement that interface. Therefore, these class provide implementation for the fill method. The AbstrcatFactory class has two methods as getShape and getColor. The ShapeFactory class extends AbstractFacotry. Similarly, the ColorFactory class extends AbstractFactory. These classes have the implementations for getShape and getColor methods.

FactoryProducer class has a method called getFactory which returns an AbstractFactory type object. It returns a ShapeFactory object or ColorFactory object or null depending on the string it gets. Finally, the main method uses the FactoryProducer to get AbstractFactory to get factories of concrete classes.

Moreover, the main method uses FactoryProducer to get a AbstractFactory object. It passes information such as “RECTANGLE”, “SQUARE”, “CIRCLE” to AbstractFactory to get the type of object it needs. Furthermore, it also passes information such as “YELLOW”, “ORANGLE”, “PURPLE” to get the type of object it needs. 

Difference Between Factory Pattern and Abstract Factory Pattern

Definition

Factory pattern is a creational pattern that uses a method to deal with the problem of creating objects without having to specify the exact class of the object that will be created. Abstract factory pattern, on the other hand, is a creational pattern that provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes. Thus, these definitions explain the main difference between factory pattern and abstract factory pattern.

Hiding the Construction

Another difference between factory pattern and abstract factory pattern is that the factory pattern hides the construction of a single object while abstract factory pattern hides the construction of a family of related objects.

Conclusion

Factory pattern and Abstract Factory pattern are two creational design patterns. Factory pattern provides a method of creating objects without specifying the exact class used to create it. In contrast, abstract factory pattern provides a method to combine a group of individual factories without specifying their concrete classes. Therefore, this is the main difference between factory pattern and abstract factory pattern.

Reference:

1. “Factory Method Design Pattern .” Javatpoint, Available here.
2. “Abstract Factory Pattern .” Javatpoint, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply