← Back to list

Factory Method vs Abstract Factory — Explained with Real Backend Examples

Hi everyone!

Navaneethsankar · 2026-02-22 03:21 · 0 claps · 3.4 min read
#factory-vs-abstract #factory-method-pattern #abstract-factory-pattern #design-patterns
Open on Medium ↗
Wiki topics: TLS · Design Tools & Workflow 🌐 · Web Development

Factory Method vs Abstract Factory — Explained with Real Backend Examples

Hi everyone!

In this article, we’re going to understand two important Creational Design Patterns:

  • Factory Method
  • Abstract Factory

These two patterns are often confused because their names sound similar. But they solve slightly different architectural problems.

We’ll break everything down slowly, define every technical term, and use real backend examples — including a logger implementation, which is one of the best real-world examples of Factory Method.

Let’s go.

Table of Contents

  1. What is a Design Pattern?
  2. What is the Factory Method Pattern?
  3. Logger Example (Real Backend Use Case)
  4. What is the Abstract Factory Pattern?
  5. Region-Based Payment Example
  6. Key Differences
  7. When to Use Which
  8. Final Summary

1. What is a Design Pattern?

A design pattern is:

A reusable solution to a commonly occurring software design problem.

Important:

  • It is not a library.
  • It is not a framework.
  • It is not copy-paste code.
  • It is a structured way of solving a recurring problem.

Think of it like:

  • A building blueprint.
  • A standard recipe.
  • A proven architectural solution.

2. What is Factory Method?

Simple Definition

Factory Method is a pattern that:

Defines a method for creating an object, but lets subclasses decide which class to instantiate.

Now let’s define the jargon.

Jargon Explained

Instantiate Creating an object from a class.

Example:

logger = FileLogger()

This is instantiation.

Subclass A class that inherits from another class.

Example:

class FileLogger(Logger)

Here, FileLogger is a subclass of Logger.

Concrete Class A real class you can create objects from.

Example:

FileLogger()

Abstraction Hiding implementation details and exposing only what is necessary.

Example: The system knows it has a “Logger”. It doesn’t need to know whether it is file-based or database-based.

3. Factory Method — Logger Example (Real Backend Pattern)

Imagine a backend system.

We want logging support:

  • File logging
  • Database logging
  • Cloud logging

Instead of writing:

if logger_type == "file":
    logger = FileLogger()
elif logger_type == "db":
    logger = DatabaseLogger()

everywhere in the code, we centralize creation logic.

Step 1 — Product (Base Interface)

class Logger:
    def log(self, message):
        pass

This is the common interface.

Step 2 — Concrete Products

class FileLogger(Logger):
    def log(self, message):
        print("Logging to file:", message)
class DatabaseLogger(Logger):
    def log(self, message):
        print("Logging to database:", message)

Step 3 — Factory Class

class LoggerFactory:
    def create_logger(self, logger_type):
        if logger_type == "file":
            return FileLogger()
        elif logger_type == "db":
            return DatabaseLogger()
        else:
            raise ValueError("Invalid logger type")

Usage

factory = LoggerFactory()
logger = factory.create_logger("file")
logger.log("User created successfully")

Now:

  • Creation logic is centralized.
  • Business logic doesn’t depend on concrete classes.
  • Easy to extend.

This is Factory Method.

4. What is Abstract Factory?

Now let’s move to the bigger pattern.

Simple Definition

Abstract Factory is a pattern that:

Provides an interface for creating families of related objects without specifying their concrete classes.

Now we explain the key word:

What is a “Family of Objects”?

A group of related objects designed to work together.

Example: If region = India:

  • RazorpayPayment
  • SMSNotification

If region = US:

  • StripePayment
  • EmailNotification

Payment and Notification must match region.

That group is called a family.

5. Abstract Factory — Region Example

Step 1 — Abstract Products

class Payment:
    def pay(self):
        pass
class Notification:
    def send(self):
        pass

Step 2 — Concrete Products

India

class RazorpayPayment(Payment):
    def pay(self):
        print("Paying with Razorpay")
class SMSNotification(Notification):
    def send(self):
        print("Sending SMS")

US

class StripePayment(Payment):
    def pay(self):
        print("Paying with Stripe")
class EmailNotification(Notification):
    def send(self):
        print("Sending Email")

Step 3 — Abstract Factory

class RegionFactory:
    def create_payment(self):
        pass
    def create_notification(self):
        pass

Step 4 — Concrete Factories

class IndiaFactory(RegionFactory):
    def create_payment(self):
        return RazorpayPayment()
    def create_notification(self):
        return SMSNotification()
class USFactory(RegionFactory):
    def create_payment(self):
        return StripePayment()
    def create_notification(self):
        return EmailNotification()

Usage

def run_system(factory):
    payment = factory.create_payment()
    notification = factory.create_notification()
    payment.pay()
    notification.send()

The system does not know which region it is using.

It only knows it has:

  • A Payment
  • A Notification

That is abstraction at a higher level.

6. Mental Model

Factory Method answers:

“Which logger should I create?”

Abstract Factory answers:

“Which complete ecosystem should I create?”

7. Why Does This Matter in Backend Architecture?

In real systems like:

  • E-commerce
  • Marketplaces
  • Cloud platforms
  • Payment systems

We often need:

  • Pluggable modules
  • Region-specific behavior
  • Environment-specific configuration

These patterns help:

  • Reduce tight coupling
  • Improve scalability
  • Support Open/Closed Principle

What is Tight Coupling?

When many parts of the system directly depend on concrete classes.

Changing one class breaks many others.

What is Open/Closed Principle?

A system should be:

Open for extension Closed for modification

Meaning: You can add new behavior without modifying existing tested code.

8. When Should You Use Each?

Use Factory Method when:

  • You need to create one type of object
  • You want centralized creation logic
  • You want to remove if-else chains

Use Abstract Factory when:

  • Objects must work together
  • You need ecosystem-level switching
  • You want strong compatibility guarantees

Final Summary

Factory Method:

  • Focuses on creating one object.
  • Simplifies instantiation logic.
  • Great for loggers, parsers, adapters.

Abstract Factory:

  • Focuses on creating related groups of objects.
  • Ensures consistency between components.
  • Useful for region-based systems, themes, environments.

If you understand this difference clearly, you are no longer memorizing patterns — you are thinking architecturally.

And that’s the real goal.


메타데이터
post_id
b6c94b6ebb92
slug
factory-method-vs-abstract-factory-explained-with-real-backend-examples-b6c94b6ebb92
url
https://medium.com/@navaneethsankar07/factory-method-vs-abstract-factory-explained-with-real-backend-examples-b6c94b6ebb92
canonical_url
https://medium.com/@navaneethsankar07/factory-method-vs-abstract-factory-explained-with-real-backend-examples-b6c94b6ebb92
author_url
https://medium.com/@navaneethsankar07
status
ok
fetched_at
2026-06-24 18:57:25