Demystifying Design Patterns: Factory Method Design Pattern

  1. Demystifying Design Patterns: Singleton Design Pattern
  2. Demystifying Design Patterns: Factory Method Design Pattern
  3. Demystifying Design Patterns: Abstract Factory Design Pattern
  4. Demystifying Design Patterns: Builder Design Pattern
  5. Demystifying Design Patterns: Prototype Design Pattern
  6. Demystifying Design Patterns: Adapter Design Pattern
  7. Demystifying Design Patterns: Bridge Design Pattern
  8. Demystifying Design Patterns: Composite Design Pattern
  9. Demystifying Design Patterns: Decorator Design Pattern
  10. Demystifying Design Patterns: Proxy Design Pattern
  11. Demystifying Design Patterns: Observer Design Pattern
  12. Demystifying Design Patterns: Strategy Design Pattern
  13. Demystifying Design Patterns: Command Design Pattern
  14. Demystifying Design Patterns: State Design Pattern
  15. Demystifying Design Patterns: Chain of Responsibility Design Pattern
  16. Demystifying Design Patterns: Visitor Design Pattern
  17. Demystifying Design Patterns: Template Method Design Pattern

Welcome to the second installment of our “Demystifying Design Patterns” series. In this exploration of design patterns, we will delve into the Factory Method Design Pattern—a versatile tool for object creation that enhances flexibility and extensibility in software development.


The Factory Method Design Pattern, a member of the creational design pattern family, provides a structured approach to object creation. It defines an interface for creating objects but delegates the responsibility of instantiation to its subclasses. This pattern promotes the “Open-Closed Principle,” allowing you to extend a system’s functionality without modifying existing code.

Understanding the Factory Method Pattern

To grasp the Factory Method pattern, let’s dissect its core components:

– Creator: An abstract class or interface that declares the Factory Method.

– ConcreteCreator: Subclasses of the Creator that implement the Factory Method to produce specific objects.

– Product: An interface or abstract class representing the objects created by the Factory Method.

– ConcreteProduct: Subclasses of Product that implement the actual objects.

The Factory Method pattern introduces a level of indirection between client code and the actual object creation process. It allows clients to work with abstract product types, leaving the concrete details of object creation to the implementing subclasses.

Factory Method vs. Simple Factory

It’s essential to distinguish between the Factory Method and the Simple Factory pattern:

– Simple Factory: A single factory class responsible for creating objects based on client specifications. It doesn’t provide the same level of flexibility as the Factory Method because it doesn’t involve inheritance or subclassing.

– Factory Method: Each product type has its own factory class, and the client code works with these factories. This allows for greater customization and flexibility, making it a preferred choice for complex systems.

Real-World Examples of Factory Method

Let’s explore real-world scenarios where the Factory Method pattern proves invaluable:

Example 1: Document Creation

Imagine a document creation application where users can create various document types like PDF, Word, and Text documents. Each document type has its own creation logic.


Java Example:

interface Document {
    void create();

class PDFDocument implements Document {
    public void create() {
        System.out.println("Creating a PDF document");

class WordDocument implements Document {
    public void create() {
        System.out.println("Creating a Word document");

abstract class DocumentCreator {
    public abstract Document createDocument();

class PDFDocumentCreator extends DocumentCreator {
    public Document createDocument() {
        return new PDFDocument();

class WordDocumentCreator extends DocumentCreator {
    public Document createDocument() {
        return new WordDocument();

Example 2: Pizza Delivery

In a pizza delivery system, different types of pizzas (e.g., Margherita, Pepperoni, and Veggie) have their own preparation processes.


Python Example:

from abc import ABC, abstractmethod

class Pizza(ABC):
    def prepare(self):

class MargheritaPizza(Pizza):
    def prepare(self):
        print("Preparing Margherita Pizza")

class PepperoniPizza(Pizza):
    def prepare(self):
        print("Preparing Pepperoni Pizza")

class PizzaStore(ABC):
    def create_pizza(self):

    def order_pizza(self):
        pizza = self.create_pizza()
        pizza. Prepare()

class MargheritaPizzaStore(PizzaStore):
    def create_pizza(self):
        return MargheritaPizza()

class PepperoniPizzaStore(PizzaStore):
    def create_pizza(self):
        return PepperoniPizza()

Factory Method in Object-Oriented Design

In the realm of object-oriented design, the Factory Method pattern brings forth several advantages:

– Open-Closed Principle: The pattern enables the extension of a system’s functionality by introducing new product types through subclassing, without altering existing code.

– Encapsulation: It encapsulates the object creation process within ConcreteCreators, reducing the coupling between client code and product classes.

– Flexibility: Clients interact with abstract product types, fostering flexibility and interchangeability of concrete product implementations.


The Factory Method Design Pattern is a potent tool for object creation in a manner that enhances flexibility and extensibility. It establishes a structured approach where the responsibility of creating objects is delegated to subclasses, aligning with the “Open-Closed Principle.” Real-world applications of this pattern are prevalent in document creation tools, pizza delivery systems, and various other software systems.

As we continue our journey through the world of design patterns, our next article will uncover yet another fascinating pattern. Stay tuned for more insights and demystification!