Abstract Factory Pattern in Java

by | Sep 15, 2020 | Java

Home » Java » Abstract Factory Pattern in Java

The Abstract Factory pattern is a modification of the Factory Pattern and is extensively used in JAVA to solve creational problems. This design pattern can be thought of as a factory of factory classes. It gives birth to several factories that go on to create objects of a particular type.

Definition

By definition, the Abstract Factory Pattern allows you to create a group of related or similar objects (object factories) from an interface where you do not have to specify concrete classes of the objects. This adds to the abstraction quotient of the factory pattern. When a client software is produced, developers often use this design pattern to provide users with objects created from the generic interface of the application. By means of this design, the implementation and nuances of the object are abstracted from a client who is only concerned about the general usage.

Real-Life Example

As a simple example, consider creating software for a furniture shop. It goes without saying that a furniture shop will have several items to offer. The requirement now is that each item of the furniture shop will also have several variants and the software has to match related furniture objects. What an abstract factory pattern does here is create a separate factory for each class of furniture and the objects those factories create represent a single item of furniture.

Implementation

We will implement the Abstract Factory Design on the ’Shapes’ example in the Factory Pattern article. All we need to do to extend this code is add an abstract class.

public intShapes{            //creating a common interface
void getShape(); }

public class Pentagon impelements intShapes{        // creating a concrete shape class
@override
public void getPentagon() {
System.out.println (“Pentagon”); }

public class Triangle impelements intShapes{          // creating another concrete shape class
@override
public void getTriangle() {
System.out.println (“Triangle”); }

At this stage, an abstract class has to be introduced to create factories for triangles and pentagons

public abstract class AbsFactoryShapes {
abstract intShapes drawShape (String shapeChoice); }

Creating Factory classes

public class FactoryShapeClass {

public intShape drawShape (String shapeChoice){         // creating an object of type intShape
if (shapeChoice == null){                                                    //incase the choice entered is blank
return null; }

if (shapeChoice.equals(“Pentagon”)) {
return new Pentagon();

if (shapeChoice.equals(“Triangle”)) {
return new Triangle();

Finally. we have to create a test class called shapeTest which will call the methods of the Factory class coded above.

public class shapeTest {

public static void main (String [] args) {
FactoryShapeClass obj = new FactoryShapeClass();    //creating an object of the factory class

/* We now need to call the getTriangle and getPentagon methods*/

intShape obj1 = FactoryShapeClass.drawShape(“Triangle”);
obj1.getTriangle();

intShape obj2 = FactoryShapeClass.drawShape(“Pentagon”);
obj2.getPentagon();
}
}

 

When to use the Abstract Factory Class?

To be able to use it in the proper place, it is imperative that one understands its use. Any program that requires creation of several groups of objects that are related to a super factory can use the Abstract Factory class. This also acts as a perfect screen between a client and a developer. A high level of abstraction in this sort of design makes sure a developer’s ingenuity is kept hidden while the client is also satisfied at the same time.

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author