Learn About Abstract Factory Design Pattern

1
603
Abstract Factory Design Pattern

 

In the earlier chapter, we discussed about the factory pattern where an object is created without exposing the actual creation logic to the client and with the help of a common interface reference can be made to a newly created object. In this chapter, we are going to discuss about Abstract Factory patterns which behaves as a super-factory that creates a number of other factories. Therefore, such super-factory is also known as factory of factories. Like factory pattern, abstract factory pattern also comes under the category of creational design pattern that provide a common and very simple way to create objects and hide the actual creation logic.

In the Abstract Factory pattern, it uses an interface which is responsible for the creation of a factory of related objects where there is no need to explicitly specify their classes. Each factory which is generated is capable of providing the objects as per the Factory design pattern.

Implementation of Abstract Factory design pattern
To understand the abstract factory design pattern, we are going to create the Vehicle and Color interfaces and their concrete classes (Car, Bike and Truck classes) and (RedColor, GreenColor and BlueColor classes) which implement the Vehicle and Color interfaces respectively. Next, we create an abstract factory class which will be inherited by other factory classes (here VehicleFactory and ColorFactory classes). These classes extends the AbstractFactoryClass. Further, a FactoryProducerClass is created in order to get factories by passing information such as Vehicle or Color type choice.

The implementation class i.e. AbstractFactoryPatternDemo will use FactoryProducerClass to procure an AbstractFactoryClass object. It will pass information such as CAR / BIKE / TRUCK for Vehicle type to AbstractFactoryClass in order to procure the type of object it requires. Further, it will pass information such as RED / GREEN / BLUE for Color type to AbstractFactoryClass in order to procure the type of object it requires.

Let’s summarize below the step by step implementation of Abstract Factory design pattern.

Step 1: – Creation of an interface i.e. Vehicle.java

Step 2: – Creation of the three concrete classes (i.e., Car, Bike and Truck classes) which implement the same interface i.e. Vehicle.java
Car.java

Bike.java

Truck.java

Step 3: – Creation of an interface i.e. Color.java
Color.java

Step 4: – Creation of the three concrete classes (i.e., RedColor, GreenColor and BlueColor classes) which implement the same interface i.e. Color.java
RedColor.java

GreenColor.java

BlueColor.java

Step 5: – Creation of an Abstract class to get factories for Color and Vehicle Objects.
AbstractFactoryClass.java

Step 6: – Creation of the Factory classes extending AbstractFactoryClass in order to generate object of concrete class based on the available information.
VehicleFactory.java

ColorFactory.java

Step 7: – Creation of a Factory generator/producer class in order to get factories by passing an information such as Vehicle or Color type choice.
FactoryProducerClass.java

Step 8: – Now, we can use the FactoryProducerClass to get AbstractFactoryClass in order to get factories of concrete classes by providing an information such as choice type (vehicle or color).
AbstractFactoryPatternDemo.java

Step 9: – When we execute AbstractFactoryPatternDemo class as a Java application then will observe the following desired output which demonstrate the Abstract Factory design pattern.

Conclusion: –
In this chapter, we discussed about the abstract factory design pattern in detail along with a suitable example. In that example we have demonstrated the step by step creation of an Interface, concrete classes, abstract factory class, factory producer class and Abstract Factory design pattern implementer class.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here