Skip to content

Factory Method Pattern

The Factory Pattern is a well-known creational design pattern used in software development to create objects without specifying the exact class of object that will be created. In Java, the Factory Pattern is implemented using either the Factory Method or the Abstract Factory pattern. The Factory Method pattern involves defining an interface for creating objects, but allowing subclasses to decide which class to instantiate. The Abstract Factory pattern involves creating a factory of factories, where each factory is responsible for creating objects of a particular type.

Factory method pattern overview

The Factory Pattern is widely used in Java programming due to its ability to separate the construction of objects from their use. This separation allows for greater flexibility and modularity in code, making it easier to maintain and update. Additionally, by using the Factory Pattern, developers can reduce code duplication and improve code readability, making it easier for others to understand and work with the codebase.

Overall, the Factory Pattern is a powerful tool in Java programming that can greatly improve the quality and maintainability of code. By separating object construction from object use, developers can create more modular and flexible code that is easier to update and maintain over time.

Understanding Factory Pattern in Java#

The Factory Pattern is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. In Java, the Factory Pattern is implemented using the Factory Method or the Abstract Factory pattern.

The Factory Method pattern defines an interface for creating objects, but allows subclasses to decide which class to instantiate. This pattern is useful when there are multiple subclasses of a superclass and the exact class of object to be created is not known beforehand. The Factory Method pattern provides a way to encapsulate the object creation process and to make it more flexible.

On the other hand, the Abstract Factory pattern provides an interface for creating families of related objects without specifying their concrete classes. This pattern is useful when there are multiple families of objects that need to be created and the exact classes of objects to be created are not known beforehand. The Abstract Factory pattern provides a way to encapsulate the creation of objects and to make it more flexible.

Both the Factory Method and Abstract Factory patterns are examples of creational design patterns, which are used to create objects in a system. These patterns provide a way to encapsulate the object creation process and to make it more flexible.

In Java, the Factory Pattern is widely used in frameworks such as Spring and Hibernate. It is also used in the standard Java libraries, such as the JDBC API, which provides a way to create database connections using the DriverManager class.

Overall, the Factory Pattern is a powerful tool in Java programming that can help developers create flexible and extensible systems. By encapsulating the object creation process, the Factory Pattern allows for more maintainable and reusable code.

Example#

There are several variations of the Factory Pattern, and here is an example of the simple Factory Pattern:

// Step 1: Define a common interface for products
interface Product {
    void create();
}

// Step 2: Implement concrete products
class ConcreteProduct1 implements Product {
    @Override
    public void create() {
        System.out.println("Creating Concrete Product 1");
    }
}

class ConcreteProduct2 implements Product {
    @Override
    public void create() {
        System.out.println("Creating Concrete Product 2");
    }
}

// Step 3: Create a simple factory class
class ProductFactory {
    // Factory method to create products based on a type
    public Product createProduct(String type) {
        if ("1".equals(type)) {
            return new ConcreteProduct1();
        } else if ("2".equals(type)) {
            return new ConcreteProduct2();
        } else {
            throw new IllegalArgumentException("Invalid product type");
        }
    }
}

// Step 4: Client code that uses the factory
public class FactoryClient {
    public static void main(String[] args) {
        // Create a product factory
        ProductFactory factory = new ProductFactory();

        // Create products using the factory
        Product product1 = factory.createProduct("1");
        Product product2 = factory.createProduct("2");

        // Use the created products
        product1.create();
        product2.create();
    }
}
Explanation:

Product Interface (Product):

  • Declares the common interface for all concrete products.

Concrete Products (ConcreteProduct1 and ConcreteProduct2):

  • Implement the Product interface and define their specific behavior.

Product Factory (ProductFactory):

  • Contains a factory method (createProduct) that creates products based on a type parameter.
  • The client code interacts with the factory to create products without having to know the concrete classes.

Client Code (FactoryClient):

  • Creates a ProductFactory.
  • Uses the factory to create specific products based on their types.
  • Interacts with the created products through the common interface.

