The “Production Line” of Coding World: The Factory Design Pattern!

Mustafa şahin
Huawei Developers
Published in
3 min readDec 27, 2023
Factory Design Pattern

Introduction

Hello there! The Factory Pattern comes into play when object creation gets a bit tricky. Let’s say you’re writing a game with various types of enemy characters. Each enemy requires different characteristics to be created. Here, the Factory Pattern acts like a chef, saying, “This enemy needs these features, and that one those features,” helping you out.

Why Should We Use the Factory Pattern?

  • Less Chaos: It centralizes object creation processes, preventing the “Where did this object come from?” confusion in your code.
  • Flexibility: It allows you to produce different objects for different situations, just like choosing from a diverse menu in a restaurant.
  • Ease of Maintenance: In the future, if you want to add a new type of object to your code, you just update the factory. It’s like adding a few new pieces of furniture instead of redecorating your entire house.

Real-Life Scenario

Imagine you’re developing an e-commerce application, offering various payment options to your customers: Credit Card, PayPal, or Bitcoin. The Factory Pattern steps in and simplifies decisions like, “Should the customer pay with a credit card or PayPal?” Instead of writing separate codes for each payment method, your factory handles it.

Let’s Dive Into the Java Code Example

First, let’s create the necessary interface and classes

Payment Interface

public interface Payment {
void pay(double amount);
}

Credit Card Payment Class

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

PayPal Payment Class

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

Bitcoin Payment Class

public class BitcoinPayment implements Payment {
@Override
public void pay(double amount) {
System.out.println("Payment of " + amount + " made using Bitcoin.");
}
}

Now, Let’s Create the Factory Class

Payment Factory

public class PaymentFactory {
public static Payment getPaymentMethod(String type) {
switch (type) {
case "CreditCard":
return new CreditCardPayment();
case "PayPal":
return new PayPalPayment();
case "Bitcoin":
return new BitcoinPayment();
default:
throw new IllegalArgumentException("Payment type not defined.");
}
}
}

And Finally, the Usage

Main Class

public class Main {
public static void main(String[] args) {
Payment payment = PaymentFactory.getPaymentMethod("CreditCard");
payment.pay(250.0);

payment = PaymentFactory.getPaymentMethod("PayPal");
payment.pay(75.5);

payment = PaymentFactory.getPaymentMethod("Bitcoin");
payment.pay(500.0);
}
}

How Does It Work?

In this code, the PaymentFactory class creates the appropriate payment method object based on the payment type. For example, CreditCardPayment for "CreditCard", PayPalPayment for "PayPal", and BitcoinPayment for "Bitcoin". In the main class, we use this factory method to obtain the necessary payment method and perform the transaction.

Conclusion

The Factory Design Pattern offers a stylish and practical solution to the “How and when should I create this object?” question in software development. By learning this fantastic design pattern, you can make your code more organized, understandable, and easy to maintain, turning your code into an efficient “object production line.”

References

--

--