Skip to content
Programmer block logo for blog for Java, Cloud tutorials
Programmer Block
  • About Us
  • Programming
    • Java
  • Cloud Computing
    • AWS Cloud
  • System Design
  • Contact Us
Programmer block logo for blog for Java, Cloud tutorials
Programmer Block
Simple Factory Design Pattern in Java

Simple Factory Design Pattern In Java

Himanshu Gupta, June 16, 2023December 14, 2023

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();
			break;
		case LifeStyleBlog.LIFESTYLE_BLOG_NAME:
			blog = new LifeStyleBlog();
			break;
		default:
			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
		System.out.println(blog.createBlog());
		blog = BlogFactory.createBlog("LifeStyleBlog");
		// this will print the lifestyle blog
		System.out.println(blog.createBlog());
	}
}

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.

Conclusion

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

  • Event-Driven Architecture: A Guide to Scalable and Resilient Systems

    December 20, 2023

    In the ever-evolving landscape of software architecture, one paradigm that has gained significant traction is…

  • Load Balancers: Optimizing System Design

    December 7, 2023

    In today’s digital landscape, applications face massive user traffic, demanding efficient handling and consistent performance….

FacebookTweetPinLinkedInEmail
Java Programming design patterndesign pattern in javasimple factory patternsoftware development

Post navigation

Previous post
Next post

Subscribe to Our Newsletter

Subscribe our Newsletter

Subscribe to our newsletter to get the latest updates on new Cloud and Java tutorials and Lab challenges

We don’t spam!

Check your inbox or spam folder to confirm your subscription.

Categories

  • AWS Cloud
  • Cloud Computing
  • Java
  • Programming
  • System Design

Recent Posts

  • Event-Driven Architecture: A Guide to Scalable and Resilient Systems December 20, 2023
  • Load Balancers: Optimizing System Design December 7, 2023
  • Mastering System Design Scaling Principles: Strategies and Techniques October 26, 2023
  • Prototype Design Pattern in Java: A Comprehensive Guide October 20, 2023
  • Simple Factory Design Pattern In Java June 16, 2023
©2025 Programmer Block | WordPress Theme by SuperbThemes