Developer terms: Factory

What makes an object a 'factory'? Well, today I learned that a factory refers to: An object whose purpose is to create other objects is a factory.... 1 (emphasis mine) For example: class Book attr_reader :title, :author def initialize(title, author) @title = title @author = author end end module BookFactory def self.create(title:, author:) Book.new(title, author) end end In this code snippet, BookFactory is a factory because its (sole) purpose is to create a new instance of Book. S. Metz, Practical object-oriented design: An agile primer using Ruby, 2nd ed. Boston, MA: Addison-Wesley Educational, 2017. ↩

Apr 22, 2025 - 06:08
 0
Developer terms: Factory

What makes an object a 'factory'?

Well, today I learned that a factory refers to:

An object whose purpose is to create other objects is a factory.... 1 (emphasis mine)

For example:

class Book
  attr_reader :title, :author
  def initialize(title, author)
    @title  = title
    @author = author
  end
end

module BookFactory
  def self.create(title:, author:)
    Book.new(title, author)
  end
end

In this code snippet, BookFactory is a factory because its (sole) purpose is to create a new instance of Book.

  1. S. Metz, Practical object-oriented design: An agile primer using Ruby, 2nd ed. Boston, MA: Addison-Wesley Educational, 2017. ↩