In this example, the ProductFactory encapsulates the creation logic, and the client code uses it to create products without directly instantiating concrete product classes. The Factory Pattern allows for flexibility by centralizing the creation process and decoupling the client code from the specific classes being instantiated.

Core Components of Factory Pattern#

The Factory Pattern is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. This pattern is useful when a class cannot anticipate the type of objects it needs to create beforehand. The following are the core components of the Factory Pattern:

Interface#

The interface is an abstract class or an interface that defines the methods for creating objects. The interface declares a factory method that returns an object of the type defined by the interface. The factory method is usually abstract and is implemented by the concrete classes.

Abstract Class#

The abstract class is a class that cannot be instantiated directly, but can be subclassed. The abstract class provides a default implementation of the factory method. The concrete classes can override the factory method to create different types of objects.

Concrete Class#

The concrete class is a class that implements the factory method of the abstract class or interface. The concrete class is responsible for creating the objects of a specific type. The concrete class can be subclassed, but it is not necessary.

Factory Class#

The factory class is a class that encapsulates the creation of objects. The factory class is responsible for creating the objects of different types based on the input parameters. The factory class can be a separate class or a method of the abstract class or interface.

Constructor#

The constructor is a method that is called when an object is created. The constructor initializes the object's state and sets the initial values of the object's attributes. The constructor can be used to create objects of a specific type.

In summary, the Factory Pattern is a useful pattern for creating objects of different types based on the input parameters. The core components of the Factory Pattern include the interface, abstract class, concrete class, factory class, and constructor. By using this pattern, the code can be made more flexible and easy to maintain.

Factory Method Pattern#

The Factory Method Pattern is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. It is also known as the Virtual Constructor Pattern.

The Factory Method Pattern is useful when a class cannot anticipate the type of objects it needs to create. By delegating the responsibility of object creation to subclasses, the Factory Method Pattern allows for greater flexibility in object creation.

To implement the Factory Method Pattern, a superclass defines an abstract Factory Method that returns an object of a Product class. Subclasses of the superclass then implement the Factory Method to create objects of a specific Product class.

The following table summarizes the key entities involved in the Factory Method Pattern:

Entity

Description

Factory Method Pattern

A creational design pattern that allows for object creation in a superclass, but allows subclasses to alter the type of objects that will be created.

Factory Method

An abstract method in a superclass that returns an object of a Product class.

Implementation

The concrete implementation of the Factory Method in a subclass that creates objects of a specific Product class.

Object Creation

The creation of a new object of a Product class by calling the Factory Method.

In Java, the Factory Method Pattern can be implemented using an abstract class or interface to define the Factory Method. Subclasses of the abstract class or interface then implement the Factory Method to create objects of a specific Product class.

Overall, the Factory Method Pattern is a powerful tool for creating flexible and extensible code. By delegating object creation to subclasses, the Factory Method Pattern allows for greater flexibility in object creation and can help to reduce code duplication.

Advantages of Factory Pattern#

The Factory Pattern is a creational pattern that provides a way to create objects without exposing the creation logic to the client. Instead of using the "new" operator to create an object, the Factory Pattern provides a way to delegate the responsibility of object creation to a separate factory method. Here are some advantages of using the Factory Pattern in Java:

Loose Coupling#

One of the primary advantages of the Factory Pattern is that it promotes loose coupling between objects. By delegating the responsibility of object creation to a separate factory method, the client code is decoupled from the implementation details of the object creation process. This allows the client code to be more flexible and adaptable to changes in the implementation of the object creation process.

Inheritance#

The Factory Pattern promotes inheritance, which is a fundamental concept in object-oriented programming. By using a factory method to create objects, the client code can rely on the inheritance hierarchy to determine the type of object to create. This allows for greater flexibility in the creation process, as new classes can be added to the hierarchy without affecting the client code.

Code Reuse#

The Factory Pattern promotes code reuse by providing a centralized location for object creation. This allows the client code to reuse the same object creation code in multiple places throughout the application, without having to duplicate the code. This can lead to a more efficient and maintainable codebase.

Easy to Test#

