Factory Design Pattern in Java

 

Factory Design Pattern in Java

Introduction

The Factory Design Pattern is one of the most widely used creational design patterns in Java. It provides a way to create objects without specifying the exact class type, promoting loose coupling and high maintainability.

This article will cover the Factory Design Pattern in detail, including its definition, implementation, advantages, and a real-world case scenario.



What is the Factory Design Pattern?

The Factory Design Pattern provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. It encapsulates object creation logic in a separate class, improving modularity and flexibility.

Key Characteristics of Factory Pattern:

  • Encapsulates object creation logic

  • Supports loose coupling between client and implementation

  • Enhances scalability and maintainability

  • Promotes Dependency Inversion Principle

Implementation of Factory Design Pattern

Step 1: Define an Interface (Product)

// Step 1: Define an interface
interface Vehicle {
    void drive();
}

Step 2: Implement Concrete Classes

// Concrete class: Car
class Car implements Vehicle {
    @Override
    public void drive() {
        System.out.println("Driving a car...");
    }
}

// Concrete class: Bike
class Bike implements Vehicle {
    @Override
    public void drive() {
        System.out.println("Riding a bike...");
    }
}

Step 3: Create a Factory Class

// Factory class to generate objects
class VehicleFactory {
    public static Vehicle getVehicle(String type) {
        if (type.equalsIgnoreCase("car")) {
            return new Car();
        } else if (type.equalsIgnoreCase("bike")) {
            return new Bike();
        }
        return null;
    }
}

Step 4: Use the Factory Class

// Client code using Factory Pattern
public class FactoryPatternExample {
    public static void main(String[] args) {
        Vehicle vehicle1 = VehicleFactory.getVehicle("car");
        vehicle1.drive(); // Output: Driving a car...

        Vehicle vehicle2 = VehicleFactory.getVehicle("bike");
        vehicle2.drive(); // Output: Riding a bike...
    }
}

Advantages of Factory Design Pattern

  • Encapsulation: Hides object creation logic from clients.

  • Loose Coupling: Clients depend on an interface rather than concrete classes.

  • Scalability: New products can be added without modifying client code.

  • Reusability: Reduces code duplication and improves maintainability.

Real-World Case Scenario

Example: Payment Processing System

Imagine a payment processing system that supports different payment methods such as Credit Card, PayPal, and Google Pay. Instead of creating an object of a specific payment method manually, we can use the Factory Design Pattern.

Step 1: Create an Interface

interface Payment {
    void pay(double amount);
}

Step 2: Implement Concrete Classes

class CreditCardPayment implements Payment {
    @Override
    public void pay(double amount) {
        System.out.println("Paid $" + amount + " using Credit Card.");
    }
}

class PayPalPayment implements Payment {
    @Override
    public void pay(double amount) {
        System.out.println("Paid $" + amount + " using PayPal.");
    }
}

Step 3: Create a Factory Class

class PaymentFactory {
    public static Payment getPaymentMethod(String type) {
        if (type.equalsIgnoreCase("creditcard")) {
            return new CreditCardPayment();
        } else if (type.equalsIgnoreCase("paypal")) {
            return new PayPalPayment();
        }
        return null;
    }
}

Step 4: Use the Factory in Client Code

public class PaymentSystem {
    public static void main(String[] args) {
        Payment payment1 = PaymentFactory.getPaymentMethod("creditcard");
        payment1.pay(100.0); // Output: Paid $100.0 using Credit Card.

        Payment payment2 = PaymentFactory.getPaymentMethod("paypal");
        payment2.pay(200.0); // Output: Paid $200.0 using PayPal.
    }
}

Why Use Factory Pattern Here?

  • New payment methods can be added without modifying existing client code.

  • The system is scalable and maintainable.

  • The payment creation logic is centralized in the factory.

Conclusion

The Factory Design Pattern is a powerful tool that helps developers write modular, scalable, and maintainable code. By encapsulating object creation logic, it ensures loose coupling and reusability, making it a preferred choice in real-world applications like banking, e-commerce, and payment processing systems.


Previous
Next Post »