Introduction to the Factory Method Pattern

Introduction to the Factory Method Pattern

Welcome back! So far, you’ve learned about the Singleton Pattern and have seen how it ensures a class has only one instance with a global access point. Now, we’re moving on to another essential creational design pattern: the Factory Method Pattern. This pattern is all about creating objects in a much more flexible way than direct instantiation.

What You'll Learn

In this lesson, you'll explore the Factory Method Pattern in C++. We'll cover the following key points:

  1. Understanding the Factory Method Pattern: Learn the core concepts and when to use this pattern.
  2. Implementing a Factory Method: Discover how to create your own factory methods to instantiate different types of objects.
  3. Flexibility and Extensibility: See how the Factory Method Pattern allows your code to handle new object types with ease.

Here's a glimpse of the code you’ll be working through:

document.hpp file that defines interface for product(Document) and concrete products(WordDocument, ExcelDocument):

#include <iostream>
#include <string>

// Document base class for an interface
class Document {
public:
    virtual ~Document() = default; // Virtual destructor for proper cleanup
    virtual void open() = 0; // Pure virtual function for opening the document
};

// WordDocument implementation
class WordDocument : public Document {
public:
    // Concrete implementation of the open function
    void open() override {
        std::cout << "Opening Word document." << std::endl;
    }
};

// ExcelDocument implementation
class ExcelDocument : public Document {
public:
    // Concrete implementation of the open function
    void open() override {
        std::cout << "Opening Excel document." << std::endl;
    }
};

document_creator.hpp file that defines interface for creator(DocumentCreator) and concrete creators(WordDocumentCreator, ExcelDocumentCreator):

// DocumentCreator base class for an interface
class DocumentCreator {
public:
    virtual ~DocumentCreator() = default; // Virtual destructor for proper cleanup
    virtual Document* createDocument() = 0; // Pure virtual function for creating a document
};

// WordDocumentCreator implementation
class WordDocumentCreator : public DocumentCreator {
public:
    // Concrete implementation of the createDocument function for Word documents creation
    Document* createDocument() override {
        return new WordDocument();
    }
};

// ExcelDocumentCreator implementation
class ExcelDocumentCreator : public DocumentCreator {
public:
    // Concrete implementation of the createDocument function for Excel documents creation
    Document* createDocument() override {
        return new ExcelDocument();
    }
};

main.cpp file that demonstrates the usage of the Factory Method Pattern:

int main() {
    // Create a Word document using DocumentCreator pointer and open it
    DocumentCreator* creator = new WordDocumentCreator();
    Document* doc = creator->createDocument();
    doc->open();

    // Create an Excel document using DocumentCreator pointer and open it
    creator = new ExcelDocumentCreator();
    doc = creator->createDocument();
    doc->open();

    delete doc;
    delete creator;

    return 0;
}

This snippet demonstrates a simple implementation of the Factory Method Pattern using different document types. Let's break down the code and understand how the Factory Method Pattern works in practice:

  • Document: An abstract base class representing a document interface with a pure virtual function open(). This class defines the common behavior for all document types.
    • WordDocument and ExcelDocument: Concrete classes that implement the Document interface with specific open() functions for Word and Excel documents, respectively.
  • DocumentCreator: An abstract base class for creating documents with a pure virtual function createDocument(). This class acts as the factory method interface.
    • WordDocumentCreator and ExcelDocumentCreator: Concrete classes that implement the DocumentCreator interface to create Word and Excel documents, respectively. In reality the createDocument() function would be a factory method that creates the specific document type with more complex initialization logic, but for simplicity, we're directly instantiating the document objects here.
  • Main Function: Demonstrates how to use the factory method to create Word and Excel documents through the DocumentCreator interface.

In short for the Factory Method Pattern we need a product interface (Document), concrete products (WordDocument, ExcelDocument), creator interface (DocumentCreator), and concrete creators (WordDocumentCreator, ExcelDocumentCreator) to create different types of products.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal