Factory Pattern in Java

One of the key creational patterns of JAVA, the Factory design pattern in Java is used in programs that deal with several classes. This structure of coding is commonly used in the Java Development Kit. Certain frameworks also make use of this effective pattern such as Spring and Struts. Object manipulation such as testing, adding or removing attributes, and reusing becomes so much easier. Software that primarily uses objects as their major functionality make sure to use Factory Pattern. It provides their code with the much-needed flexibility while dealing with objects.

Definition

As a general description, the Factory design pattern facilitates the creation of objects without showing clients the where and how of the object that has been created. This creates a required server-client demarcation. The pattern works similar to a factory where users only get to see the final product. The object that is created is referred by a common interface between the client and the server.

As a more particular use, the Factory Pattern is preferred when a superclass has more than one sub-class and data from one subclass has to be returned to the superclass.

Example with Code

This is a simple example where there will be a factory shape class. A test class will take the relevant shape objects from the factory classes and create the required shape (for the purpose of this example, simply display a text containing the shape name).

//For the first step, we will need to create an interface

public intShapes{            //creating an 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”); }

Now we have to create a Factory class conforming with the rules of the Factory design pattern

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 Factory Design Pattern in Java?

This design pattern can be used in any program or software that requires efficient object handling. It also plays a crucial role in codes that make the concept of inheritance their top priority. This structure is an effective way of withholding trade secrets and offering clients only the end-product.

Leave a comment

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