The Factory Pattern is easy to test, as it promotes loose coupling and dependency injection. By using a factory method to create objects, the client code can easily swap out the implementation of the factory method with a mock object for testing purposes. This allows for more comprehensive and efficient testing of the application.

In summary, the Factory Pattern provides several advantages in Java development, including loose coupling, inheritance, code reuse, and easy testing. By using a factory method to create objects, the client code is decoupled from the implementation details of the object creation process, leading to a more flexible and adaptable codebase.

Real-World Examples of Factory Pattern#

The Factory Pattern is widely used in the software industry to create objects without exposing the creation logic to the client. Here are some real-world examples of the Factory Pattern:

Car Manufacturing#

In car manufacturing, the Factory Pattern is used to create different types of cars based on the customer's requirements. For example, a customer may request a car with specific features such as leather seats, sunroof, and a navigation system. The factory will use the Factory Pattern to create a car object with the requested features.

Notification Service#

In a notification service, the Factory Pattern is used to create different types of notifications such as email, SMS, and push notifications. The client can request a notification object without knowing the details of how it is created. The factory will use the Factory Pattern to create the requested notification object.

Email Service#

In an email service, the Factory Pattern is used to create different types of email objects such as plain text, HTML, and attachments. The client can request an email object without knowing the details of how it is created. The factory will use the Factory Pattern to create the requested email object.

SMS Service#

In an SMS service, the Factory Pattern is used to create different types of SMS objects such as plain text, Unicode, and flash messages. The client can request an SMS object without knowing the details of how it is created. The factory will use the Factory Pattern to create the requested SMS object.

Overall, the Factory Pattern is a powerful design pattern that is widely used in the software industry to create objects without exposing the creation logic to the client. It is a useful tool for creating complex objects in a simple and efficient manner.

Factory Pattern in JDK#

The Factory pattern is widely used in the Java Development Kit (JDK) and is a creational design pattern that provides an interface for creating objects in a superclass with multiple subclasses. Based on the input, the Factory pattern returns one of the subclasses.

The following JDK classes implement the Factory pattern:

  • java.util.Calendar: The getInstance() method of the Calendar class returns a Calendar object based on the specified time zone and locale.

  • java.util.ResourceBundle: The getBundle() method of the ResourceBundle class returns a ResourceBundle object based on the specified base name, locale, and class loader.

  • java.text.NumberFormat: The getInstance() method of the NumberFormat class returns a NumberFormat object that formats a number based on the specified locale.

  • Wrapper classes: The valueOf() method of the wrapper classes (Boolean, Integer, etc.) returns an object of the respective wrapper class based on the specified string.

  • java.sql.DriverManager: The getConnection() method of the DriverManager class returns a Connection object based on the specified URL, username, and password.

The Factory pattern provides a flexible way to create objects without exposing the creation logic to the client. It also allows the subclass to change the type of object that will be created without affecting the client code.

Overall, the Factory pattern is a powerful design pattern that is widely used in the JDK. It provides a flexible and extensible way to create objects and is a valuable tool for developers.

Factory Pattern in Spring Framework#

The Factory pattern is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. In the Spring Framework, the Factory pattern is used to provide a central point for creating objects that are used throughout an application.

One of the most common uses of the Factory pattern in Spring is to implement the Singleton pattern. The Singleton pattern ensures that there is only one instance of a particular object in an application. This is useful when an object is expensive to create or when only one instance of an object is required to maintain consistency across the application.

In Spring, the Singleton pattern is implemented using the singleton scope. When a bean is defined with the singleton scope, Spring creates only one instance of that bean and returns the same instance every time the bean is requested.

Another important use of the Factory pattern in Spring is to provide abstraction. Abstraction is a technique that allows a class to be used as a generic type, without exposing the implementation details of that class. This is useful when there are multiple implementations of a particular interface, and the implementation details should be hidden from the client code.

In Spring, abstraction is achieved through the use of interfaces. By defining an interface for a particular service, and then providing multiple implementations of that interface, Spring allows the client code to use the service without knowing the implementation details.

Overall, the Factory pattern is an important design pattern in Spring, as it provides a central point for object creation, allows for the implementation of the Singleton pattern, and provides abstraction through the use of interfaces.

