What is the Difference Between Factory and Abstract Factory Pattern? with Proper Definition and Brief Explanation

The main difference between them is that the factory pattern provides a method to create objects without specifying the exact class used to create it whereas 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 type of system or the programming language. Furthermore, they are proven solutions and provide best practices. There are three types of design patterns as creational, structural and behavioral. Creation patterns provide a method for creating objects while hiding the creational logic. In addition, they make the program more flexible. Factory pattern and abstract factory pattern are two such creative design patterns.

Key Areas Covered

1. What is factory pattern?
     – Definition, Functionality
2. What is the 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

What is the factory pattern?

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

                                                               Figure 1: form interface

                                                            Figure 2: Rectangular Class

                                                                Figure 3: square class

                                                                 Figure 4: Circle Class

                                                            Figure 5: ShapeFactory Class

                                                              Figure 6: Main Class

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

In the main method, there is an object called form. It is of type ShapeFactory. The getShape method is called using that object. The string “Rectangle” is passed to it. The returned object is stored in shape1, which is of type Shape. Similarly, the strings “SQUARE” and “CIRCLE” are passed to getShape. The returned objects are stored in shape2 and shape3. Each object calls the draw method, and the corresponding draw method is executed according to the passed string.

The main program uses the ShapeFactory class to get a Shape object. Pass the required string to the ShapeFactory to get the type of object you need.

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 its classes. In other words, after you create the super factory, you can create other factories. See the following program.

                                                              Figure 7: Color interface

                                                                Figure 8: Yellow Class

                                                                 Figure 9: Orange Class

                                                                  Figure 10: Purple Class

                                                          Figure 11: AbstractFactory class

                                                          Figure 12: ShapeFactory class

                                                           Figure 13: ColorFactory class

                                                          Figure 14: FactoryProducer class

                                                             Figure 15: Main program

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

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

Also, the main method uses FactoryProducer to get an AbstractFactory object. Pass information like “RECTANGLE”, “SQUARE”, “CIRCLE” to AbstractFactory to get the type of object you need. In addition, it also passes information like “YELLOW”, “ORANGE”, “PURPLE” to get the type of object it needs.

Key Differences

Definition

The factory pattern is a creation pattern that uses a method to deal with the problem of creating objects without having to specify the exact class of the object to be created. The 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. Therefore, these definitions explain the main difference between them.

Hiding the build

Another difference between them is that 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 creative design patterns. The factory pattern provides a method to create objects without specifying the exact class used to create it. In contrast, the abstract factory pattern provides a method of combining a group of individual factories without specifying their concrete classes. Thus, this is the main difference between them.

Reference:

1. “Factory Method Design Pattern.” Javapoint, available here.
2. “Abstract Factory Pattern”. Javapoint, available here.

See More:

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA


Back to top button