Simple Factory Design Pattern in Java

Simple Factory Design Pattern In Java

In software development, design patterns are crucial in creating robust and maintainable systems. One such pattern is the Simple Factory pattern, also known as the simple factory design pattern.
In this blog post, we will explore the Simple Factory pattern in Java, its implementation, and its numerous benefits to software design.

To learn about Design Patterns in Java and its benefits, please visit Design Patterns in Java.

Let’s dive in!

What is Simple Factory Design Pattern?

The Simple Factory pattern is a creational design pattern that simplifies object creation. It provides a centralized factory class responsible for creating objects based on user specifications, hiding the complexity of the instantiation process.
Using the Simple Factory pattern, developers can decouple object creation from the client code, promoting code organization and reusability.

UML Diagram of Simple Factory Pattern

The UML diagram for the Simple Factory pattern consists of three main components:

  1. Client: The client is the entity that requests an object from the factory. The client is unfamiliar with the specific details of creating objects. The client interacts with the factory to obtain the desired object.
  2. Factory: The factory class creates objects based on the client’s request. It encapsulates the creation logic and shields the client from the complexities of object creation. The factory class typically has a method that takes in parameters specifying the type or attributes of the object to be created.
  3. Product: The product represents the objects created by the factory. It can be an abstract class or an interface that defines the standard interface for all the concrete products created by the factory. Each concrete product implements the methods defined in the product interface.
Simple Design Pattern in Java
Simple Design Pattern in Java

In this UML diagram, the client requests an object by interacting with the factory. The factory class has a method, createBlog(), which instantiates and returns the appropriate concrete product based on the client’s request. The concrete products, represented by TechnicalBlog and LifeStyleBlog, implement the methods defined in the product interface.

The Simple Factory pattern allows the client to obtain objects without directly knowing the details of their creation. It encapsulates the object creation logic within the factory, providing a clean and decoupled way to create objects in a software system.

Implementing the Simple Factory Pattern in Java

To illustrate implementing the Simple Factory pattern in Java, let’s consider a scenario where we have different types of blogs: Technical and LifeStyle.

The Simple Factory pattern can assist in creating instances of these vehicles without exposing the client to the creation process.

public interface Blog {
    void createBlog();

public class TechnicalBlog implements Blog {
    public void createBlog() {
        System.out.println("Creating Technical Blog");

public class Motorcycle implements Blog {
    public void createBlog() {
        System.out.println("Creating LifeStyle Blog");

public class BlogFactory {
	public static BlogPost createBlog(String type) {
		BlogPost blog = null;
		switch (type) {
		case TechnicalBlog.TECHNICAL_BLOG_NAME:
			blog = new TechnicalBlog();
		case LifeStyleBlog.LIFESTYLE_BLOG_NAME:
			blog = new LifeStyleBlog();
			throw new IllegalArgumentException();

		return blog;

// Client code
public class SimpleFactoryClient {

	public static void main(String[] args) {
		BlogPost blog = BlogFactory.createBlog("TechnicalBlog");
		// this will print the technical Blog
		blog = BlogFactory.createBlog("LifeStyleBlog");
		// this will print the lifestyle blog

Benefits of Using the Simple Factory Design Pattern

  • Encapsulation: The Simple Factory pattern encapsulates the complexities of object creation, promoting cleaner and more maintainable code.
  • Code Reusability: By centralizing object creation, the Simple Factory pattern allows for easy factory logic reuse in different application parts.
  • Flexibility and Extensibility: The factory class can be modified or extended without affecting the client code, enabling seamless integration of new vehicle types or modifications to the creation process.
  • Improved Code Organization: The Simple Factory pattern helps organize code by separating the creation and business logic, resulting in a more structured and readable codebase.

Examples of Simple Factory Patterns used in Java

Below are some examples, along with their corresponding code snippets:

  • JDBC (Java Database Connectivity): The Simple Factory pattern is used to create instances of the Connection interface for accessing databases
  • Calendar class: The Simple Factory pattern is used through the getInstance() method to create an instance of the Calendar class based on the default time zone and locale.
  • NumberFormat class: The Simple Factory pattern is utilized with methods like getInstance() and getNumberInstance() to create instances of the NumberFormat class for formatting and parsing numbers.


The Simple Factory design pattern is a valuable tool in software development, providing a streamlined approach to object creation.

By implementing the Simple Factory pattern, developers can achieve code encapsulation, reusability, flexibility, and improved code organization. When faced with complex object creation requirements, consider employing the Simple Factory pattern to simplify your design and create more maintainable software systems.

Source Code Present at Location: Github

AWS Community Builder | Software Architect @ Gigaforce | Serverless | Aspiring Entrepreneur | Quirky Introvert­čśé

Leave a Reply

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