Factory Pattern in Object-Oriented Programming#

The Factory pattern is a creational pattern in object-oriented programming. It is used to create objects in a superclass, but the objects are defined in its subclasses. The main class in the pattern is the Factory class which has a method called a factory method. This method is responsible for creating the objects.

The Factory pattern is used to create objects without using the new keyword. This is because the new keyword creates a tight coupling between the classes. The Factory pattern creates a loose coupling between the classes. This makes it easier to change the concrete implementations of the objects.

The Factory pattern is implemented using interfaces and abstract classes. The Factory class is an abstract class or interface. The subclasses of the Factory class are concrete implementations of the objects. The Factory class delegates the creation of the objects to its subclasses.

The Factory pattern is an example of polymorphism in object-oriented programming. The Factory class and its subclasses implement the same interface. This allows the Factory class to create different types of objects.

The Factory pattern is often used in conjunction with a notification interface. The notification interface is used to notify the Factory class when an object is created. The Factory class can then create a new object based on the notification.

The output of a program that uses the Factory pattern is dependent on the concrete implementations of the objects. The Factory pattern allows for easy changes to the concrete implementations of the objects without changing the code in the main class.

In summary, the Factory pattern is a creational pattern in object-oriented programming. It is used to create objects without using the new keyword. The Factory pattern creates a loose coupling between the classes. It is implemented using interfaces and abstract classes. The Factory pattern is an example of polymorphism in object-oriented programming. The output of a program that uses the Factory pattern is dependent on the concrete implementations of the objects. The Factory pattern allows for easy changes to the concrete implementations of the objects without changing the code in the main class.

UML Representation of Factory Pattern#

The Factory pattern is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. In Java, the Factory pattern is implemented using two patterns: Factory Method and Abstract Factory.

To visualize the Factory pattern, a UML class diagram can be used. The diagram consists of four main entities: Product, Creator, ConcreteProduct, and ConcreteCreator.

  • Product: This is the abstract class or interface that defines the common interface for all concrete products.

  • Creator: This is the abstract class or interface that declares the factory method, which returns an object of type Product.

  • ConcreteProduct: This is the concrete class that implements the Product interface.

  • ConcreteCreator: This is the concrete class that implements the Creator interface and overrides the factory method to return an instance of a ConcreteProduct.

Further Examples and Implementations#

The Factory pattern is a widely used design pattern in Java, and there are many examples and implementations available on various platforms. Here are some of the most popular ones:

Concrete Classes#

Concrete classes are an important part of the Factory pattern in Java. These classes are the ones that are responsible for creating the objects that the Factory pattern is designed to produce. For example, in the generatebill class, there are two concrete classes: DomesticPlan and CommercialPlan. These classes are responsible for creating the Bill objects that the Factory pattern is designed to produce.

Programming with the Factory Pattern#

Programming with the Factory pattern in Java can be a bit tricky, especially for beginners. However, once you understand the basic concepts behind the pattern, it becomes much easier to use. One important thing to keep in mind is that the Factory pattern is a static pattern, which means that it is designed to work with static methods and variables. This can be a bit confusing at first, but once you get the hang of it, it becomes second nature.

Output#

The output of the Factory pattern in Java is the object that is created by the concrete classes. This object can be anything that the developer wants it to be, but it is typically some sort of data structure or object that is used by the application. For example, in the generatebill class, the output of the Factory pattern is a Bill object that contains all of the information needed to generate a bill for a customer.

Subclasses#

Subclasses are an important part of the Factory pattern in Java. These classes are used to create the objects that the Factory pattern is designed to produce. For example, in the generatebill class, there are two subclasses: DomesticPlan and CommercialPlan. These subclasses are used to create the Bill objects that the Factory pattern is designed to produce.

Instantiation#

Instantiation is the process of creating an object from a class. In the Factory pattern, instantiation is typically done by the concrete classes. For example, in the generatebill class, the DomesticPlan class is responsible for creating Bill objects for domestic customers, while the CommercialPlan class is responsible for creating Bill objects for commercial customers.