Design Pattern Explained with Examples: Factory Method Pattern, Abstract Factory Pattern

DevGrowth
8 min readOct 16, 2023
Photo by Alex Simpson on Unsplash

This article will use extensive examples and diagrams to explain the factory design pattern(one of the creational design patterns), what you will learn from it includes:

  • Simple Factory
  • Factory Method Pattern
  • Abstract Factory Pattern
  • Applicability

Problem

Before we talk about the patterns, we should first understand what problem they are trying to solve. Let’s take a payment processing in an e-commerce website as an example, long time ago customer can only pay with cheque or wire transfer, later with debit card or credit card, nowadays, customer can also pay with PayPal, Google Pay, Apple Pay. Over the years, the payment processing class in this e-commerce application may look like this:

PaymentProcessor processPayment(String type) {
PaymentProcessor paymentProcessor;

if (type.equals("credit card")) {
paymentProcessor = new CreditCardPaymentProcessor();
} else if (type.equals("google pay")) {
paymentProcessor = new GooglePayPaymentProcessor();
} else if (type.equals("apple pay")) {
paymentProcessor = new ApplePayPaymentProcessor();
} else if (type.equals("paypal")) {
paymentProcessor = new…

--

--

DevGrowth

I write things about software engineering, productivity, self improvement. Hope it will help you in some way :)