Understanding and Implementing the Factory Method Pattern in PHP

Introduction to the Factory Method Pattern

Welcome back! So far, you’ve learned about the Singleton Pattern and 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 PHP. 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.php file that defines the interface for the product (Document) and concrete products (WordDocument, ExcelDocument):

PHP
<?php

// Document interface for representing a document
interface Document {
    public function open();
}

// WordDocument implementation
class WordDocument implements Document {
    // Concrete implementation of the open function
    public function open() {
        echo "Opening Word document.\n";
    }
}

// ExcelDocument implementation
class ExcelDocument implements Document {
    // Concrete implementation of the open function
    public function open() {
        echo "Opening Excel document.\n";
    }
}

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

PHP
<?php

// DocumentCreator interface for creating documents
interface DocumentCreator {
    public function createDocument(): Document;
}

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

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

index.php file that demonstrates the usage of the Factory Method Pattern:

PHP
<?php

require_once 'Document.php';
require_once 'DocumentCreator.php';

// Create a Word document using DocumentCreator interface and open it
$creator = new WordDocumentCreator();
$doc = $creator->createDocument();
$doc->open();

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

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 interface representing a document with a function open(). This interface 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 interface for creating documents with a 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 a more complex implementation, the createDocument() function would handle more intricate initialization logic, but for simplicity, we're directly instantiating the document objects here.
  • Index File: 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