Introduction to the Factory Method Pattern in Ruby

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 Ruby. We'll cover the following key points:

  1. Understanding the Factory Method Pattern: Learn about 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 in Ruby.
  3. Flexibility and Extensibility: See how the Factory Method Pattern allows your code to handle new object types with ease using Ruby’s dynamic nature.

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

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

# Document class acting as an interface
class Document
  def open
    raise NotImplementedError, "This method should be overridden."
  end
end

# WordDocument implementation
class WordDocument < Document
  def open
    puts 'Opening Word document.'
  end
end

# ExcelDocument implementation
class ExcelDocument < Document
  def open
    puts 'Opening Excel document.'
  end
end

document_creator.rb file that defines the DocumentCreator, responsible for creating all types of documents:

# DocumentCreator class acting as a single factory
class DocumentCreator
  def create_document(type)
    case type
    when :word
      WordDocument.new
    when :excel
      ExcelDocument.new
    else
      raise "Unsupported document type: #{type}"
    end
  end
end

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

# Main script demonstrating the Factory Method Pattern

creator = DocumentCreator.new

# Create a Word document using DocumentCreator and open it
doc = creator.create_document(:word)
doc.open

# Create an Excel document using DocumentCreator and open it
doc = creator.create_document(:excel)
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: A base class representing a document interface with a method open that raises an exception if not implemented. This class defines the common behavior for all document types. Note: In Ruby, the Document class acts as a "duck-typed interface." This means it defines a common method (open) that all subclasses are expected to implement. While Ruby doesn’t enforce interfaces like Java or C#, raising a NotImplementedError serves as a runtime safeguard to ensure the method is overridden in subclasses.
    • WordDocument and ExcelDocument: Concrete classes that implement the Document interface with specific open methods for Word and Excel documents, respectively.
  • DocumentCreator: A single class responsible for creating any type of document. The create_document method instantiates the appropriate document object based on the input type parameter. The DocumentCreator class encapsulates the logic for deciding which type of document to create. By centralizing this decision-making, the class shields the rest of the application from needing to know the specifics of each document type. This decoupling means the client code focuses solely on using the returned objects rather than worrying about their creation.
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