Learn About 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.

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

1

2

3

4

5

6

7

8

9

10

11

12

13

packagecom.eduonix.design.pattern;

/**

*

* @author Aparajita

*

*/

publicclassCarimplementsVehicle{

@Override

publicvoiddrive(){

System.out.println("Inside Car::drive () method.");

}

}

Bike.java

1

2

3

4

5

6

7

8

9

10

11

12

13

packagecom.eduonix.design.pattern;

/**

*

* @author Aparajita

*

*/

publicclassBikeimplementsVehicle{

@Override

publicvoiddrive(){

System.out.println("Inside Bike::drive () method.");

}

}

Truck.java

1

2

3

4

5

6

7

8

9

10

11

12

13

packagecom.eduonix.design.pattern;

/**

*

* @author Aparajita

*

*/

publicclassTruckimplementsVehicle{

@Override

publicvoiddrive(){

System.out.println("Inside Truck::drive () method.");

}

}

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

1

2

3

4

5

6

7

8

9

packagecom.eduonix.design.pattern;

/**

*

* @author Aparajita

*

*/

publicinterfaceColor{

voidfillColor();

}

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

packagecom.eduonix.design.pattern;

/**

*

* @author Aparajita

*

*/

publicclassRedColorimplementsColor{

@Override

publicvoidfillColor(){

System.out.println("Inside Red::fillColor() method.");

}

}

GreenColor.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

packagecom.eduonix.design.pattern;

/**

*

* @author Aparajita

*

*/

publicclassGreenColorimplementsColor{

@Override

publicvoidfillColor(){

System.out.println("Inside Green::fillColor() method.");

}

}

BlueColor.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

packagecom.eduonix.design.pattern;

/**

*

* @author Aparajita

*

*/

publicclassBlueColorimplementsColor{

@Override

publicvoidfillColor(){

System.out.println("Inside Blue::fillColor() method.");

}

}

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

1

2

3

4

5

6

7

8

9

10

11

12

13

packagecom.eduonix.design.pattern;

/**

*

* @author Aparajita

*

*/

publicabstractclassAbstractFactoryClass{

abstractColor getColor(Stringcolor);

abstractVehicle getVehicle(Stringvehicle);

}

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

packagecom.eduonix.design.pattern;

/**

*

* @author Aparajita

*

*/

publicclassVehicleFactoryextendsAbstractFactoryClass{

@Override

publicVehicle getVehicle(StringvehicleType){

if(vehicleType==null){

returnnull;

}

if(vehicleType.equalsIgnoreCase("CAR")){

returnnewCar();

}elseif(vehicleType.equalsIgnoreCase("BIKE")){

returnnewBike();

}elseif(vehicleType.equalsIgnoreCase("TRUCK")){

returnnewTruck();

}

returnnull;

}

@Override

Color getColor(Stringcolor){

returnnull;

}

}

ColorFactory.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

packagecom.eduonix.design.pattern;

/**

*

* @author Aparajita

*

*/

publicclassColorFactoryextendsAbstractFactoryClass{

@Override

publicVehicle getVehicle(StringvehicleType){

returnnull;

}

@Override

Color getColor(Stringcolor){

if(color==null){

returnnull;

}

if(color.equalsIgnoreCase("RED")){

returnnewRedColor();

}elseif(color.equalsIgnoreCase("GREEN")){

returnnewGreenColor();

}elseif(color.equalsIgnoreCase("BLUE")){

returnnewBlueColor();

}

returnnull;

}

}

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

packagecom.eduonix.design.pattern;

/**

*

* @author Aparajita

*

*/

publicclassFactoryProducerClass{

publicstaticAbstractFactoryClass getFactory(Stringchoice){

if(choice.equalsIgnoreCase("VEHICLE")){

returnnewVehicleFactory();

}elseif(choice.equalsIgnoreCase("COLOR")){

returnnewColorFactory();

}

returnnull;

}

}

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.

1

2

3

4

5

6

Inside Car::drive()method.

Inside Bike::drive()method.

Inside Truck::drive()method.

Inside Red::fillColor()method.

Inside Green::fillColor()method.

Inside Blue::fillColor()method.

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.