Factory Design Pattern Example in Java

The Factory Design Pattern is a creational design pattern that provides a way to create objects without exposing the object creation logic to the client. It defines an interface or an abstract class for creating objects, but lets the subclasses decide which class to instantiate.

In the Factory Design Pattern, we create an object without exposing the creation logic to the client and refer to newly created objects using a common interface or abstract class. This helps to decouple the client code from the object creation logic and makes the system more flexible and easy to maintain.

The Factory Design Pattern consists of the following components:

  • Creator : An interface or an abstract class that declares the factory method for creating objects. This class can also define a default implementation of the factory method.
  • Concrete Creator : A class that implements the Creator interface and provides an implementation for the factory method.
  • Product : The interface or abstract class that defines the type of object the factory method returns.
  • Concrete Product : A class that implements the Product interface and provides an implementation for the methods defined in the interface.
interface Shape { void draw(); }

In this example, we define an interface called Shape that defines a method draw(). We then created concrete classes Triangle,Circle, Rectangle, Parallelogram and Square, which implement the Shape interface and override the draw() method with their own implementation.

// Create concrete classes implement the Shape interface class Circle implements Shape { public void draw() { System.out.println("Circle::draw() method is called."); } } class Rectangle implements Shape { public void draw() { System.out.println("Rectangle::draw() method is called."); } } class Triangle implements Shape { public void draw() { System.out.println("Triangle::draw() method is called."); } } class Parallelogram implements Shape { public void draw() { System.out.println("Parallelogram::draw() method is called."); } } class Square implements Shape { public void draw() { System.out.println("Square::draw() method is called."); } }

class ShapeFactory { public Shape getShape(String shapeType) { if (shapeType == null) { return null; } if (shapeType.equalsIgnoreCase("CIRCLE")) { return new Circle(); } else if (shapeType.equalsIgnoreCase("RECTANGLE")) { return new Rectangle(); } else if (shapeType.equalsIgnoreCase("SQUARE")) { return new Square(); } else if (shapeType.equalsIgnoreCase("SQUARE")) { return new Square(); } else if (shapeType.equalsIgnoreCase("TRIANGLE")) { return new Triangle(); } else if (shapeType.equalsIgnoreCase("PARRALLELOGRAM")) { return new Parallelogram(); } return null; } }

We then create a factory called ShapeFactory that generates objects of concrete classes based on the given information. The getShape() method in the factory takes a String parameter representing the type of shape to be created, and returns an object of the appropriate class.

public class FactoryDesignPatternExample { public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory(); Shape parallelogram = shapeFactory.getShape("PARRALLELOGRAM"); parallelogram.draw(); Shape triangle = shapeFactory.getShape("TRIANGLE"); triangle.draw(); Shape circle = shapeFactory.getShape("CIRCLE"); circle.draw(); Shape rectangle = shapeFactory.getShape("RECTANGLE"); rectangle.draw(); Shape square = shapeFactory.getShape("SQUARE"); square.draw(); } }

Output

Parallelogram::draw() method is called. Triangle::draw() method is called. Circle::draw() method is called. Rectangle::draw() method is called. Square::draw() method is called.

Finally, in the main() method, we create objects of concrete classes using the factory and call their draw() method to display their respective shapes.

The

The Factory Design Pattern helps in creating objects based on a given set of information, and it allows for flexibility in creating and managing objects of different classes.

Factory Design Pattern advantages for software development

The Factory Design Pattern provides several advantages for software development, including:

  • Encapsulation : The Factory Design Pattern encapsulates the object creation process and hides the complexity of object creation from the client. Clients only need to provide the necessary information to the factory, and the factory takes care of the rest
  • Flexibility : The Factory Design Pattern allows for flexible object creation, as it allows the creation of objects of different classes based on the given information. This allows for easy modification and extension of the system, as new classes can be added without changing the client code.
  • Code maintenance : By centralizing object creation in a factory, code maintenance becomes easier, as changes only need to be made in one place. This improves the overall maintainability of the code.
  • Testing : The Factory Design Pattern makes testing easier, as it allows for the creation of mock objects for testing purposes. This is particularly useful when testing complex systems that depend on multiple objects.
  • Decoupling : The Factory Design Pattern decouples the client code from the object creation process, reducing the overall coupling between classes. This makes the system more modular and easier to manage.

Overall, the Factory Design Pattern is a powerful tool for software development that provides encapsulation, flexibility, and improved maintainability.

Conclusion

In this article we have seen Factory Design Pattern. We also seen how to use Factory Design Pattern in java with examples.
All the classes code is available on GitHub.